本實驗中分別針對空庫、脫機、聯(lián)機三種方式,配置一主兩從的mysql標準異步復制。只做整服務器級別的復制,不考慮對個別庫表或使用過濾復制的情況。
實驗環(huán)境
[root@slave2 ~]# cat /etc/hosts
192.168.2.138 master
192.168.2.192 slave1
192.168.2.130 slave2
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.16 |
+-----------+
1 row in set (0.00 sec)
一、空庫
1.查看主庫二進制信息
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 155 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2.在主庫上建立復制用戶
mysql> create user 'repl'@'%' identified with mysql_native_password by 'wwwwww';
Query OK, 0 rows affected (0.03 sec)
mysql> grant replication client,replication slave on *.* to 'repl'@'%';
Query OK, 0 rows affected (0.04 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.在從庫創(chuàng)建主庫信息
mysql> stop slave;
mysql> change master to master_host='192.168.2.138', master_port=3306, master_user='repl', master_password='wwwwww', master_log_file='mysql-bin.000004', master_log_pos=155;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
剛才我們并沒有在從庫上建立repl用戶,但由于create user語句是在起始位置點后執(zhí)行的,因此可以正常復制到從庫,查詢mysql.user表即可確認。
sql> select * from mysql.user where user='repl'\G
二、脫機
如果數(shù)據(jù)庫已經(jīng)存在應用數(shù)據(jù),但允許一個可接受的脫機時間窗口做復制,這種場景下常用的做法是先直接將主庫的數(shù)據(jù)目錄整體拷貝到從庫,再啟動復制。具體步驟如下。
1.在master節(jié)點創(chuàng)建測試庫和測試表
CREATE DATABASE test;
Query OK, 1 row affected (0.04 sec)
mysql> USE test;
Database changed
mysql> CREATE TABLE t(id int(10));
Query OK, 0 rows affected (0.09 sec)
mysql> INSERT INTO t VALUES (111);
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO t VALUES (222);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO t VALUES (333);
Query OK, 1 row affected (0.00 sec)
2.在主庫創(chuàng)建復制用戶
mysql> create user 'repl'@'%' identified with mysql_native_password by 'wwwwww';
Query OK, 0 rows affected (0.03 sec)
mysql> grant replication client,replication slave on *.* to 'repl'@'%';
Query OK, 0 rows affected (0.04 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.停止復制的所有實例,在master、slave1、slave2分別執(zhí)行
[root@master ~]# ln -s /usr/local/mysql/bin/mysqladmin /usr/bin/mysqladmin
[root@master ~]# mysqladmin -hlocalhost -uroot -pwwwwww shutdown
4.復制數(shù)據(jù)至slave1、slave2
[root@master data]# cd /data
[root@master data]# scp -r mysql/ slave1:/data/
[root@master data]# scp -r mysql/ slave2:/data/
5.在slave1、slave2從庫執(zhí)行命令,刪除auto.cnf文件
[root@slave1 mysql]# cd /data/mysql
[root@slave1 mysql]# rm -rf auto.cnf
[root@slave2 mysql]# cd /data/mysql
[root@slave2 mysql]# rm -rf auto.cnf
6.重啟實例,在三個節(jié)點都需要執(zhí)行
[root@master data]# service mysqld start
Starting MySQL.. SUCCESS!
7.在主庫查看二進制日志
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 155 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
8.在slave1、slave2從庫執(zhí)行命令
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql> change master to master_host='192.168.2.138', master_port=3306, master_user='repl', master_password='wwwwww', master_log_file='mysql-bin.000005', master_log_pos=155;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
mysql> show slave status\G
9.在slave1、slave2從庫執(zhí)行命令查看庫和表是否同步過來
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t |
+----------------+
1 row in set (0.00 sec)
mysql> select * from t;
+------+
| id |
+------+
| 111 |
| 222 |
| 333 |
+------+
3 rows in set (0.00 sec)
三、mysqldump聯(lián)機
脫機建立復制的需求太過理想化,大多數(shù)情況下,復制是被要求在不影響線上業(yè)務的情況下,聯(lián)機創(chuàng)建的,而且還要求對線上庫的影響越小越好。例如,復制過程化中對主庫加鎖會影響對主庫的訪問,因此通常是不被允許的。這種場景下有兩種備選的復制方案:使用mysqldump程序或使用如XtraBackup的第三方工具。這兩種方案有各自的適用場合。使用mysqldump聯(lián)機建立復制的過程如下。
1.在主庫創(chuàng)建測試的數(shù)據(jù)庫和表
mysql> CREATE DATABASE test;
Query OK, 1 row affected (0.04 sec)
mysql> use test;
Database changed
mysql> CREATE TABLE t(id int(10));
Query OK, 0 rows affected (0.10 sec)
mysql> INSERT INTO t VALUES(111);
Query OK, 1 row affected (0.09 sec)
mysql> INSERT INTO t VALUES(222);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO t VALUES(333);
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO t VALUES(444);
Query OK, 1 row affected (0.00 sec)
2.在主庫創(chuàng)建復制用戶
mysql> create user 'repl'@'%' identified with mysql_native_password by 'wwwwww';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication client,replication slave on *.* to 'repl'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
3.在slave1、slave2從庫創(chuàng)建主庫信息
mysql> change master to master_host='192.168.2.138', master_port=3306, master_user='repl', master_password='wwwwww';
Query OK, 0 rows affected, 2 warnings (0.04 sec)
4.在slave1、slave2從庫使用mysqldump命令復制數(shù)據(jù)
[root@slave2 ~]# mysqldump --single-transaction --all-databases --master-data=1 --host=192.168.2.138 --user=root --password=wwwwww --apply-slave-statements | mysql -uroot -pwwwwww -hlocalhost
mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
參數(shù)說明
–single-transaction參數(shù)可以對Innodb表執(zhí)行非鎖定導出。此選項將事務隔離模式設置為REPEATABLE READ,并在轉(zhuǎn)儲數(shù)據(jù)之前向服務器發(fā)送START TRANSACTION SQL語句。它僅適用于Innodb等事務表,因為它會在發(fā)出START TRANSACTION時轉(zhuǎn)儲數(shù)據(jù)庫的一致狀態(tài),而不會阻塞任何應用程序。因此這里假定:1. 所有的應用數(shù)據(jù)表都使用Innodb引擎。2. 所有系統(tǒng)表數(shù)據(jù)在備份過程中不會發(fā)生變化。
–master-data參數(shù)會導致轉(zhuǎn)儲輸出包含類似 CHANGE MASTER TO MASTER_LOG_FILE=‘binlog.000004', MASTER_LOG_POS=1480; 的SQL語句,該語句指示主庫的二進制日志坐標(文件名和位置)。如果選項值為2,則CHANGE MASTER TO語句將寫為SQL注釋,因此僅提供信息,不會執(zhí)行。如果參數(shù)值為1,則該語句不會寫為注釋,并在重新加載轉(zhuǎn)儲文件時執(zhí)行。如果未指定選項值,則默認值為1。
–apply-slave-statements參數(shù)會在CHANGE MASTER TO語句之前添加STOP SLAVE語句,并在輸出結(jié)尾處添加START SLAVE語句,用來自動開啟復制。
通過管道操作符,導出導入一步進行,不需要中間落盤生成文件。
5.在從庫確認復制狀態(tài)
mysql> show slave status\G
6.在從庫查看庫和表是否復制成功
use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t;
+------+
| id |
+------+
| 111 |
| 222 |
| 333 |
| 444 |
| 555 |
+------+
5 rows in set (0.00 sec)
mysqldump方式的優(yōu)點是可以進行部分復制,如在配置文件中定義replicate-do-table=db1.*,則用這種方法可以只復制db1庫而忽略其它復制事件。缺點是由于mysqldump會生成主庫轉(zhuǎn)儲數(shù)據(jù)的SQL語句,實際是一種邏輯備份方式所以速度較慢,不適用于大庫。
四、XtraBackup聯(lián)機復制
聯(lián)機建立復制的另一種可選方案是使用XtraBackup。XtraBackup是Percona公司的開源項目,用以實現(xiàn)類似Innodb官方的熱備份工具InnoDB Hot Backup的功能,它支持在線熱備份,備份時不影響數(shù)據(jù)讀寫。到目前為止,最新的版本為Percona XtraBackup 8.0.6,可以從https://www.percona.com/downloads/下載安裝包。XtraBackup有很多功能和優(yōu)點,例如支持全備、增量備份、部分備份;支持壓縮備份;備份不影響數(shù)據(jù)讀寫、事務等,但是也有缺陷不足:例如不支持脫機備份、不支持直接備份到磁帶設備、不支持Cloud Back,MyISAM的備份也會阻塞。不過這些小瑕疵不影響XtraBackup成為一款流行的MySQL備份工具。另外,注意XtraBackup只支持Linux平臺,不支持Windows平臺。下面演示用XtraBackup聯(lián)機搭建主從復制的過程,主庫已經(jīng)建立了用于執(zhí)行復制的用戶repl。
在主庫創(chuàng)建復制用戶
mysql> create user 'repl'@'%' identified with mysql_native_password by 'wwwwww';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication client,replication slave on *.* to 'repl'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
1.在主庫和從庫都安裝XtraBackupv
[root@master ~]# yum -y install libev
[root@master home]# yum localinstall percona-xtrabackup-80-8.0.6-1.el7.x86_64.rpm -y
2.配置主庫到從庫的SSH免密碼連接
[root@master home]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:GBLbrw17UVck8RyCa/fbYyLkSNZIRc5p+jPQmpkD+bI root@master
The key's randomart image is:
+---[RSA 2048]----+
| . .o+o+ |
| + +..* . |
| o o o*. o |
| . +.o*.. |
| ooS+oo . |
| =o=Bo . |
| o.=B++ o |
| .o..oo..o.|
| E . o .|
+----[SHA256]-----+
[root@master home]# ssh-copy-id 192.168.2.138
[root@master home]# ssh-copy-id 192.168.2.192
[root@master home]# ssh-copy-id 192.168.2.130
3.停止從庫,并刪除從庫里面的數(shù)據(jù)
[root@slave1 home]# service mysql stop
[root@slave2 home]# service mysql stop
[root@slave1 home]# rm -rf /data/mysql/*
[root@slave2 home]# rm -rf /data/mysql/*
4.備份數(shù)據(jù)并傳輸
[root@master tmp]# xtrabackup -uroot -pwwwwww --socket=/data/mysql/mysql.sock --no-lock --backup --compress --stream=xbstream --parallel=4 --target-dir=./ | ssh root@192.168.2.192 "xbstream -x -C /data/mysql/ --decompress"
執(zhí)行過程中報錯,
190606 01:21:47 >> log scanned up to (19597291)
190606 01:21:47 Selecting LSN and binary log position from p_s.log_status
Error: failed to fetch query result SELECT server_uuid, local, replication, storage_engines FROM performance_schema.log_status: Access denied; you need (at least one of) the BACKUP_ADMIN privilege(s) for this operation
mysql> grant BACKUP_ADMIN on *.* to 'root'@'%';
Query OK, 0 rows affected (0.01 sec)
行如下命令,刪除192.168.2.192:/data/mysql/*的內(nèi)容,再次執(zhí)行命令,發(fā)現(xiàn)已經(jīng)正確了。成功執(zhí)行如下所示:
這條命令連接主庫,進行并行壓縮流式備份,同時將備份通過管道操作符傳輸?shù)綇膸?,并直接解壓縮到從庫的數(shù)據(jù)目錄。所有操作一條命令完成,不需要中間落盤生成文件。
5.在從庫恢復備份
[root@slave1 /]# xtrabackup --prepare --target-dir=/data/mysql
[root@slave2 /]# xtrabackup --prepare --target-dir=/data/mysql
6.在從庫查看二進制bin-log日志
[root@slave1 mysql]# cat xtrabackup_binlog_info
mysql-bin.000008 155
[root@slave2 mysql]# cat xtrabackup_binlog_info
mysql-bin.000009 155
7.啟動從庫
[root@slave1 data]# service mysqld start
Starting MySQL... SUCCESS!
[root@slave2 data]# service mysqld start
Starting MySQL... SUCCESS!
8.創(chuàng)建主庫信息,其中的master_log_file和master_log_pos值來自第6步
mysql> change master to master_host='192.168.2.138', master_port=3306, master_user='repl', master_password='wwwwww', master_log_file='mysql-bin.000008', master_log_pos=155;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
9.在從庫測試數(shù)據(jù)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t;
+------+
| id |
+------+
| 111 |
| 222 |
| 333 |
| 444 |
| 555 |
+------+
5 rows in set (0.00 sec)
XtraBackup是物理復制,性能比mysqldump高的多,而且對主庫的影響極小,非常適用于從頭聯(lián)機創(chuàng)建高負載、大數(shù)據(jù)量、全實例從庫的場景。
到此這篇關于淺談MySQL8.0 異步復制的三種方式的文章就介紹到這了,更多相關MySQL8.0 異步復制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Mysql主從復制與讀寫分離圖文詳解
- MySQL 復制表的方法
- MySQL 8.0.23中復制架構(gòu)從節(jié)點自動故障轉(zhuǎn)移的問題
- MYSQL數(shù)據(jù)庫GTID實現(xiàn)主從復制實現(xiàn)(超級方便)
- MySql主從復制實現(xiàn)原理及配置
- 淺析MySQL的WriteSet并行復制
- MySQL主從復制原理以及需要注意的地方
- mysql 如何動態(tài)修改復制過濾器
- 淺析MySQL并行復制
- mysql 主從復制如何跳過報錯
- MySQL 4種常用的主從復制架構(gòu)
- 磁盤寫滿導致MySQL復制失敗的解決方案