1. 程式人生 > >mysql的級聯複製和多源複製

mysql的級聯複製和多源複製

MySQL的複製: https://www.cnblogs.com/wxzhe/p/10051114.html

級聯複製的結構如圖

我們來設定基於filename和pos的級聯複製,並且接受mysql-utilities工具中mysqlreplicate的用法!

首先在MySQL官網下載mysql-utilities工具

[[email protected] src]# tar zxvf mysql-utilities-1.6.5.tar.gz
[[email protected] src]# cd mysql-utilities-1.6.5
[[email protected]
mysql-utilities-1.6.5
]# python setup.py install

#工具已經安裝完畢
[[email protected] ~]# mysqlreplicate --version
MySQL Utilities mysqlreplicate version 1.6.5
License type: GPLv

master:10.0.102.214          test3

slave  :10.0.102.204           test2

slave-2: 10.0.102.179          test1

第一步:首先要保證要備份的主從伺服器資料的一致,先搭建主從(master---slave)

[[email protected] ~]# mysqldump -uroot -p123456 --single-transaction --all-databases > all.sql         #這裡的提示,因為是測試環境因此沒有加--set-gtid-purged=OFF引數
mysqldump: [Warning
] Using a password on the command line interface can be insecure. Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you do not want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events. [[email protected] ~]# mysql -uroot -p123456 < all.sql #在從庫上匯入資料,這裡報錯了 mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty. [[email protected] ~]# mysql -uroot -p123456 #解決辦法 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.22-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select @@GLOBAL.GTID_EXECUTED; +------------------------------------------+ | @@GLOBAL.GTID_EXECUTED | +------------------------------------------+ | fc5f303f-f6c1-11e8-98bc-fa1dae125200:1-3 | +------------------------------------------+ 1 row in set (0.00 sec) mysql> reset master; Query OK, 0 rows affected (0.01 sec) mysql> select @@GLOBAL.GTID_EXECUTED; +------------------------+ | @@GLOBAL.GTID_EXECUTED | +------------------------+ | | +------------------------+ 1 row in set (0.00 sec) mysql> exit Bye [[email protected] ~]# mysql -uroot -p123456 < all.sql

資料已經保持一致:

在主上檢視filename和pos:

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| test3-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

在從或主伺服器上執行如下命令:【需要注意的是這個--rpl-user指定的使用者名稱和密碼需要在master上建立】

[[email protected] ~]# mysqlreplicate --master=root:123456@10.0.102.214 --slave=root:123456@10.0.102.204 --rpl-user=repl:123456 --master-log-file=test3-bin.000001 --master-log-pos=154 -vv
WARNING: Using a password on the command line interface can be insecure.
# master on 10.0.102.214: ... connected.
# slave on 10.0.102.204: ... connected.
# master id = 5
#  slave id = 3
# master uuid = 4687e05d-f37f-11e8-8fc7-fa336351fc00
#  slave uuid = f1983579-f6c4-11e8-8651-fa1dae125200
# Checking InnoDB statistics for type and version conflicts.
# Checking storage engines...
# Checking for binary logging on master...
# Setting up replication...
# Connecting slave to master...
# CHANGE MASTER TO MASTER_HOST = '10.0.102.214', MASTER_USER = 'repl', MASTER_PASSWORD = '123456', MASTER_PORT = 3306, MASTER_AUTO_POSITION=1
# Starting slave from master log file 'test3-bin.000001' using position 154...
# IO status: Waiting for master to send event
# IO thread running: Yes
# IO error: None
# SQL thread running: Yes
# SQL error: None
# ...done.

mysqlreplicate命令的用法如下:

[[email protected] log]# mysqlreplicate --help
MySQL Utilities mysqlreplicate version 1.6.5 
License type: GPLv2
Usage: mysqlreplicate [email protected]:3306 [email protected]: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.
  -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.
-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.

#這個--rpl-user指定的使用者名稱和密碼,不能識別在master上建立的使用“%”的標識,也就是master需要指定建立"repl"@"10.0.102.204"的使用者名稱,
"repl"@"%"好像就不可以。

從伺服器上一定要開啟二進位制日誌和log_slave_updates引數。因為若是不設定log_slave_updates,從伺服器只是開啟了二進位制日誌,但是卻沒有向二進位制日誌裡面寫入資料。

