浅谈MySQL8.0 异步复制的三种方式
本实验中分别针对空库、脱机、联机三种方式,配置一主两从的mysql标准异步复制。只做整服务器级别的复制,不考虑对个别库表或使用过滤复制的情况。
实验环境
[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.在从库创建主库信息
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语句是在起始位置点后执行的,因此可以正常复制到从库,查询mysql.user表即可确认。
sql> 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
9.在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,可以从下载安装包。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.在主库和从库都安装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.停止从库,并删除从库里面的数据
[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/*的内容,再次执行命令,发现已经正确了。成功执行如下所示:
这条命令连接主库,进行并行压缩流式备份,同时将备份通过管道操作符传输到从库,并直接解压缩到从库的数据目录。所有操作一条命令完成,不需要中间落盘生成文件。
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高的多,而且对主库的影响极小,非常适用于从头联机创建高负载、大数据量、全实例从库的场景。
到此这篇关于浅谈mysql8.0 异步复制的三种方式的文章就介绍到这了,更多相关mysql8.0 异步复制内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!