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