如果要比較數據庫表結構就使用mysqldiff,並生成差異SQL語句
如果要比較數據庫主從一致使用pt-table-checksum,生成差異報告,在利用pt-table-sync 數據修復
mysqldbcompare是官方提供一個可以實現多庫或單庫比較數據一致(無法基於表級別使用)工具,對於表數據很大情況下,測試運行效率並不是很高,有時候會報異常超時等。
官方文檔:http://dev.mysql.com/doc/mysql-utilities/1.3/en/mysqldbcompare.html
工具下載地址:http://downloads.mysql.com/snapshots/pb/mysql-connector-utilities-fabric/mysql-utilities-1.4.0-labs-fabric.tar.gz
二、安裝
解壓安裝後,需要使用python命令進行安裝
cd mysql-utilities-1.4.0-labs-fabric
python setup.py install
安裝完成後,mysqldbcompare命令在/usr/local/bin下面
用法:
mysqldbcompare --server1=dlan:[email protected]:3307 --server2=dlan:[email protected]:3307 --run-all-test --changes-for=server1 --difftype=sql ms:ms
對於差異的展現能通過參數--difftype調控
① unified (default)
② context
③ differ
④ sql
參數選項
--version show program's version number and exit
--help 幫助信息
--license 版權信息
--server1=SERVER1 connection information for first server in the form:
<user>[:<password>]@<host>[:<port>][:<socket>] or
<login-path>[:<port>][:<socket>].
--server2=SERVER2 connection information for second server in the form:
<user>[:<password>]@<host>[:<port>][:<socket>] or
<login-path>[:<port>][:<socket>].
--character-set=CHARSET
設置客戶端字符集。默認是以'character_set_client'變量值.
-f FORMAT, --format=FORMAT
指定缺失或改變行的顯示格式,有grid (default), tab, csv,
or vertical
--skip-checksum-table
skip CHECKSUM TABLE step in data consistency check.
--skip-object-compare
skip object comparison step.
--skip-row-count skip row count step.
--skip-diff skip the object diff step.
--skip-data-check skip data consistency check.
--skip-table-options skip check of all table options (e.g., AUTO_INCREMENT,
ENGINE, CHARSET, etc.).
--width=WIDTH display width
-t, --run-all-tests 首次發現差異時不中止
-a, --all 檢測所有庫,1.4.0版本引入
-x EXCLUDE, --exclude=EXCLUDE
排除一個或多個特定的數據庫。1.4.0版本引入
exclude one or more databases from the operation using
either a specific name (e.g. db1), a LIKE pattern
(e.g. db%) or a REGEXP search pattern. To use a REGEXP
search pattern for all exclusions, you must also
specify the --regexp option. Repeat the --exclude
option for multiple exclusions.
-c, --compact compact output from a diff.
--disable-binary-logging
防止比較操作被寫入二進制日誌,如果二進制啓用
(SQL_LOG_BIN=1).需要SUPER權限.
Prevents compare operations from being written to the
binary log.
--span-key-size=SPAN_KEY_SIZE
changes the size of the key used for compare table
contents. A higher value can help to get more accurate
results comparing large databases, but may slow the
algorithm. Default value is 8.
--use-indexes=USE_INDEXES
for each table, indicate which index to use as if were
a primary key (each of his columns must not allow null
values).
-v, --verbose control how much information is displayed. e.g., -v =
verbose, -vv = more verbose, -vvv = debug
-q, --quiet turn off all messages for quiet execution.
-d DIFFTYPE, --difftype=DIFFTYPE
指定不同的顯示格式。: [unified|context|differ|sql]
(default: unified).
--changes-for=CHANGES_FOR
執行要顯示的轉換與其他服務器匹配。如, 要看到server1的對象定義
與server2相匹配,使用 --changes-for=server1。合法的值有
'server1' or 'server2'。默認'server1'。
--show-reverse produce a transformation report containing the SQL
statements to transform the object definitions
specified in reverse. For example if --changes-for is
set to server1, also generate the transformation for
server2. Note: the reverse changes are annotated and
marked as comments.
-G, --basic-regexp, --regexp
use 'REGEXP' operator to match pattern. Default is to
use 'LIKE'.
--ssl-ca=SSL_CA The path to a file that contains a list of trusted SSL
CAs.
--ssl-cert=SSL_CERT The name of the SSL certificate file to use for
establishing a secure connection.
--ssl-key=SSL_KEY The name of the SSL key file to use for establishing a
secure connection.