Tuesday, December 24, 2013

MySQL Error: Can't create a new thread (errno 11); if you are not out of available memory


Recently I have met a big trouble in one of my newly migrated server.When ever mysql reached 1005 connections it throws a below error,

mysql error: Can't create a new thread (errno 11); 
if you are not out of available memory, you can consult the manual
for a possible OS-dependent bug 

So I have checked the mysql max_connections,open_file_limit and ulimit.Everything looks fine But still i got the same error.I was googled  and found a solution for it.This error depend upon OS not from MySQL.

So when I checked my  max user processor and open files by using ulimit -a I have seen these two variable has 1024 only so I have increased the max user processor and open files value in server.



Then restarted MySQL.


Now its fixed..



Saturday, November 9, 2013

MySQL Partitioning Basics


Recently I met a big trouble in one of my server it have 1.5TB data in a table but I need only last month data in the particular table.So I have planned to delete old records in the table but If am deleting means it will take so many days to purge it so I have searched and find a solution in Partitioning table after that I have deleted the unwanted partition and got free.
Here I have given the process for mysql partitioning.



Partition can be done by using primary key only and the value should be unique.otherwise it will throw and error as "A PRIMARY KEY must include all columns in the table's partitioning function"

create a table with partition.

ex:

CREATE TABLE testnew (
 Id int(20) unsigned NOT NULL,
 name varchar(20),
 PRIMARY KEY (Id)
)
PARTITION BY RANGE ( ID ) (
    PARTITION first VALUES LESS THAN (1000),
    PARTITION second VALUES LESS THAN (2000),
    PARTITION third VALUES LESS THAN (3000),
    PARTITION fourth VALUES LESS THAN (4000),
    PARTITION fifth VALUES LESS THAN (MAXVALUE))
); 
 
It will create a table with partiton using column "ID".Here the records coming below 1000 will goes to first partition and like records will come from 1001 to 2000 will goes to second partition and if the records will come more than 4000 means it will go to fifth partition here this fifth partition is unlimited.

After the partition we can see some new files in mysql data directory.

-rw-rw---- 1 mysql mysql   56 Nov  9 03:08  testnew.frm
-rw-rw---- 1 mysql mysql   56 Nov  9 03:08 testnew.par
-rw-rw---- 1 mysql mysql  96K Nov  9 03:08 testnew#P#first.ibd
-rw-rw---- 1 mysql mysql  96K Nov  9 03:08 testnew#P#third.ibd
-rw-rw---- 1 mysql mysql  96K Nov  9 03:08 testnew#P#second.ibd
-rw-rw---- 1 mysql mysql  96K Nov  9 03:08 testnew#P#fourth.ibd
-rw-rw---- 1 mysql mysql  96K Nov  9 03:08 testnew#P#fifth.ibd

We can create partiton by date also,
 
CREATE TABLE blue (
 Id int(20) unsigned NOT NULL,
 name int(20),
 lastdate datetime NOT NULL DEFAULT 0,
 PRIMARY KEY (ID,name,lastdate)
)
PARTITION BY RANGE ( TO_DAYS(lastdate) ) (
    PARTITION Jan2014 VALUES LESS THAN (TO_DAYS('2014-02-01')),
    PARTITION Feb2014 VALUES LESS THAN (TO_DAYS('2014-03-01')),
    PARTITION Mar2014 VALUES LESS THAN (TO_DAYS('2014-04-01')),
    PARTITION Apr2014 VALUES LESS THAN (TO_DAYS('2014-05-01')),
    PARTITION May2014 VALUES LESS THAN (TO_DAYS('2014-06-01')),
    PARTITION Jun2014 VALUES LESS THAN (TO_DAYS('2014-07-01')),
    PARTITION Jul2014 VALUES LESS THAN (TO_DAYS('2014-08-01'))
); 

 I will insert records by month wise.We will have partition for each and every month.
 
Add partition for existing table by alter command,


Alter Command :

