MySQL主从同步之半同步复制
一、MySQL主从同步介绍
MySQL异步复制是指,MySQL主库将事务信息写入binlog文件中的时候,此时主库会通过binlog dump线程给从库发送这些新的binlog变化,然后并不等待从库的响应继续提交事务并写入binlog,所以主库并不保证这些事务变化的binlog数据会传输并应用到任何从库。
MySQL全同步复制是指,当主库提交事务的binlog后,所有的从库节点必须全部收到事务并且apply并且提交这些内容之后,即io_thread和sql_thread完成所有binlog变化的接受的应用执行,主库的线程才可以继续进行后续操作,但是缺点是,主库完成一个事务的时间会被拉长,性能急剧降低。
MySQL半同步复制是介于异步和全同步之间,主库只需要等待至少一个从节点,收到并且flush binlog到relay log文件即可,主库不需要等待所有从库给主库反馈,这里只是一个收到的反馈,而并不是从库已经完成并提交的反馈,即从库只应用完成io_thread内容即可无需等到sql_thread的执行完成。
二、MySQL搭建主从复制
2.1 普通部署
文章推荐:UCloud MySQL与自建MySQL搭建主从同步
2.2 容器化部署
(1)创建容器网络
[root@10-27-0-224 ~]# docker network create master-slave 9c20984e29c42ca9b0b09f5a07bb1246b294b158cd6efdb88cea5db0ac38c2f7
(2)创建master和slave容器
[root@10-27-0-224 ~]# docker run -itd --restart=always --name master_mysql --net master-slave -p 3306:3306 --mount src=mysql-master,dst=/var/lib/mysql -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 --character-set-server=utf8 [root@10-27-0-224 ~]# docker run -itd --restart=always --name slave_mysql --net master-slave -p 3307:3306 --mount src=mysql-slave,dst=/var/lib/mysql -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 --character-set-server=utf8
(3)MySQL容器安装vim编辑器
[root@10-27-0-224 ~]# docker exec -it master_mysql bash root@0b120d58d1ea:/# apt-get update Get:1 http://deb.debian.org/debian buster InRelease [122 kB] Get:2 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB] Get:3 http://deb.debian.org/debian buster-updates InRelease [51.9 kB] Get:4 http://repo.mysql.com/apt/debian buster InRelease [21.5 kB] Get:5 http://security.debian.org/debian-security buster/updates/main amd64 Packages [298 kB] Get:6 http://deb.debian.org/debian buster/main amd64 Packages [7907 kB] Get:7 http://deb.debian.org/debian buster-updates/main amd64 Packages [15.2 kB] Get:8 http://repo.mysql.com/apt/debian buster/mysql-5.7 amd64 Packages [5686 B] Fetched 8486 kB in 2s (4436 kB/s) Reading package lists... Done root@0b120d58d1ea:/# apt-get install vim
【注】主库和从库同样配置。
2.3 修改MySQL配置
(1)主库配置
[root@10-27-0-224 ~]# docker exec -it master_mysql bash root@0b120d58d1ea:/# vim /etc/mysql/mysql.conf.d/mysqld.cnf [mysqld] server-id = 100 log-bin = mysql-master-bin [root@10-27-0-224 ~]# mysql -uroot -h10.27.0.224 -P3306 -p123456 mysql> show variables like "log_bin"; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ 1 row in set (0.01 sec) mysql> select@@server_id; +-------------+ | @@server_id | +-------------+ | 100 | +-------------+ 1 row in set (0.00 sec)
(2)从库配置
[root@10-27-0-224 ~]# docker exec -it slave_mysql bash root@f440cb50f05c:/# vim /etc/mysql/mysql.conf.d/mysqld.cnf [mysqld] server-id = 200 log-bin = mysql-slave-bin relay_log = mysql-relay-bin [root@10-27-0-224 ~]# mysql -uroot -h10.27.0.224 -P3307 -p123456 mysql> show variables like "log_bin"; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ 1 row in set (0.01 sec) mysql> show variables like "relay_log"; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | relay_log | mysql-relay-bin | +---------------+-----------------+ 1 row in set (0.01 sec) mysql> select@@server_id; +-------------+ | @@server_id | +-------------+ | 200 | +-------------+ 1 row in set (0.00 sec)
2.4 搭建主从关系
(1)查看主从库信息
# 查看主库IP [root@10-27-0-224 ~]# docker inspect master_mysql |grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "", "IPAddress": "172.18.0.2", # 查看主库持久卷 [root@10-27-0-224 var]# docker volume inspect mysql-master [ { "CreatedAt": "2021-08-02T15:52:43+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/mysql-master/_data", "Name": "mysql-master", "Options": null, "Scope": "local" } ] # 查看从库IP [root@10-27-0-224 ~]# docker inspect slave_mysql |grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "", "IPAddress": "172.18.0.3", # 查看从库持久卷 [root@10-27-0-224 var]# docker volume inspect mysql-slave [ { "CreatedAt": "2021-08-02T15:16:57+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/mysql-slave/_data", "Name": "mysql-slave", "Options": null, "Scope": "local" } ]
(2)创建主从复制用户
#登陆到主库中创建并授权主从复制用户 mysql> create user ' slavebackup '@'%' identified by 'ucloudbackup'; mysql> grant replication slave on *.* to slavebackup@'%' identified by 'ucloudbackup'; mysql> flush privileges;
(3)查看主库binlog信息
mysql> show master status\G *************************** 1. row *************************** File: mysql-bin.000001 Position: 154 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec) mysql> show master logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 840 | +------------------+-----------+ 1 row in set (0.00 sec)
(4)从库执行命令
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154,MASTER_HOST='172.18.0.2',MASTER_PORT=3306,MASTER_USER='slavebackup',MASTER_PASSWORD='ucloudbackup',master_connect_retry=30; 参数解读: master_port # Master的端口号,指的是容器的端口号 master_user # 用于数据同步的用户 master_password # 用于同步的用户的密码 master_log_file # 指定 Slave 从哪个日志文件开始复制数据,即上文中提到的 File 字段的值 master_log_pos # 从哪个 Position 开始读,即上文中提到的 Position 字段的值 master_connect_retry # 如果连接失败,重试的时间间隔,单位是秒,默认是60秒
(5)查看主从关系
mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.18.0.2 Master_User: slavebackup Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 840 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 1006 Relay_Master_Log_File: mysql-bin.000001 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: 840 Relay_Log_Space: 1213 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: 100 Master_UUID: 54787f80-f39f-11eb-8e91-0242ac120002 Master_Info_File: /var/lib/mysql/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)
三、MySQL半同步插件安装
3.2 主库插件安装
# 主库安装 [root@10-27-0-224 ~]# docker exec -it master_mysql sh # mysql -uroot -p123456 mysql> show global variables like 'plugin_dir'; +---------------+------------------------+ | Variable_name | Value | +---------------+------------------------+ | plugin_dir | /usr/lib/mysql/plugin/ | +---------------+------------------------+ 1 row in set (0.01 sec) mysql> install plugin rpl_semi_sync_master soname "semisync_master.so"; Query OK, 0 rows affected (0.01 sec) mysql> set global rpl_semi_sync_master_enabled=1; Query OK, 0 rows affected (0.00 sec) mysql> show variables like "%semi_sync%"; # 查看半同步复制的值 +-------------------------------------------+------------+ | Variable_name | Value | +-------------------------------------------+------------+ | rpl_semi_sync_master_enabled | ON | | rpl_semi_sync_master_timeout | 10000 | | rpl_semi_sync_master_trace_level | 32 | | rpl_semi_sync_master_wait_for_slave_count | 1 | | rpl_semi_sync_master_wait_no_slave | ON | | rpl_semi_sync_master_wait_point | AFTER_SYNC | +-------------------------------------------+------------+ 6 rows in set (0.01 sec) mysql> show status like "rpl%"; # 查看半同步复制的相关信息 +--------------------------------------------+-------+ | Variable_name | Value | +--------------------------------------------+-------+ | Rpl_semi_sync_master_clients | 0 | | Rpl_semi_sync_master_net_avg_wait_time | 0 | | Rpl_semi_sync_master_net_wait_time | 0 | | Rpl_semi_sync_master_net_waits | 0 | | Rpl_semi_sync_master_no_times | 0 | | Rpl_semi_sync_master_no_tx | 0 | | Rpl_semi_sync_master_status | ON | | Rpl_semi_sync_master_timefunc_failures | 0 | | Rpl_semi_sync_master_tx_avg_wait_time | 0 | | Rpl_semi_sync_master_tx_wait_time | 0 | | Rpl_semi_sync_master_tx_waits | 0 | | Rpl_semi_sync_master_wait_pos_backtraverse | 0 | | Rpl_semi_sync_master_wait_sessions | 0 | | Rpl_semi_sync_master_yes_tx | 0 | +--------------------------------------------+-------+ 14 rows in set (0.00 sec) mysql> set global rpl_semi_sync_master_timeout = 2000; Query OK, 0 rows affected (0.00 sec) mysql> show variables like "rpl_semi_sync_master_timeout"; # 半同步退化时间 +------------------------------+-------+ | Variable_name | Value | +------------------------------+-------+ | rpl_semi_sync_master_timeout | 2000 | +------------------------------+-------+ 1 row in set (0.00 sec)
3.2 从库插件安装
# 从库安装 [root@10-27-0-224 ~]# docker exec -it slave_mysql sh # mysql -uroot -p123456 mysql> install plugin rpl_semi_sync_slave soname "semisync_slave.so"; Query OK, 0 rows affected (0.01 sec) mysql> set global rpl_semi_sync_slave_enabled=1; Query OK, 0 rows affected (0.01 sec) mysql> show variables like "%semi_sync%"; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | rpl_semi_sync_slave_enabled | ON | | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+-------+ 2 rows in set (0.01 sec) mysql> show status like "rpl%"; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | Rpl_semi_sync_slave_status | OFF | +----------------------------+-------+ 1 row in set (0.01 sec)
作者:UStarGao
链接:https://www.starcto.com/mysql/195.html
来源:STARCTO
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
UCloud云平台推荐
随便看看
- 2021-09-07开源ShowDoc文档管理平台容器化部署
- 2021-09-24Postfix邮件服务器安装部署
- 2021-11-03Redis持久化解决方案实操
- 2021-06-12MySQL主从同步异常之跳过错误
- 2021-09-13开源为知笔记容器化部署