Shantanu's Blog

Database Consultant

September 24, 2009

 

Maatkit examples

If you want to check if the table exists on the second server, use --nocrc check. "count" check is disabled by default. If you want to check if the tables are the same based on the count of the records, use the second example.

mk-table-checksum

# mk-table-checksum h=localhost,u=root,P=3306 --databases myDB h=192.168.50.113,u=root,p=PassWd,P=3306 --databases myDB --nocrc | mk-checksum-filter
myDB.sometbl does not exist on slave 192.168.50.113:3306 at /usr/bin/mk-table-checksum line 4798.
myDB.othertbl does not exist on slave 192.168.50.113:3306 at /usr/bin/mk-table-checksum line 4798.
myDB.third does not exist on slave 192.168.50.113:3306 at /usr/bin/mk-table-checksum line 4798.

# mk-table-checksum h=localhost,u=root,P=3306 --databases myDB h=192.168.50.113,u=root,p=passWd,P=3306 --databases myDB --nocrc --count | mk-checksum-filter
DATABASE TABLE CHUNK HOST ENGINE COUNT CHECKSUM TIME WAIT STAT LAG
myDB tbla 0 192.168.50.113 InnoDB 0 NULL 0 0 NULL NULL
myDB tbla 0 localhost InnoDB 9 NULL 0 0 NULL NULL
myDB xyz 0 192.168.50.113 InnoDB 6250 NULL 0 0 NULL NULL
myDB xyz 0 localhost InnoDB 30 NULL 0 0 NULL NULL
myDB pqr does not exist on slave 192.168.50.113:3306 at /usr/bin/mk-table-checksum line 4798.
myDB pqr 0 192.168.50.113 MyISAM NULL NULL 0 0 NULL NULL
myDB pqr 0 localhost MyISAM 1 NULL 0 0 NULL NULL
myDB mnp 0 192.168.50.113 InnoDB 51 NULL 0 0 NULL NULL
myDB mnp 0 localhost InnoDB 52 NULL 0 0 NULL NULL

You can check all the databases against the other server using the shell script.
#/bin/sh
for each dbName in `mysqlshow`
do
mk-table-checksum h=localhost,u=root,P=3306 --databases $dbName h=192.168.100.101,u=root,p=PassWord,P=3306 --databases $dbName --no-crc --count | mk-checksum-filter
done

_____

mk-table-sync

On Localhost:
+------+---------------------+
| id | tran_time |
+------+---------------------+
| 1 | 2011-06-07 07:58:51 |
| 1 | 2011-06-07 17:38:51 |
+------+---------------------+

On 10.10.10.24:
+------+---------------------+
| id | tran_time |
+------+---------------------+
| 1 | 2011-06-07 07:58:51 |
+------+---------------------+
_____

When you will need to sync the localhost data with remote 24 server, you will need one insert statement. Whereas if you want to sync the remote 24 with the local, then in that case, one delete is required.

/usr/bin/mk-table-sync --print --no-check-slave h=localhost,u=shantanu,p=shantanu@123,P=3306 --databases test --tables tz1 h=10.10.10.24,u=shantanu,p=shantanu@123,P=3306 --databases test --tables tz1 | more
INSERT INTO `test`.`tz1`(`id`, `tran_time`) VALUES ('1', '2011-06-07 17:38:51')

/usr/bin/mk-table-sync --print --no-check-slave h=10.10.10.24,u=shantanu,p=shantanu@123,P=3306 --databases test --tables tz1 h=localhost,u=shantanu,p=shantanu@123,P=3306 --databases test --tables tz1 | more
DELETE FROM `test`.`tz1` WHERE `id`='1' AND `tran_time`='2011-06-07 17:38:51' LIMIT 1

* The print option will display the statements instead of actually executing it.
* Change the --print to --execute if you want to really sync the data.

Example command:

mk-table-sync --execute --charset utf8 --verbose --no-foreign-key-checks h=1.2.3.4,d=sync,t-uad_b_stop,u=myuser,p=mypass

_____

parallel-dump and restore

Dump all the tables from someDB database, excluding the tables starting with the name "mail" and "qb". For e.g. mail_backup, mail_head