mysql>alter table blue  PARTITION BY RANGE ( ID ) 
                                (     PARTITION first VALUES LESS THAN (1000),  
                                  PARTITION second VALUES LESS THAN (2000),  
                                     PARTITION third VALUES LESS THAN (3000), 
                                   PARTITION fourth VALUES LESS THAN (4000), 
                             PARTITION fifth VALUES LESS THAN (MAXVALUE))  );
Remove all partition without loss of data:

mysql>ALTER TABLE testnew remove partitioning;

Remove particular partition:


mysql>ALTER TABLE testnew drop partition first;
 
Reorganize the partition table:


mysql>ALTER TABLE testnew REORGANIZE PARTITION first INTO (partition first values less than (500),partition firstone values less than (1000));
Alter the existing table with pt-online-schema-change tool for live servers without locking the incoming queries.
#pt-online-schema-change  --execute --user=admin --password=welcome --host=localhost  --alter "partition by range(ID) (PARTITION first VALUES LESS THAN (1000), PARTITION second VALUES LESS THAN (2000), PARTITION third VALUES LESS THAN (3000), PARTITION fourth VALUES LESS THAN (4000), PARTITION fifth VALUES LESS THAN (MAXVALUE)))" D=test,t=blue





mysqldump: Couldn't execute 'SHOW TRIGGERS LIKE 'Events'': Got error 28 from storage engine (1030)


Are you gettting this below error while taking backup?

mysqldump -u root -p --all-databases > db.sql

mysqldump: Couldn't execute 'SHOW TRIGGERS LIKE 'Events'': Got error 28 from storage engine (1030)

Solution:

Check your disk space its almost full so that it is showing the above error.

Clear your disk and take  dump again.



Monday, October 28, 2013

MySQL 5.6 Mysqldump error : Couldn't execute 'SET OPTION SQL_QUOTE_SHOW_CREATE=1':


Are you getting this below error in MySQL 5.6. while taking backup:

mysqldump: Couldn't execute 'SET OPTION SQL_QUOTE_SHOW_CREATE=1': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPTION SQL_QUOTE_SHOW_CREATE=1' at line 1 (1064)


Solution:

Use the mysqldump path to resolve this issue.

#usr/local/mysql/bin/mysqldump -u root -p --all-databases > databases.sql.


Now its solved.



Monday, October 14, 2013

Create a new MySQL Server with existing MySQL data directory


Do you like to create a new server with existing MySQL data directory ?

Taking dump and import in new server will take so much time to do the process.

So better copy the data directory to new server is a good way.Below I have given the steps that how to copy the data directory and set up a new server.


1.Create a new MySQL Server.

2.Stop this newly created MySQL server.

3.Move this server data directory to some other location.now nothing should not be there in new server data directory.

4.Now copy the data directory files from the existing server it should includes ibdata,log file 0 and 1.(This server should be stop while copying the files)

5.After copied all the files, Check the owner and group name for the data directory files.If the owner and group name are in root then change it to mysql.

7.Start this new MySQL server.

8.After started check the new MySQL error logs to confirm that there is no issues.

9.Login to new MySQL server.

10.Now use the same databases and tables like existing server.



There is a tool to copy the files by taking the data directory backup with "xtrabackup" and restore it in new server. 

 

Note :

If you get this below error:

" ERROR 1146 (42S02): Table 'database.tablename' doesn't exist"

 It means that you have not copied all the files properly from the existing server.

 Now copy the all files properly and start the server and now it will works fine.

   

Monday, September 16, 2013

Percona Tool Kit Installation


How to install Percona Tool Kit.

1.Download the latest tar.gz Percona toolkit from Percona Software download link.

link : http://www.percona.com/downloads/percona-toolkit/LATEST/

#wget  http://www.percona.com/redir/downloads/percona-toolkit/LATEST/percona-toolkit-2.2.4.tar.gz

2. Extract the file.

#tar -xvzf percona-toolkit-2.2.4.tar.gz

3.Go to the file name.

#cd percona-toolkit-2.2.4

