全部博文(535)
分类: LINUX
2011-05-03 11:57:12
Maatkit是非常不错的mysql管理工具,网络评价非常不错。包含以下主要工具:
1、mk-table-checksum 检查主从表是否一致的有效工具
2、mk-table-sync 有效保证表一致的工具,不必重载从表而能够保证一致。
3、mk-visual-explain exlpain解释工具
4、mk-heartbeat 主从同步的监视工具,能够给出从落后于主多少
5、mk-parallel-dump 多线程的mysqldump工具
6、mk-parallel-restore 多线程的表回复工具
7、mk-query-profiler 查询检测分析工具
8、mk-deadlock-logger 死锁的记录工具,支持innodb
9、mk-duplicate-key-checker key侦测工具
10、mk-show-grants 权限管理显示工具
11、mk-slave-restart slave的检测和重启工具
12、mk-slave-delay slave delay replication 的工具
13、mk-slave-prefetch This tool implements Paul Tuckfield’s famous “oracle” algorithm to read ahead of the slave SQL thread in the relay logs, rewriting queries as SELECT and executing them to warm the slave’s caches. This can help an I/O-bound slave SQL thread run faster under some conditions, because it doesn’t have to wait for as much I/O to complete. 这个看起来非常不错。
Maatkit是非常不错的mysql管理工具,网络评价非常不错。包含以下主要工具:
1、mk-table-checksum 检查主从表是否一致的有效工具
2、mk-table-sync 有效保证表一致的工具,不必重载从表而能够保证一致。
3、mk-visual-explain exlpain解释工具
4、mk-heartbeat 主从同步的监视工具,能够给出从落后于主多少
5、mk-parallel-dump 多线程的mysqldump工具
6、mk-parallel-restore 多线程的表回复工具
7、mk-query-profiler 查询检测分析工具
8、mk-deadlock-logger 死锁的记录工具,支持innodb
tar zxvf maatkit-2325.tar.gz
cd maatkit-2325
perl Makefile.PL
make install
mk-table-checksum – 检查数据库复制模式里,master和slave的表是否一致,安装方法可以参考这里。
mysql在5.1之前,其replication都是采用的STATEMENT模式,对表的数据是否一致要求并不严格,所以对数据一致性要求比较严格的应用,定期检查数据一致性是很有必要的,mk-table-checksum 是一个很不错的检查工具。
例如:
我要检查master=192.168.0.1,slave=192.168.0.2的两台mysql的test库里的test表是否一致,命令如下:
mk-table-checksum h=192.168.0.1,u=sg,p=sg109504 h=192.168.0.2,u=sg,p=xxxx -d test -t test |
解释下输出的意思:
DATABASE:数据库名
TABLE:表名
CHUNK:checksum时的近似数值
HOST:MYSQL的地址
ENGINE:表引擎
COUNT:表的行数
CHECKSUM:校验值
TIME:所用时间
WAIT:等待时间
STAT:MASTER_POS_WAIT()返回值
LAG:slave的延时时间
这里需要顺带介绍下mk-table-checksum的过滤工具mk-checksum-filter,例如我只想知道上面的例子中,test库中哪些表不是一致的。只需要加一个管道符。
mk-table-checksum h=10.0.0.156,u=sg,p=sg109504 h=10.0.0.159,u=sg,p=sg109504 -d test -r|mk-checksum-filter |
需要提醒一下,在使用mk-table-checksum进行操作的时候,会对表加一个表级锁,所以一般这类检查最好是在业务比较闲的时候进行。下面给
出这2个工具的帮助。
mk-table-checksum --help |
原文来自:
mk-table-sync?是maatkit里的一个同步主从数据库的利器,执行同步过程中,会同步 DELETE,REPLACE,INSERT,UPDATE语句,mk-table-sync?把包含前面几个的语句都执行一遍,举个例子来说,主从库上 都有:a表,主库上的数据如下:
id name
1 aa
2 bb
3 cc
4 dd
5 ee
6 ff
从库上的数据如下:
id name
1 aa
2 bb
3 hh
4 gg
5 ee
那么mk-table-sync会执行3条语句,同步"6 ff"添加到从库,更新"3 cc","4 gg"到从库,一共3条语句,而不是我们看到的从库只比主库少1条数据,其实同步过程中,执行了3条;
执行同步操作一般:
mk-table-sync --charset=utf8 --execute --print --no-check-slave -d test -t a \
h=localhost,u='root',p='123456' \
h=192.168.0.24,u='root',p='123456'
注意事项:
1.记得加上--charset选项,否则会造成从库乱码;
2.我们如果想要看下到底哪些数据不同步,可以这样做:
mk-table-sync --charset=utf8 --print --no-check-slave -d test -t a \
h='127.0.0.1',u='root',p='123456' \
h='192.168.0.24',u='root',p='123456'>result
3.如果有好几个从库的话,建议分开同步,除非几个从库的checksum一样;因为每个从库同步的步调不一定都一样,如果几个从库一块同步的话,很容易造成主键冲突,导致主从同步中断,举个例子来说,建议:
mk-table-sync --charset=utf8 --execute --print --no-check-slave -d test -t a \
h='127.0.0.1',u='root',p='123456' \
h='192.168.0.24',u
不建议:
mk-parallel-restore - Load files into MySQL in parallel.
上一篇介绍了mk-parallel-dump并行备份,会把mysql的按照database模式,每个表都生成一个单独的备份文件,并统一在你指定的目录。mk-deadlock-logger - 查看mysql的死锁信息。安装方法可以参考。
有2种方式可以查看死锁信息。第一种方法是直接打印出来:
$ mk-deadlock-logger --source u=sg,p=xxxx,h=localhost --print
server ts thread txn_id txn_time user hostname ip db tbl idx lock_type lock_mode wait_hold victim query
localhost 2007-03-08T20:34:22 81 21309 29 baron localhost test c GEN_CLUST_INDEX RECORD X w 1 select * from c for update
localhost 2007-03-08T20:34:22 83 21310 19 baron localhost test a GEN_CLUST_INDEX RECORD X w 0 select * from a for update
OK,很简单的命令,查看最后出现的死锁的信息。当然,你也可以指定想看的信息:
$ mk-deadlock-logger --source u=sg,p=xxxx,h=localhost --print -C ts,user,hostname,db,tbl,idx
ts user hostname db tbl idx
2007-03-08T20:34:22 baron localhost test c GEN_CLUST_INDEX
2007-03-08T20:34:22 baron localhost test a GEN_CLUST_INDEX
第二种方法是把信息储存到指定的表内。首先创建这个表:
mysql> use test;
Database changed
mysql> CREATE TABLE deadlocks (
-> server char(20) NOT NULL,
-> ts datetime NOT NULL,
-> thread int unsigned NOT NULL,
-> txn_id bigint unsigned NOT NULL,
-> txn_time smallint unsigned NOT NULL,
-> user char(16) NOT NULL,
-> hostname char(20) NOT NULL,
-> ip char(15) NOT NULL, -- alternatively, ip int unsigned NOT NULL
-> db char(64) NOT NULL,
-> tbl char(64) NOT NULL,
-> idx char(64) NOT NULL,
-> lock_type char(16) NOT NULL,
-> lock_mode char(1) NOT NULL,
-> wait_hold char(1) NOT NULL,
-> victim tinyint unsigned NOT NULL,
-> query text NOT NULL,
-> PRIMARY KEY (server,ts,thread)
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.00 sec)
mk-deadlock-logger --source u=sg,p=xxxx,h=localhost --dest D=test,\
t=deadlocks --dest D=test,t=deadlocks --daemonize -m 4h -i 30s
OK,这条语句就是,把死锁信息存入deadlocks表,并在4小时内每30秒检查1次。
mysql5.0以后,出现死锁的情况是越来越少了,如果不是innodb引擎。出现死锁的情况,特别的少见。
OK,还有其他的一些参数,可以看下帮助文件。
mk-deadlock-logger --help
mk-deadlock-logger extracts and saves information about the most recent deadlock
in a MySQL server. You need to specify whether to print the output or save it
in a database. For more details, please use the --help option, or try 'perldoc
mk-deadlock-logger' for complete documentation.
Usage: mk-deadlock-logger --source DSN
Options:
--askpass Prompt for a password when connecting to MySQL
--[no]collapse -c Collapse whitespace in queries to a single space
--columns -C Output only this comma-separated list of columns
--daemonize Fork and run in the background; POSIX OSes only
--dest -d DSN for where to store deadlocks
--help Show this help message
--interval -i How often to check for deadlocks (default 0s). Optional
suffix s=seconds, m=minutes, h=hours, d=days; if no suffix,
s is used.
--numip -n Express IP addresses as integers
--print -p Print results on standard output
--setvars Set these MySQL variables (default wait_timeout=10000)
--source -s DSN to check for deadlocks; required
--tab -t Print tab-separated columns, instead of aligned
--time -m How long to run before exiting. Optional suffix s=seconds,
m=minutes, h=hours, d=days; if no suffix, s is used.
--version Output version information and exit
Specify at least one of --print or --dest.
DSN values in --dest default to values from --source.
DSN syntax is key=value[,key=value...] Allowable DSN keys:
KEY COPY MEANING
=== ==== =============================================
A yes Default character set
D yes Database to use
F yes Only read default options from the given file
P yes Port number to use for connection
S yes Socket file to use for connection
h yes Connect to host
p yes Password to use when connecting
t yes Table in which to store deadlock information
u yes User for login if not current user
If the DSN is a bareword, the word is treated as the 'h' key.
Options and values after processing arguments:
--askpass FALSE
--collapse FALSE
--columns (No value)
--daemonize FALSE
--dest (No value)
--help TRUE
--interval 0
--numip FALSE
--print FALSE
--setvars wait_timeout=10000
--source (No value)
--tab FALSE
--time (No value)
--version FALSE
article/y2009/211_mysql%E7%AE%A1%E7%90%86%E5%BF%85%E5%A4%87%E5%B7%A5%E5%85%B7maatkit%E8%AF%A6%E8%A7%A3%E4
Mysql管理工具Maatkit详解之二(mk-archiver)
mk-archiver - 删除时按行备份,支持备份到异地库。安装方法可以参考。
mysql在删除数据的时候,是无法rollback(innodb引擎除外)的,如果你删错了数据,那么你就只能进行恢复了,这显然是很耗成本的操作。
当然你也可以在执行delete的之前,用mysqldump进行备份,或者你在删除之前,先执行load data 或者 insert into as select,至少需要执行2次命令,并且不支持备份到异地库。
那么,下面这个工具可能是你需要的。
利用mk-archiver工具,可以在删除mysql表数据的时候,把需要删除的数据备份。
备份的方式有2种,其一是备份到文件,就像load data一样。其二是备份到另外1张结构相同的表,支持备份到异地库。
mysql> select * from test;
+------+------+
| uid | note |
+------+------+
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
| 5 | NULL |
| 6 | NULL |
| 7 | NULL |
| 8 | NULL |
| 9 | NULL |
+------+------+
9 rows in set (0.00 sec)
mk-archiver --source h=localhost,u=sg,p='xxxx',D=test,t=test --file '/u01/%Y-%m-%d-%D.%t' --where 'uid<5'
Cannot find an ascendable index in table at /usr/bin/mk-archiver line 1262.
需要给这个表加一个索引。
mysql> create index uid on test(uid);
Current database: test
Query OK, 9 rows affected (0.00 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql> \! mk-archiver --source h=localhost,u=sg,p='xxxx',D=test,t=test --file '/u01/%Y-%m-%d-%D.%t' --where 'uid<5'
mysql> select * from test;
Current database: test
+------+------+
| uid | note |
+------+------+
| 5 | NULL |
| 6 | NULL |
| 7 | NULL |
| 8 | NULL |
| 9 | NULL |
+------+------+
5 rows in set (0.00 sec)
OK,已经被删除了。被删除的数据在指定的文件里
mysql> \! cat /u01/2008-07-31-test.test
1 \N
2 \N
3 \N
4 \N
想恢复回来,很简单。
mysql> LOAD DATA LOCAL INFILE '/u01/2008-07-31-test.test' into table test;
Current database: test
Query OK, 4 rows affected (0.01 sec)
Records: 4 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from test;
+------+------+
| uid | note |
+------+------+
| 4 | NULL |
| 3 | NULL |
| 2 | NULL |
| 1 | NULL |
| 5 | NULL |
| 6 | NULL |
| 7 | NULL |
| 8 | NULL |
| 9 | NULL |
+------+------+
9 rows in set (0.00 sec)
当然,也可以把数据备份到另外一个相同结构的表,这样需要先把备份表建立,支持备份到异地库。
mysql> create table test2 select * from test where 1=2;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test2;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| uid | int(11) | YES | | NULL | |
| note | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from test2;
Empty set (0.00 sec)
mysql> \! mk-archiver --source h=localhost,u=sg,p='xxxx',D=test,t=test --dest h=localhost,u=sg,p='xxxx',D=test,t=test2 --where 'uid<5'
mysql> select * from test2;
+------+------+
| uid | note |
+------+------+
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
+------+------+
4 rows in set (0.00 sec)
OK,还有其他的一些参数,可以看下帮助文件。
mk-archiver --help
mk-archiver nibbles records from a MySQL table. The --source and --dest
arguments use DSN syntax; if COPY is yes, --dest defaults to the key's value
from --source. For more details, please use the --help option, or try 'perldoc
mk-archiver' for complete documentation.
Usage: mk-archiver --source DSN --where WHERE
Options:
--analyze -Z Run ANALYZE TABLE afterwards on --source and/or --dest
--ascendfirst Ascend only first column of index
--askpass Prompt for password for connections
--buffer -b Buffer output to --file and flush at commit
--bulkdel Delete each chunk with a single statement (implies
--commit-each)
--bulkins Insert each chunk with LOAD DATA INFILE (implies --bulkdel
--commit-each)
--charset -A Default character set
--[no]chkcols -C Ensure --source and --dest have same columns (default)
--columns -c Comma-separated list of columns to archive
--commit-each Commit each set of fetched and archived rows (disables -z)
--delayedins Add the DELAYED modifier to INSERT statements
--dest -d Table to archive to
--file -f File to archive to, with DATE_FORMAT()-like formatting
--forupdate Adds the FOR UPDATE modifier to SELECT statements
--header -h Print column header at top of --file
--help Show this help message
--hpselect Adds the HIGH_PRIORITY modifier to SELECT statements
--ignore -i Use IGNORE for INSERT statements
--limit -l Number of rows to fetch and archive per statement (default
1)
--local -L Do not write OPTIMIZE or ANALYZE queries to binlog
--lpdel Add the LOW_PRIORITY modifier to DELETE statements
--lpins Add the LOW_PRIORITY modifier to INSERT statements
--noascend Do not use ascending index optimization
--nodelete Do not delete archived rows
--optimize -O Run OPTIMIZE TABLE afterwards on --source and/or --dest
--pkonly -k Primary key columns only
--plugin Perl module name to use as a generic plugin
--progress -P Print progress information every X rows
--purge -p Purge, not archive; allows to omit --file and --dest
--quickdel Add the QUICK modifier to DELETE statements
--quiet Do not print any output, such as for --statistics
--replace -r Use REPLACE instead of INSERT statements
--retries -R Number of retries per timeout or deadlock (default 1)
--[no]safeautoinc Do not archive row with max AUTO_INCREMENT (default)
--sentinel -S Sentinel file; default /tmp/mk-archiver-sentinel
--setvars Set these MySQL variables (default wait_timeout=10000)
--sharelock Adds LOCK IN SHARE MODE to SELECT statements
--skipfkchk -K Turn off foreign key checks
--sleep -e Sleep time between fetches
--source -s Table to archive from (required)
--statistics Collect and print timing statistics
--stop Stop running instances by creating the sentinel file
--test -t Test: print queries and exit without doing anything
--time -m Time to run before exiting. Optional suffix s=seconds,
m=minutes, h=hours, d=days; if no suffix, s is used.
--txnsize -z Number of rows per transaction; disable with 0; default 1)
--version Output version information and exit
--where -W WHERE clause to limit which rows to archive (required)
--whyquit -q Print reason for exiting unless rows exhausted
--ignore and --replace are mutually exclusive.
--txnsize and --commit-each are mutually exclusive.
--lpins and --delayedins are mutually exclusive.
--sharelock and --forupdate are mutually exclusive.
--analyze and --optimize are mutually exclusive.
Specify at least one of --dest, --file, or --purge.
DSN values in --dest default to values from --source if COPY is yes.
--noascend and --nodelete are mutually exclusive.