一、相关的知识点
MySQL的异步复制是MySQL自带的数据同步功能,在公司里面也是也就最为常见的。通俗的将,异步复制就是在主库写入binlog日志后即可成功返回客户端,无须等待binlog日志传递给从库的过程。一旦主库发生宕机,就有可能出现丢失数据的情况;
- Master服务器中需要开启二进制日志binlog,从服务器需要开启中继日志relay-log。
- 二进制日志binlog的主要功能是:记录令数据库内容产生改变的语句,如insert语句;二进制日志在备份还原的时候至关重要。
- 中继日志relay-log则是从服务器中开启,作用是从主服务器的二进制日志中复制,并在在从服务器本地执行一次,达到与主服务器内容一致的效果。
- 一般MySQL复制是放在内网中进行的,因为MySQL的同步并没有进行加密。而且相比较于在公网传输,在内网中丢包的概率较低,带宽也高。
传统的复制方式,也就是非gtid模式是基于binlog和position方式
在生产环境中,很少是单节点的环境,最起码的也是一个简单的主从复制环境,所以,下面就来测试搭建一个基于传统方式的MySQL 5.7主从复制环境;
下面的MySQL 5.7主从复制环境的搭建是基于之前MySQL 5.7单实例环境的前提下搭建的,关于单实例的安装详细可参考:安装MYSQL5.7单实例数据库
二、搭建MySQL 5.7 异步复制环境
A、实验环境:
主:10.10.1.10 server-id = 330610 gtid_mode = off
备:10.10.1.20 server-id = 330620 gtid_mode = off
B、必要条件:
1)server-id有且唯一
2)主库开启binlog功能 #建议从库也开启binlog,且开启log_slave_updates让从库与写binlog方便后期扩展
3)binlog_format = row
C、搭建步骤:
主库上的操作
1、创建一个主从复制帐号
root@localhost [(none)]>create user 'wjqrepl'@'10.10.1.%' identified by 'xxxxxx'; Query OK, 0 rows affected (0.01 sec) root@localhost [(none)]>grant replication slave on *.* to 'wjqrepl'@'10.10.1.%'; Query OK, 0 rows affected (0.00 sec) root@localhost [(none)]>flush privileges; Query OK, 0 rows affected (0.00 sec)
2、初始化数据
[root@wjq1 ~]# mysqldump -u root -pqcloud@2018 --single-transaction --master-data=2 -A > /tmp/10all.sql 注意:--master-data=2必须让备份出来的文件中记录备份这一时刻的binlog文件与position号,为搭建主从环境做准备 [root@wjq1 ~]# more /tmp/10all.sql ...... -- -- Position to start replication or point-in-time recovery from -- -- CHANGE MASTER TO MASTER_LOG_FILE='mysql3306.000008', MASTER_LOG_POS=320114; ......
3、将备份文件拷贝到备库
[root@wjq1 ~]# scp /tmp/10all.sql 10.10.1.20:/tmp/ root@10.10.1.20's password: 10all.sql 100% 7624KB 3.0MB/s 00:02
从库上的操作
4、恢复从主库传递过来的数据
[root@wjq2 ~]# mysql -u root -pqcloud@2018 < /tmp/10all.sql root@localhost [(none)]>show databases; +--------------------+ | Database | +--------------------+ | information_schema | | join_test | | mysql | | performance_schema | | sys | | test | | testdb | | wjqtest | +--------------------+ 8 rows in set (0.12 sec) root@localhost [(none)]>select user,host from mysql.user; +---------------+-----------+ | user | host | +---------------+-----------+ | root | % | | wjqrepl | 10.10.1.% | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | | wjqbackup | localhost | +---------------+-----------+ 6 rows in set (0.00 sec)
5、配置主从命令
root@localhost [(none)]>change master to -> master_host='10.10.1.10', -> master_user='wjqrepl', -> master_password='xxxxxx', -> master_port=3306, -> master_log_file='mysql3306.000008', -> master_log_pos=320114; Query OK, 0 rows affected, 2 warnings (0.09 sec)
6、启动主从复制
root@localhost [(none)]>start slave; Query OK, 0 rows affected (0.02 sec)
7、查看主从复制状态
root@localhost [(none)]>show slave status\G; *************************** 1. row *************************** Slave_IO_State: Connecting to master Master_Host: 10.10.1.10 Master_User: wjqrepl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql3306.000008 Read_Master_Log_Pos: 320114 Relay_Log_File: wjq2-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql3306.000008 Slave_IO_Running: Connecting Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 320114 Relay_Log_Space: 154 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 2003 Last_IO_Error: error connecting to master 'wjqrepl@10.10.1.10:3306' - retry-time: 60 retries: 1 Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 Master_UUID: Master_Info_File: /data/mysql/3306/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: 181106 15:43:15 Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
查看主从复制状态发现,出现了如上的报错:
Last_IO_Error: error connecting to master ‘wjqrepl@10.10.1.10:3306’ – retry-time: 60 retries: 1
原因分析:
IO线程:把主库binlog日志的内容记录到本机的中继日志文件里。
IO线程报错的原因有两个:第一是指定主库信息时参数信息有误;第二时安全限制(包括firewall,selinux)。
目前可以确定的是指定主库信息时参数信息没有问题,下面检查一下防火墙和selinux,发现原来的是防火墙没有关闭导致的
先停掉同步关系
root@localhost [(none)]>stop slave; Query OK, 0 rows affected (0.04 sec)
关闭防火墙
[root@wjq2 data]# systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2018-10-08 23:04:04 CST; 4 weeks 0 days ago Docs: man:firewalld(1) Main PID: 783 (firewalld) Tasks: 2 CGroup: /system.slice/firewalld.service └─783 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid Oct 08 23:03:54 wjq2 systemd[1]: Starting firewalld - dynamic firewall daemon... Oct 08 23:04:04 wjq2 systemd[1]: Started firewalld - dynamic firewall daemon. [root@wjq2 data]# systemctl stop firewalld.service [root@wjq2 data]# systemctl disable firewalld.service Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
重新启动主从同步
root@localhost [(none)]>start slave; Query OK, 0 rows affected (0.01 sec)
查看主从复制状态
root@localhost [(none)]>show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.10.1.10 Master_User: wjqrepl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql3306.000008 Read_Master_Log_Pos: 320114 Relay_Log_File: wjq2-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql3306.000008 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 320114 Relay_Log_Space: 526 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 330610 Master_UUID: dadc5a32-e0c9-11e8-9dd3-000c29c743b3 Master_Info_File: /data/mysql/3306/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
##Slave_IO_Running和Slave_SQL_Running均为Yes即主从复制正常
8、主从环境测试:
主库:
root@localhost [wjqtest]>create table test1(id int,name varchar(10)); Query OK, 0 rows affected (0.04 sec)
从库:
root@localhost [wjqtest]>show tables; +-------------------+ | Tables_in_wjqtest | +-------------------+ | backuptest | | test1 | | vote_record | +-------------------+ 3 rows in set (0.00 sec)
三、主从复制管理命令
show slave status\G 查看主从复制状态
show master status\G 查看主库binlog文件和位置及开启gtid模式下记录的gtid
change master to 在从库上配置主从过程
start slave 开启主从同步
stop slave 关闭主从同步
reset slave all 清空从库的所有配置信息
传统方式搭建MySQL 5.7异步复制环境就完成了,那mysql主从复制环境数据同步的原理是什么呢?当主从环境出现问题,我们要怎么去处理呢?莫着急,这一系列的问题,博主会接下来的文章中慢慢讲解;