4.Do the following steps one  by one

#perl Makefile.PL

#make

#make test

#make install

5.Check the tool kit by using this tool.

#pt-summary

It will show the details of your Server details.

Troubleshooting:

#perl Makefile.PL
Checking if your kit is complete...
Looks good
Warning: prerequisite DBD::mysql 3 not found.
Warning: prerequisite DBI 1.46 not found.
Writing Makefile for percona-toolkit

Fix it by,

#yum install perl-DBI perl-DBD-MySQL


Thank you.

More details :  http://form.percona.com/rs/percona/images/Percona_Toolkit_2_1_9_Operations_Manual.pdf



Thursday, September 12, 2013

MySQL Grant Password Error : ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number



When you creating a new user for a MySQL server with the below method sometime you may get this below error.

Ex:

mysql>grant all privileges on *.* to 'root'@'localhost' identified by password 'welcome';

"ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number"


Solution:

mysql> select password ('welcome');

+-------------------------------------------+
| password ('welcome')           |
+-------------------------------------------+
| *DF216F57F1F2066124E1AA5491D995C3CB57E4C2 |
+-------------------------------------------+

1 row in set (0.02 sec)

mysql>grant all privileges on *.* to 'root'@'localhost' identified by password '*DF216F57F1F2066124E1AA5491D995C3CB57E4C2';

 Query OK, 0 rows affected (0.02 sec)

mysql>flush privileges;

 Query OK, 0 rows affected (0.02 sec)


Now the error gone.Try this when you are getting this error.


Better always try this below way to create MySQL user.

mysql>grant all privileges on *.* to 'root'@'localhost' identified by 'welcome';

Query OK, 0 rows affected (0.00 sec)

mysql>flush privileges;

Query OK, 0 rows affected (0.02 sec)



Monday, August 5, 2013

Percona Xtrabackup Tool


We all know to take backup MySQL databases through Mysqldump options but here I am going to explain another tool called Xtrabackup to backup our MySQL databases without any disturbance to your Server.



We can download Xtrabackup from this link : http://www.percona.com/downloads/XtraBackup/


Xtrabackup RPM  Installation:

In RPM there is 3 files:

1.Xtrabackup ( percona-xtrabackup-2.1.2-611.rhel6.x86_64.rpm )
2.Debug-files (percona-xtrabackup-debuginfo-2.1.2-611.rhel6.x86_64.rpm )
3.Test (percona-xtrabackup-test-2.1.2-611.rhel6.x86_64.rpm )

Download these above three files.

Installation :

Step 1:

$ rpm -Uvh percona-xtrabackup-2.1.2-611.rhel6.x86_64.rpm

Step 2:

$ rpm -Uvh  percona-xtrabackup-debuginfo-2.1.2-611.rhel6.x86_64.rpm

Step 3:

$ rpm -Uvh  percona-xtrabackup-test-2.1.2-611.rhel6.x86_64.rpm


After installed it check it whether it is installed or not by using,

$ yum list|grep percona

Here you can see the list for percona files was installed.


Take Backup Using Xtrabackup:

Add your data directory in my.cnf or my.ini file.

$ innobackupex --user=root --password=pa$$word --export  /nfsdrive

/nfsdrive  --> backup path.

Take compressed backup:

$ innobackupex --user=root --password=pa$$word --export --compress /nfsdrive

or

$ innobackupex --user=root --password=pa$$word --stream=tar ./ | gzip -c > /nfsdrive/backup.tar.gz


Restore Backup:

1.Stop mysql
2.Move your mysql data directory to other location.

3.Copy the backup from backup path to mysql data directory.
4.Start the server.

Now you can use your databases.

Note:

If you are using "--compress" option while taking backup.It will save in ".qp" format you need to extract it by download qpress and extract it by,

/path/qpress  -d  /backup_path/example.qp  /backup_path

More details : http://www.percona.com/software/percona-xtrabackup






Wednesday, July 10, 2013

Know about Locking Mechanism in MySQL Storage Engines



