hbhe0316
作者hbhe0316·2019-06-06 14:06
数据库管理员·IBM

mysql 8.0异步复制的三种方式

字数 10646阅读 5400评论 0赞 4

本实验中分别针对空库、脱机、联机三种方式,配置一主两从的mysql标准异步复制。只做整服务器级别的复制,不考虑对个别库表或使用过滤复制的情况。
1.实验环境

[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 |              |                  |                   |
  2. 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.在从库创建主库信息

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
  1. 刚才我们并没有在从库上建立repl用户,但由于create user语句是在起始位置点后执行的,因此可以正常复制到从库,查询mysql.user表即可确认。
mysql> select * from mysql.user where user='repl'\\G

二.脱机

    如果数据库已经存在应用数据,但允许一个可接受的脱机时间窗口做复制,这种场景下常用的做法是先直接将主库的数据目录整体拷贝到从库,再启动复制。具体步骤如下。

1.在master节点创建测试库和测试表

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.在主库创建复制用户

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分别执行

[root@master ~]# ln -s /usr/local/mysql/bin/mysqladmin /usr/bin/mysqladmin
[root@master ~]# mysqladmin -hlocalhost -uroot -pwwwwww shutdown

4.复制数据至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从库执行命令,删除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.重启实例,在三个节点都需要执行

[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从库执行命令

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
  1. 在slave1、slave2从库执行命令查看库和表是否同步过来
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联机
脱机建立复制的需求太过理想化,大多数情况下,复制是被要求在不影响线上业务的情况下,联机创建的,而且还要求对线上库的影响越小越好。例如,复制过程化中对主库加锁会影响对主库的访问,因此通常是不被允许的。这种场景下有两种备选的复制方案:使用mysqldump程序或使用如XtraBackup的第三方工具。这两种方案有各自的适用场合。使用mysqldump联机建立复制的过程如下。
1.在主库创建测试的数据库和表

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.在主库创建复制用户

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从库创建主库信息

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命令复制数据

[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.

参数说明
--single-transaction参数可以对Innodb表执行非锁定导出。此选项将事务隔离模式设置为REPEATABLE READ,并在转储数据之前向服务器发送START TRANSACTION SQL语句。它仅适用于Innodb等事务表,因为它会在发出START TRANSACTION时转储数据库的一致状态,而不会阻塞任何应用程序。因此这里假定:1. 所有的应用数据表都使用Innodb引擎。2. 所有系统表数据在备份过程中不会发生变化。
--master-data参数会导致转储输出包含类似 CHANGE MASTER TO MASTER_LOG_FILE='binlog.000004', MASTER_LOG_POS=1480; 的SQL语句,该语句指示主库的二进制日志坐标(文件名和位置)。如果选项值为2,则CHANGE MASTER TO语句将写为SQL注释,因此仅提供信息,不会执行。如果参数值为1,则该语句不会写为注释,并在重新加载转储文件时执行。如果未指定选项值,则默认值为1。
--apply-slave-statements参数会在CHANGE MASTER TO语句之前添加STOP SLAVE语句,并在输出结尾处添加START SLAVE语句,用来自动开启复制。
通过管道操作符,导出导入一步进行,不需要中间落盘生成文件。

5.在从库确认复制状态

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方式的优点是可以进行部分复制,如在配置文件中定义replicate-do-table=db1.*,则用这种方法可以只复制db1库而忽略其它复制事件。缺点是由于mysqldump会生成主库转储数据的SQL语句,实际是一种逻辑备份方式所以速度较慢,不适用于大库。

四、XtraBackup联机复制
联机建立复制的另一种可选方案是使用XtraBackup。XtraBackup是Percona公司的开源项目,用以实现类似Innodb官方的热备份工具InnoDB Hot Backup的功能,它支持在线热备份,备份时不影响数据读写。到目前为止,最新的版本为Percona XtraBackup 8.0.6,可以从https://www.percona.com/downloads/下载安装包。XtraBackup有很多功能和优点,例如支持全备、增量备份、部分备份;支持压缩备份;备份不影响数据读写、事务等,但是也有缺陷不足:例如不支持脱机备份、不支持直接备份到磁带设备、不支持Cloud Back,MyISAM的备份也会阻塞。不过这些小瑕疵不影响XtraBackup成为一款流行的MySQL备份工具。另外,注意XtraBackup只支持Linux平台,不支持Windows平台。下面演示用XtraBackup联机搭建主从复制的过程,主库已经建立了用于执行复制的用户repl。

在主库创建复制用户

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.在主库和从库都安装XtraBackup

[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.停止从库,并删除从库里面的数据

[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.备份数据并传输

[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"

执行过程中报错,
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/*的内容,再次执行命令,发现已经正确了。成功执行如下所示:
ltfh4jh6uml

ltfh4jh6uml

这条命令连接主库,进行并行压缩流式备份,同时将备份通过管道操作符传输到从库,并直接解压缩到从库的数据目录。所有操作一条命令完成,不需要中间落盘生成文件。

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.创建主库信息,其中的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.在从库测试数据

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高的多,而且对主库的影响极小,非常适用于从头联机创建高负载、大数据量、全实例从库的场景。

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

4

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广