$ time /usr/bin/mk-parallel-dump -h'localhost' --no-gzip --base-dir '/home/develop/pdump' --databases 'someDB' --ignore-tables-regex 'mail|qb'
--ignore-tables 'tbl1,tbl2,tbl3,tbl4' --socket '/tmp/mysql.sock'

We can restore all or any one table (for e.g. Customer) using the following statement.
$ /usr/bin/mk-parallel-restore -h'localhost' -u'root' --database 'shantanu' --tables 'Customer' '/home/develop/pdump'

You may need to change the ownership of the folder /pdump like this...
rm -rf /pdump/
mkdir /pdump/
chmod 777 /pdump/

# in order to restore a single table for e.g. ach_routing_info use the following command
# /usr/bin/mk-parallel-restore -h'localhost' -u'shantanu' -p'xyz' --no-foreign-key-checks --database 'customer' --tables 'ach_routing_info' '/pdump'
_____

Copy the entire database from one server to another
Master:
mkdir -p /home/shantanu/today`date +%Y%m%d`
mk-parallel-dump --base-dir /home/shantanu/today`date +%Y%m%d` --databases mumbaicentral
scp -r /home/shantanu/today`date +%Y%m%d`/default/ root@199.199.199.199:/home/shantanu/

Slave on 199.199.199.199:
mk-parallel-restore /home/shantanu/default/mumbaicentral/ --database mumbaicentral

# The binary log position is dumped to a file named 00_master_data.sql in the --base-dir.
_____

Install Maatkit

These are the 5 steps to download and install Maatkit

wget http://maatkit.googlecode.com/files/maatkit-7119.tar.gz
tar -zxf maatkit-7119.tar.gz
cd maatkit-7119
perl Makefile.PL
make install

Confirm that everything is working:
mk-show-grants

OR

wget http://maatkit.googlecode.com/files/maatkit-7540-1.noarch.rpm
yum localinstall --nogpgcheck maatkit-7540-1.noarch.rpm --skip-broken

_____

mk-loadavg

Load Avg Utility: There are times when you need to collect the server stats when the application is running too slow or doesn't work at all. You will need to look at process list, IO and VM stats, mysql processlist, etc. You can save these stats using a cron, but I need the data only when there is a trigger, for e.g. average load is more than 50%
loadavg utility from maatkit does exactly that.


1) Download the maatkit utility called load Average.
wget http://www.maatkit.org/get/mk-loadavg

2) Write a shell script that will collect all the relevant stats when the processor load is too high. for e.g.
#!/bin/bash
ps -eaf >> collected/today.txt 2>&1 &

3) Change the permission of this file to 777
chmod 777 collect-stats.sh

4) Call the program whenever there are more than 50 mysql threads running simultaneously.
perl mk-loadavg -uroot -pPassWd --watch "Status:status:Threads_running:>:50" --execute-command /home/shantanu/collect-stats.sh

http://www.maatkit.org/doc/mk-loadavg.html#OPTIONS

The server stats those are really needed to be looked at...
> statsFile.txt
ps -eaf >> statsFile.txt 2>&1 &
top -bn1 > statsFile.txt 2>&1 &
mysql -uroot -pPassWOrd -e 'show innodb status\G show full processlist\G' >> statsFile.txt 2>&1 &
vmstat 1 30 > statsFile.txt 2>&1 &
iostat -dx 1 30 > statsFile.txt 2>&1 &
mysqladmin -uroot -pPassWord ext > statsFile.txt 2>&1 &

_____

mk-kill

# mk-kill --match-command Sleep --print --no-only-oldest
# mk-kill --match-command Sleep --kill --no-only-oldest

Print, do not kill, queries running longer than 60s:
mk-kill --busy-time 60 --print --match-command Query

Check for sleeping processes and kill them all every 10s:
mk-kill --match-command Sleep --kill --no-only-oldest --interval 10

--match-command
Common Command values are:

Query
Sleep
Binlog Dump
Connect
Delayed insert
Execute
Fetch
Init DB
Kill
Prepare
Processlist
Quit
Reset stmt
Table Dump

--match-state --ignore-state
Common State values are:

Locked
login
copy to tmp table
Copying to tmp table
Copying to tmp table on disk
Creating tmp table
executing
Reading from net
Sending data
Sorting for order
Sorting result
Table lock
Updating

--match-info --ignore-info
The Info column of the processlist shows the query that is being executed or NULL if no query is being executed.

--all
If some ignore options are specified, then every connection except ignored connections are killed.

--idle-time
Kill connections that have been idle/sleeping for longer than this time. The queries must be in Command=Sleep status. This matches a query's Time value as reported by SHOW PROCESSLIST.

--ignore-command
Ignore queries whose Command matches this Perl regex.
See --match-command.
_____

# mysql -e"select sleep(100)" &
[1] 20148

# mysql -e"show processlist;"
+--------+------------+--------------------+--------+-------------+------+----------------------------------------------------------------+-------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+------------+--------------------+--------+-------------+------+----------------------------------------------------------------+-------------------+
| 125873 | root | localhost | NULL | Sleep | 43 | | NULL |
| 125895 | wuser | 110.10.10.183:57113 | mydb | Sleep | 76 | | NULL |
| 125932 | wuser | 110.10.10.82:57090 | this | Sleep | 61 | | NULL |
| 125934 | slave_user | 110.10.10.3:60019 | NULL | Binlog Dump | 54 | Has sent all binlog to slave; waiting for binlog to be updated | NULL |
| 125942 | wuser | 110.10.10.62:32913 | yourdb | Sleep | 47 | | NULL |
| 125963 | wcuser | 110.10.10.62:57956 | yourdb | Sleep | 28 | | NULL |
| 125968 | root | localhost | NULL | Query | 26 | executing | select sleep(100) |
| 125985 | wuser | 110.10.10.62:57976 | somedv | Sleep | 15 | | NULL |
| 125997 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+--------+------------+--------------------+--------+-------------+------+----------------------------------------------------------------+-------------------+

# mk-kill --match-state executing --match-command Query --ignore-info NULL --print
# 2011-01-11T02:00:22 KILL 125968 (Query 30 sec) select sleep(100)

You can add the command to the cron so that it will check the processlist every minute and log/ mail the killed queries as well!


# kill query running too long
* * * * * /usr/bin/mk-kill --print --daemonize --interval 5 --busy-time 2000 --ignore-info '(?i-smx:^insert|^update|^delete)' --match-info '(?i-xsm:select)' --log /var/log/mk-kill.log --execute-command '(echo "Subject: mk-kill query found on `hostname`"; tail -1 /var/log/mk-kill.log) | mail -s "mk-kill on `hostname`" shantanu.oak@gmail.com' --kill-query >> /home/shantanu/kill_success.txt 2>> /home/shantanu/kill_err.txt


delay slave by 10 minutes

mk-slave-delay --delay 1h --interval 59s --run-time 10m 10.10.10.10

Labels: ,


Comments: Post a Comment

<< Home

Archives

June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013   June 2013   July 2013   September 2013   October 2013   January 2014   March 2014   April 2014   May 2014   July 2014   August 2014   September 2014   October 2014   November 2014   December 2014   January 2015   February 2015   March 2015   April 2015   May 2015   June 2015   July 2015   August 2015   September 2015   January 2016   February 2016   March 2016   April 2016   May 2016   June 2016   July 2016   August 2016   September 2016   October 2016   November 2016   December 2016   January 2017   February 2017   April 2017   May 2017   June 2017   July 2017   August 2017   September 2017   October 2017   November 2017   December 2017   February 2018   March 2018   April 2018   May 2018   June 2018   July 2018   August 2018   September 2018   October 2018   November 2018   December 2018   January 2019   February 2019   March 2019   April 2019   May 2019   July 2019   August 2019   September 2019   October 2019   November 2019   December 2019   January 2020   February 2020   March 2020   April 2020   May 2020   July 2020   August 2020   September 2020   October 2020   December 2020   January 2021   April 2021   May 2021   July 2021   September 2021   March 2022   October 2022   November 2022   March 2023   April 2023   July 2023   September 2023   October 2023   November 2023   April 2024   May 2024   June 2024   August 2024   September 2024   October 2024   November 2024   December 2024  

This page is powered by Blogger. Isn't yours?