測試如下:從沒有開啟log_slave_updates!

#從伺服器二進位制日誌行數
[[email protected] mysql]# mysqlbinlog test2-bin.000001 | wc -l
20
#主上插入資料
mysql> insert into tb2 select null;
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tb2 select null;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tb2 select null;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tb2 select null;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tb2 select null;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tb2 select null;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tb2 select null;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0


#從伺服器資料同步過來
mysql> select * from tb2;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
|  7 |
+----+
7 rows in set (0.00 sec)

#但是二進位制行數沒有變,也就是二進位制日誌沒有增加
[[email protected] mysql]# mysqlbinlog test2-bin.000001 | wc -l
20
log_slave_updates引數測試

slave伺服器log_slave_updates,以當前的slave為主,做salve~slave-2的主從!

把當前slave的資料備份,在slave-2上恢復!

【過程略,參考上面的】

從伺服器上的server-id要設定為唯一的!【在叢集中是唯一的】

 mysqlreplicate --master=root:123456@10.0.102.204 --slave=root:123456@10.0.102.179 --rpl-user=repl:123456 --master-log-file=test2-bin.000002 --master-log-pos=1597 -vv

#repl-user:指定的使用者需要提前在對應的master上建立!

使用show slave status檢查是否成功!

mysql> desc test1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| a     | int(11) | NO   | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> create table test2(host varchar(30));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into test2 select @@hostname;
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from test2;
+-------+
| host  |
+-------+
| test3 |
+-------+
1 row in set (0.00 sec)

mysql> 

slave上檢視
mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| test2      |
+------------+
1 row in set (0.00 sec)

mysql> select * from test2;
+-------+
| host  |
+-------+
| test3 |
+-------+
1 row in set (0.00 sec)

slave-2上檢視資料
mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| test1      |
+------------+
1 row in set (0.00 sec)

mysql> select * from test2;
+-------+
| host  |
+-------+
| test3 |
+-------+
1 row in set (0.00 sec)
級聯複製測試

多源複製

一個slave連線多個master並接收所有來自master的變更,這種架構稱為多源架構。

注意與多主架構的區分:在多源架構中,變更來自多個master;而在多主拓補架構中,將每個master上的變更復制到其他所有的master,整個伺服器扮演單個master的角色。

若資料庫的狀態不一致,需要先備份,恢復資料使其達到一致的狀態!

master1   ---test1------10.0.102.179

master2   ---test2------10.0.102.204

slave   ---test3------10.0.102.214

多源複製過程與傳統非同步複製過程是一樣的!

建立複製賬戶,檢視複製日誌點位置!

#在兩個從上執行
mysql> grant all privileges on *.* to "repl"@"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show master status;  #檢視日誌點的位置

然後再從上開啟複製:

mysql> change master to master_host="10.0.102.204", master_user="root",master_password="123456",master_log_file="test2-bin.000003",master_log_pos=514 for channel "ch1";
ERROR 3077 (HY000): To have multiple channels, repository cannot be of type FILE; Please check the repository configuration and convert them to TABLE.
mysql>

#這裡報錯需要設定relay_log_info_repository 為table,把relay-log的資訊寫進表中!
master_info_repository =  TABLE #建議設定為table
relay_log_recovery = 1    #I/O thread crash safe
relay_log_info_repository = TABLE  # SQL thread crash safe
read_only = 1
super_read_only = on   #mysql5.7 加入的

設定之後重啟伺服器:
語句的後面加上了for channel!
mysql> change master to master_host="10.0.102.204", master_user="root",master_password="123456",master_log_file="test2-bin.000003",master_log_pos=514 for channel "ch1";
Query OK, 0 rows affected, 2 warnings (0.05 sec)

mysql> change master to master_host="10.0.102.204", master_user="root",master_password="123456",master_log_file="test2-bin.000001",master_log_pos=154 for channel "ch2";
Query OK, 0 rows affected, 2 warnings (0.03 sec)

#分別啟動
mysql> start slave for channel "ch1";
Query OK, 0 rows affected (0.04 sec)

mysql> start slave for channel "ch2";
Query OK, 0 rows affected (0.00 sec)
然後可以使用show slave status for channel 命令檢視對應的狀態!