mysqlreplicate ?工具是在兩臺服務器間設置和啟動復制。用戶提供登錄從服務器信息和連接到主的信息。也可以指定一個數據庫用于測試復制。
該工具報告條件是當主和從的存儲引擎不一樣時。如果主和從的存儲引擎不同將產生告警信息。對于Innodb存儲引擎而言,必需完全一樣,Innodb的類型(built-in 或 InnoDB Plugin)需要一樣,同時主次版本號也要一樣,并啟用狀態。
默認情況下,該工具的警告問題在于下面的信息不匹配,存儲引擎設置、默認存儲引擎和Innodb存儲引擎。為了使用錯誤代替警告,使用?--pedantic 選項來要求主從存儲引擎必需一直。
為了查看存儲引擎和innodb值之間的差異,可以使用-vv選項,不管有沒使用--pedantic選項。
可以使用下面的策略啟動復制:
- Start from the current position (default)
從當前的主二進制日志和位置開始復制。該工具使用
SHOW MASTER STATUS語句來獲取這些信息。
- Start from the beginning
從主二進制日志中記錄的第一個事件開始復制。使用?
--start-from-beginning
?選項。 - Start from a binary log file
從指定的主二進制日志的第一個事件開始復制。使用?
--master-log-file 選項。
- Start from a specific event
從特定的事件坐標開始復制 (特定的二進制日志和位置)。使用?
--master-log-file
?和?--master-log-pos
?選項。
選項
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | MySQL Utilities mysqlreplicate version 1.5.3 License type: GPLv2 Usage: mysqlreplicate --master=root@localhost:3306 --slave=root@localhost:3310 --rpl-user=rpl:passwd mysqlreplicate - establish replication with a master Options: ??--version???????????? show program's version number and exit ??--help????????????????display a help message and exit ??--license???????????? display program's license and exit ??--master=MASTER?????? connection information for master server in the form: ????????????????????????<user>[:<password>]@<host>[:<port>][:<socket>] or ????????????????????????<login-path>[:<port>][:<socket>] or <config- ????????????????????????path>[<[group]>]. ??--slave=SLAVE???????? connection information for slave server in the form: ????????????????????????<user>[:<password>]@<host>[:<port>][:<socket>] or ????????????????????????<login-path>[:<port>][:<socket>] or <config- ????????????????????????path>[<[group]>]. ??--rpl-user=RPL_USER?? the user and password for the replication user ????????????????????????requirement, in the form: <user>[:<password>] or ????????????????????????<login-path>. E.g. rpl:passwd ??-p, --pedantic????????fail if storage engines differ among master and slave. ??--test-db=TEST_DB???? database name to use in testing replication setup ????????????????????????(optional)如果沒有給出這個選項,不進行任何測試,只進行錯誤檢查。 ??--master-log-file=MASTER_LOG_FILE ????????????????????????use this master log file to initiate the slave. ??--master-log-pos=MASTER_LOG_POS ????????????????????????use this position in the master log file to initiate ????????????????????????the slave.與--master-log-file選項同時使用有效。 ??-b, --start-from-beginning ????????????????????????start replication from the first event recorded in the ????????????????????????binary logging of the master. Not valid with --master- ????????????????????????log-file or --master-log-pos.?與--master-log-file 和 ????????????????????????--master-log-pos 同時使用才有效。 ??--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. ??-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. |
注意事項
登錄主服務器的用戶必須具有對訪問數據庫的授權權限和創建賬號的權限。也就是WITH GRANT OPTION 權限。
主和從的server ID必須非零和唯一的。如果為0或相同產生錯誤報告。
IP地址和主機名混合使用不推薦。涉及到反向解析的問題。
MySQL客戶端工具的路徑需要包含在PATH環境變量中,以便使用login-paths驗證機制。允許使用my_print_defaults 來從登陸配置文件(.mylogin.cnf)讀取login-path值。
實例
在同一臺服務器上使用相同的默認設置不同端口的兩個實例的復制,命令如下:
1 2 3 4 5 6 7 | shell> mysqlreplicate --master=root@localhost:3306 \ ??????????--slave=root@localhost:3307 --rpl-user=rpl:rpl # master on localhost: ... connected. # slave on localhost: ... connected. # Checking for binary logging on master... # Setting up replication... # ...done. |
使用?--pedantic選項來確保主和從復制成功,當且僅當兩個服務器具有相同的存儲引擎,相同的默認存儲引擎和相同的InnoDB存儲引擎。如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | shell> mysqlreplicate --master=root@localhost:3306 \ ??????????--slave=root@localhost:3307 --rpl-user=rpl:rpl -vv --pedantic # master on localhost: ... connected. # slave on localhost: ... connected. # master id = 2 #??slave id = 99 # Checking InnoDB statistics for type and version conflicts. # Checking storage engines... # Checking for binary logging on master... # Setting up replication... # Flushing tables on master with read lock... # Connecting slave to master... # CHANGE MASTER TO MASTER_HOST = [...omitted...] # Starting slave... # status: Waiting for master to send event # error: 0: # Unlocking tables on master... # ...done. |
從當前主的位置開始復制,默認的。如下所示:
1 2 3 4 5 6 7 | shell> mysqlreplicate --master=root@localhost:3306 \ ??????????--slave=root@localhost:3307 --rpl-user=rpl:rpl # master on localhost: ... connected. # slave on localhost: ... connected. # Checking for binary logging on master... # Setting up replication... # ...done. |
從主記錄的事件開始復制,如下所示:
1 2 3 4 5 6 7 8 | shell> mysqlreplicate --master=root@localhost:3306 \ ???? --slave=root@localhost:3307 --rpl-user=rpl:rpl \ ???? --start-from-beginning # master on localhost: ... connected. # slave on localhost: ... connected. # Checking for binary logging on master... # Setting up replication... # ...done. |
從特定的主二進制日志開始復制,如下所示:
1 2 3 4 5 6 7 8 | shell> mysqlreplicate --master=root@localhost:3306 \ ??????????--slave=root@localhost:3307 --rpl-user=rpl:rpl \ ??????????--master-log-file=my_log.000003 # master on localhost: ... connected. # slave on localhost: ... connected. # Checking for binary logging on master... # Setting up replication... # ...done. |
從特定的主二進制日志坐標開始復制(指定二進制文件和位置),如下所示:
1 2 3 4 5 6 7 8 | shell> mysqlreplicate --master=root@localhost:3306 \ ??????????--slave=root@localhost:3307 --rpl-user=rpl:rpl \ ??????????--master-log-file=my_log.000001 --master-log-pos=96 # master on localhost: ... connected. # slave on localhost: ... connected. # Checking for binary logging on master... # Setting up replication... # ...done. |
建議
在從的my.cnf文件中配置read_only=1來確保數據不被意外修改,只允許從主讀取事件。
使用?--pedantic 和 -vv 選項來避免不同的存儲引擎可能導致的問題。
權限
在主上需要對mysql數據庫具有SELECT 和 INSERT權限,同時還要有REPLICATION SLAVE, REPLICATION CLIENT 和 GRANT OPTION權限。
在從上需要有SUPER 權限。
對于復制用戶,?--rpl-user 選項使用的,要么自動創建要么指定已經存在的,需要具有?REPLICATION SLAVE 權