We all  know MySQL has different types of locking for different storage engines.It will vary depends upon the storage engines which you are using.Here I have explained how the locking mechanism will work.


There are three types of locking in MySQL,

1.Table level locking,
2.Row level locking and 
3.Page level locking.

 Table level locking:

Example1:Assume that there is a house with 5 rooms(1,2,3,4,5) and it is having 3 floors(x,y,z) and each houses are having only one  main door and each floor is having separate door to go inside the floor.And all the doors are automated when one person goes inside it will lock until the person is leaving from the house,floor and room.Here house is a table and rooms are rows and floors are pages.when a person is going inside the house name A and it will locked until the person open the door.

Likewise In MyISAM engine when one query is running on a particular table it will lock the entire table(likewise house A locked by a person) the remaining queries on the particular table it should be wait until the lock release it.This is called table level locking.

 If a query accesses the table it will lock the entire table and not allow access to the table from other queries.  The benefit of this is that it entirely eliminates deadlocking issues. The disadvantage is that, as mentioned, no other queries have access to the table while it is locked.  If you had a table with 20,000,000 rows and needed to modify one row, the entire table is inaccessible by other queries. The MyISAM and memory storage engine use table-level locking.

Row level locking:

Example2:In a single house we are having 5 rooms(Rows) when any one want to go inside the room 2 that person can access it if some other person want to use the remaining room they can also use the remaining rooms 1,3,4,5.here when one person is trying to access the same room which it was used by some person this new person have to wait. likewise in InnoDB engine when any query is doing a process in a table it will not lock the entire table.It will lock only the particular row in the table.It is called as Row level locking.when another query want to use the same row we will see the status as "table metadata lock" in process list.

Row level acquires a lock on as small an amount as a single row from a table. This will block the minimal amount of table content and allows for the most concurrency on a table without problems. InnoDB and Falcon both use row-level locking.

Page level locking:

Example3: In the house we have 3 floors(Pages) in each floor it is having five rooms.When a person want to go inside the 3rd floor 5th room that person can go inside the room but the 3rd floor will lock until the person leaving from the room.when any person is trying to go 3rd floor it was locked by the 1st person when the person comes out only other person can access it.like wise in BDB storage engine when any process happening in a particular row it locks the full Page until finish the process no any other query can access it until the lock release.Page is depends upon the size what you have given the defaults size is 16K.

Page-level locking is locking of a group of rows instead of a the entire table. The number of rows actually locked will vary based on a number of factors. Going back to our example of a 20,000,000-row table, lets assume a page-level lock is used.  If a page consists of 1,000 rows (this would vary depending on the size of the rows and the actual amount of memory allocated to a page), a lock would lock only a thousand rows.  Any of the other 19,999,000 rows could be used by other queries without interference. The BDB storage engine uses page-level locking.



Friday, June 14, 2013

KNOW ABOUT COMMON_SCHEMA in MySQL


The COMMON_SCHEMA provides a set of thoroughly documented MySQL views, stored procedures and functions which simplify otherwise complex shell and client scripting tasks.

The COMMON_SCHEMA is compatible with all MySQL 5.1 servers, and more specialized editions are available for servers running the InnoDB plugin and Percona server.

It is like a Information_schema but in Common_schema we can analyze our database index,routines,data and index length,table name,column name,etc... without select the databases.

This common_schema is in sql format so we can import and easily use it.

Download from here, http://common-schema.googlecode.com/files/common_schema-1.3.1.sql

How to use common_schema?

For Example:

1.View how many databases,tables,data_size,index_size,etc.. in one line query.

 mysql>select * from common_schema.data_size_per_schema;


 2.View auto increment table in your database.

mysql>select * from common_schema.auto_increment_columns ;

 

3.View the text character tables in your databses.

mysql>select * from common_schema.text_columns;




Likewise we can do a lot with common_schema.I think it will help you without selecting the databases we can get information about the entire databases in your server.

For more information: http://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/introduction.html