Showing posts with label Tips and Tricks. Show all posts
Showing posts with label Tips and Tricks. Show all posts

Dec 22, 2010

MySQL: Partition-wise backups with mysqldump

How it works

The script works by querying the information_schema.PARTITIONS system view to generate an appropriate expression for mysqldump's --where option. The generated command also redirects the output to a file with this name pattern:
<schema>.<table>.<partition-name>.sql
For example, for this table (taken from the MySQL reference manual):
CREATE TABLE members (
    firstname VARCHAR(25) NOT NULL,
    lastname VARCHAR(25) NOT NULL,
    username VARCHAR(16) NOT NULL,
    email VARCHAR(35),
    joined DATE NOT NULL
)
PARTITION BY RANGE( YEAR(joined) ) (
    PARTITION p0 VALUES LESS THAN (1960),
    PARTITION p1 VALUES LESS THAN (1970),
    PARTITION p2 VALUES LESS THAN (1980),
    PARTITION p3 VALUES LESS THAN (1990),
    PARTITION p4 VALUES LESS THAN MAXVALUE
);
the script generates the following commands:
mysqldump --user=username --password=password --no-create-info --where=" YEAR(joined) < 1960" test members > test.members.p0.sql
mysqldump --user=username --password=password --no-create-info --where=" YEAR(joined) >= 1960 and  YEAR(joined) < 1970" test members > test.members.p1.sql
mysqldump --user=username --password=password --no-create-info --where=" YEAR(joined) >= 1970 and  YEAR(joined) < 1980" test members > test.members.p2.sql
mysqldump --user=username --password=password --no-create-info --where=" YEAR(joined) >= 1980 and  YEAR(joined) < 1990" test members > test.members.p3.sql
mysqldump --user=username --password=password --no-create-info --where=" YEAR(joined) >= 1990 and  YEAR(joined) < 18446744073709551615" test members > test.members.p4.sql
Tip: in order to obtain directly executable output from the mysql command line tool, run the script with the --skip-column-names (or -N) option.

Features

Currently, the script supports the following partitioning methods:

Dec 21, 2010

Selectively dumping data with mysqldump

mysqldump is a command line tool for outputting table structures and data and can be used for backups etc. By default mysqldump will dump all data from a table, but it is possible to select which data to be exported with mysqldump. This post looks at how to do this.

The examples in this post have a table called "mytable" in a database called "test". mytable has three columns: mytable_id, category_id and name, and we will be selectively exporting data that matches a specific category_id.
Using mysqldump to dump all data from the table would look like this, subsituting [username] for your username (the -t flag suppresses the table creation sql from the dump):
mysqldump -t -u [username] -p test mytable
The output from my example table looks like this, once we remove all the extra SQL commands (I've added linebreaks to make it more legible):
INSERT INTO `mytable` VALUES 
  (1,1,'Lorem ipsum dolor sit amet'),
  (2,1,'Ut purus est'),
  (3,2,'Leo sed condimentum semper'),
  (4,2,'Donec velit neque'),
  (5,3,'Maecenas ullamcorper');
If we only wanted to dump data from mytable in category_id 1, we would do this:
mysqldump -t -u [username] -p test mytable --where=category_id=1
which would output this:
INSERT INTO `mytable` VALUES 
  (1,1,'Lorem ipsum dolor sit amet'),
  (2,1,'Ut purus est');
You can also abbreviate --where as -w like so:
mysqldump -t -u [username] -p test mytable -wcategory_id=1
If you need to have spaces in the where query or other special shell characters (such as > and <) then you need to put quotes around the where clause like so:
mysqldump -t -u [username] -p test mytable --where="category_id = 1"
OR
mysqldump -t -u [username] -p test mytable -w"category_id = 1"
You can also use the --where flag to selectively dump data from more than one table, but obviously the columns specified in the where clause need to be in both tables.
An example of dumping data from two tables using the same where clause could look like this, where we are selecting category_id from tables "mytable" and "anothertable":
mysqldump -t -u [username] -p test mytable anothertable --where="category_id = 1"
If category_id exists in both tables then the dump will run without error. If the column doesn't exist, you'll see an error like this:
mysqldump: mysqldump: Couldn't execute 'SELECT /*!40001 SQL_NO_CACHE */ * FROM `anothertable` WHERE category_id=1': Unknown column 'category_id' in 'where clause' (1054)
mysqldump: Got error: 1054: Unknown column 'category_id' in 'where clause' when retrieving data from server
mysqldump is an excellent tool for exporting data from MySQL databases. Using the --where or -w flags allows you to selectively export data from one or more tables which saves you having to export all data from a table if you only need a specific subset.

Mysqldump one table - Taking dump of only one table

Use of mysqldump for taking backup of one table
In this section you will see the example of mysqldump function which takes the backup of only one table.
The mysqldump utility provides many options to take the backup of data as per user requirement. You can specify the name of table be backup in while executing the mysqldump utility.
Let's assume we have to take the backup of table1 from the mydatabase, then you can issue the following command on the console:
mysqldump -u -p mydatabase table1 > table1.sql
To restore the backup you can use the following command:
mysql -u -p mydatabase < table1.sql
The mysqldump is very handy tool, it can be easily used to take the backup of only one table from the database.

MySQL Dump Example

In this tutorial you will come to know about mysqldump command and how to use this command, how to execute, the option it supports etc. Example will make this command more clear.
  MySQLDUMP:
Introduction:
In database we need to store our important data periodically or after certain period of time. MySQL provides several techniques to keep the backup of our important data. In the current and next tutorials we will study about MySQL dump and it's procedures to store the data. You will get several examples in this topic.
In the current tutorial we will study about how to store the data using MySQL dump.
We are assuming that you already have a little knowledge on MySQL and SQL language syntax, if you did not installed MySQL in your system then goto www.mysql.org and download the latest version.
What is MySQLDUMP?
The mysqldump is console based executable utility and it allows us to assign a host of options to get the backup of a database to a file, a different MySQL server running anywhere in the world .
In fact MySQL does not backup our data instead of that it executes a set of basic sql syntaxes like "create table" and "insert into" in the MySQL server to re-create the database/s.
The mysqldump utility is present in the root directory of mysql, let us assume it is c:\mysql and you could find a folder bin in that directory. Using the mysqldump utility we can provide several commands so that we can change the way of taking backups.
There are following ways to invoke mysqldump. Use the following command to take backups:
       prompt/shell> mysqldump [options] db_name [tables]
       prompt/shell> mysqldump [options] --databases DB1 [DB2 DB3...]
       prompt/shell> mysqldump [options] --all-databases

As in the above example  we can see that how to use the command, first of all goto the root directory of MySQL, as in the example it is under xampp directory, then type mysqldump followed by a space and --opt followed by a space --user=root(default user, type the valid username you want ) followed by a space, --password, if you have set the password previously then write the password after equal sign, put a space after that write down the database name, followed by a space greater than sign (>) specify a file name, in which the backup will be taken. The file will be stored in the same folder in which the mysqldump persists. In my example the rose database does not contain any table, and the output of the rose.sql file is as below:


-- MySQL dump 10.13 Distrib 5.1.41, for Win32 (ia32)
--
-- Host: localhost Database: rose
-- ------------------------------------------------------
-- Server version 5.1.41
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2010-04-07 17:10:52
Let us create a simple table and check the output, consider a table userdetails as follows:
`user_id` int(20) NOT NULL AUTO_INCREMENT,
`user_login`
varchar(32) NOT NULL,
`password`
varchar(64) NOT NULL,
PRIMARY KEY
(`user_id`)
Now again execute the command as mentioned above with following modification:
C:\xampp\mysql\bin>mysqldump --opt --user=root --password rose userdetails> rose.sql
Enter password:

Note: Enter password is not a command, as you type the command and press the return key, it will ask for the password of the current user.
If the table is previously build then the above command will rewrite the whole file.
If you check the output of the file rose.sql then it will display the following differences:
-- MySQL dump 10.13 Distrib 5.1.41, for Win32 (ia32)
--
-- Host: localhost Database: rose
-- ------------------------------------------------------
-- Server version 5.1.41
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `userdetails`
--
DROP TABLE IF EXISTS `userdetails`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `userdetails` (
`user_id` int
(20) NOT NULL AUTO_INCREMENT,
`user_login`
varchar(32) NOT NULL,
`password`
varchar(64) NOT NULL,
PRIMARY KEY
(`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `userdetails`
--
LOCK TABLES `userdetails` WRITE;
/*!40000 ALTER TABLE `userdetails` DISABLE KEYS */;
INSERT INTO `userdetails` VALUES (1,'rose','india'),(2,'nie','jackson');
/*!40000 ALTER TABLE `userdetails` ENABLE KEYS */;
UNLOCK
TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2010-04-07 17:46:32
To get the output in the command prompt type the following code:
C:\xampp\mysql\bin>mysqldump --user=root --password rose < rose.sql
Enter password:
Note: Enter password is not a command, as you type the command and press the return key, it will ask for the password of the current user.

If we use --databases option or --all-databases option or do not specify the name of the table then entire databases are dumped. To get any kind of help or to check the options of the version you are currently using, execute mysqldump --help. Output would be as follows:

If mysqldump runs without --quick or --opt option, before dumping the result mysqldump loads the whole result into the system's memory. In MySQL 4.1, --opt is enabled by default  and it can be disabled by --skip-opt. It is recommended that if you are using  recent copy of mysqldump program to generate a dump which will be reloaded in the older version of MySQL, then you should not use the option --opt or -e options.
Few Options supported by mysqldump:
  • mysqldump -? = It displays a long list of options which are supported by mysqldump. To check the options one by one, type mysqldump -?|more.
  • mysqldump --version displays the version of the mysqldump which is in use. The command is as below: C:\xampp\mysql\bin>mysqldump --version
    mysqldump Ver 10.13 Distrib 5.1.41, for Win32 (ia32) 

How to find the mysql version

mysql> select version();
+------------------+
| version()        |
+------------------+
| 5.1.53-community |
+------------------+
1 row in set (0.01 sec)

Jan 8, 2010

publish multiple websites using one solution.

on Visual Studio you can publish only one website at a time otherwise there is a clash between one website with multiple website.if you want to do that means on startup of one website other website also publish . for that you need to do some setting on Solutional Level.

follow the steps :
1- on proejct Solution just right click it & select Product Dependencies.
2- select Dependencies tab.
3- select your main website(startup one) from Dropdown.
4- now check all the web service on "Depends on" section
that you can decide how many site you want to publish at a time of this main site.
5- click ok

now whenever you publish that main site , then it will prompt publish window for all other depended site ,then all other mention site will also get publish , if you dont want to publish that you do the same above procedure to deactivated that ,

Captcha ASP.NET 2.0 Server Control

Captcha control is used to validate user in terms of avoiding spamming on message or comments or registration or so on . implement Captcha on your application , below are the steps how u can do that

Installation instruction:

1. Unzip the downloaded file.
2. Copy MSCaptcha.dll and MSCaptcha.xml files to your /bin application directory.
3. add reference to mscaptcha.dll file.
4. Modify your web.config file, by adding this line to &l;httphandlers> section:
<add verb="GET" path="CaptchaImage.axd" type="MSCaptcha.CaptchaImageHandler, MSCaptcha"/>

5.Add MSCaptcha control to your Visual Studio toolbox (optional)


how to use on Page

1. Add line to your .aspx file:
<%@ Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="cc1" % >

2. add the control whenever you want:

<cc1:CaptchaControl ID="ccJoin" runat="server" CaptchaBackgroundNoise="none" CaptchaLength="5" CaptchaHeight="60" CaptchaWidth="200" CaptchaLineNoise="None" CaptchaMinTimeout="5" CaptchaMaxTimeout="240" / >


3. Add this on your CS file to check or Validate enter captcha is right or wrong


ccJoin.ValidateCaptcha(txtCap.Text);
if (!ccJoin.UserValidated)
{
//Inform user that his input was wrong ...
return;
}



now its look like this way :



Download Here..

Check for valid Internet Connection using C#

on many application we need internet connectio , such as smart client to send receive the data, now in this case below code will help you to make sure that you have proper internet connection or not , the conecpt is like very simple, at first we simply check that if the user has network connection and according to that it will return Success / Fail message.

Now i just ping the two website (www.google.com ,www.yahoo.com) which will ping that site and return fail or success message.


C#
public bool GetIsConnAvail()
{
var success = false;
string StatusMsg;
if (NetworkInterface.GetIsNetworkAvailable() == false)
{
return success;
}
string[] Mysite = { "www.google.com", "www.yahoo.com"};
try
{
using (var ping = new Ping())
{
foreach (var url in Mysite)
{
var replyMsg = ping.Send(url, 300);
if (replyMsg.Status == IPStatus.Success)
{
success = true;
StatusMsg = "Connection Found..."
break;
}
else
{
StatusMsg = "Internet Connection not Found on your machine..."
}

}
}
}

catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
return success;
}



i thing this code will surely help you to search your connection faster than other ways.

How to get the Upper Bound of an Array in C#

hi, if you'r using asp, vbscript and all then you have option call UBound
which will return a UpperBound value of Array. but on C#, you dont have that option call Ubound ,for that you can use GetUpperBound() option - Gets the upper bound of the specified dimension in the Array.

here is an Example which can show how to use GetUpperBound()


For i = MyArray.GetLowerBound(0) To MyArray.GetUpperBound(0)

Next i


like this way you can use both option call GetLowerBound & GetUpperBound ,
GetLowerBound will return lower bound value.here you have to pass (int value)
0 - 1st arrayvalue, 1- 2ndarrayvalue like that,

thank you.

How to know SQL Server version and Edition

some time we just install the sql server , but still we don't know which type of version it is and edition also , basically we need that when we are upgrating current version of new version. Current there is sql server 2008 is there in market,

now how to determine which version of sql 2008 is running out machine,
- run the following T-SQL statement,

SELECT SERVERPROPERTY ('productlevel') ,SERVERPROPERTY('productversion'), SERVERPROPERTY ('edition')

you will get the result like :
RTM | 10.0.1600.22 | Enterprise Edition

RTM is product level which means - 'Release To Manufacturing'.
but what is means of RTM - is a stage on which the distribution of process begins CD of
that product get created and retial packs and then product will get shipp , this is very
long process and because of that RTM date is mostly ahead of the final release date.

10.0.1600.22 means product version.

Enterprise Edition means its a Edition there are some other editions are also there like
Developer,standard ,web,Express ,Workgroup , 64- bit Edition .

The following table lists the Sqlservr.exe version number.

Release
Sqlservr.exe
RTM
2007.100.1600.0
SQL Server 2008 Service Pack 1
2007.100.2531.0