一、MySQL主从复制原理
主从同步过程中主服务器有一个工作线程I/O dump thread,从服务器有两个工作线程I/O thread和SQL thread;
主服务器:
dump Thread:为每个Slave的I/O Thread启动一个dump线程,用于向其发送binary log events;
从服务器:
I/O Thread:向Master请求二进制日志事件,并保存于中继日志中;
SQL Thread:从中继日志中读取日志事件,在本地完成重放。
主库把外界接收的SQL请求记录到自己的binlog日志中,从库的I/O thread去请求主库的binlog日志,并将binlog日志写到中继日志中,然后从库的SQL Thread重做中继日志的SQL语句,从而达到数据复制效果。
与复制功能相关的文件:
master.info:用于保存slave连接至master时的相关信息,例如账号、密码、服务器地址等;
relay-log.info:保存在当前slave节点上已经复制的当前二进制日志和本地replay log日志的对应关系。
二、主从复制相关配置实验
1、一主一从
(1) 基本配置
两台服务器,初始化后就配置成一主一从服务器,实验中用到两台主机,一台当作主服务器(192.168.214.17),一台作为从服务器(192.168.214.18),系统为CentOS7.6,数据库用的mariadb-server 5.5.60(光盘yum源安装)。
1). 两台主机都安装好mariadb-server数据库
[[email protected] ~]# yum install -y mariadb-server
2). 在主服务器上配置mariadb主配置文件 /etc/my.cnf,在 [mysqld] 下添加两项 server_id=1 和 log-bin (开启二进制日志功能)
[[email protected]17 ~]# mkdir /data/logbin #创建二进制日志log-bin的存放路径 [[email protected]-17 ~]# chown -R mysql.mysql /data/logbin/ #修改权限 [[email protected]-17 ~]# cat /etc/my.cnf [mysqld] server-id=1 #添加此项,需和从服务器不同 log-bin=/data/logbin/mariadb-bin #添加此项 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d
3). 在主服务器上启动数据库服务,并创建用于同步的账号
[[email protected]17 ~]# systemctl start mariadb [[email protected]-17 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.60-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]> reset master; #先清理一下日志
MariaDB [(none)]> grant replication slave on *.* to [email protected]‘192.168.214.%‘ identified by ‘centos‘; Query OK, 0 rows affected (0.00 sec)
4). 在从服务器上配置mariadb主配置文件 /etc/my.cnf,在 [mysqld] 下添加两项 server_id=2 和 read-only
[[email protected]18 ~]# vim /etc/my.cnf [mysqld] server-id=2 read-only ...以下省略
5). 在从服务器上使用主服务上创建的具有复制权限的用户账号连接至主服务器,并启动复制线程
[[email protected]18 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 5 Server version: 5.5.60-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. MariaDB [(none)]> CHANGE MASTER TO -> MASTER_HOST=‘192.168.214.17‘, #主服务器IP -> MASTER_USER=‘repluser‘, #用于复制的用户 -> MASTER_PASSWORD=‘centos‘, #用户密码 -> MASTER_PORT=3306, #端口 -> MASTER_LOG_FILE=‘mariadb-bin.000001‘, #主服务器日志 -> MASTER_LOG_POS=245; #开始复制的日志位置 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> start slave; #开启复制线程 Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> show slave status\G; #查看状态 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.214.17 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mariadb-bin.000002 Read_Master_Log_Pos: 245 Relay_Log_File: mariadb-relay-bin.000003 Relay_Log_Pos: 531 Relay_Master_Log_File: mariadb-bin.000002 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: 245 Relay_Log_Space: 1269 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: 1 1 row in set (0.00 sec) ERROR: No query specified
6). 在主服务器上操作,测试同步复制效果
MariaDB [(none)]> create database db1; #在主服务器上建立了一个库 MariaDB [(none)]> show databases; #在从服务器上查看,可以看到已经同步复制成功了 +--------------------+ | Database | +--------------------+ | information_schema | | db1 | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.01 sec)
(2) 在现有一主服务器的情况下扩展一个从服务器
假如主服务器运行一段时间后,也存储了一部分数据了,此时需要扩展一个从服务器;基本思路与上面差不多,只是需要先把主库数据备份出来,再在从服务器上恢复数据,并设置同步点即可。实验中用到两台主机,一台当作主服务器(192.168.214.17),一台作为从服务器(192.168.214.18),系统为CentOS7.6,数据库用的mariadb-server 5.5.60(光盘yum源安装)。
1). 假设主服器上数据库已使用一段时间了(自己建一些数据),先在从服务器安装好mariadb-server数据库
[[email protected] ~]# yum install -y mariadb-server
2). 主服务器上配置mariadb主配置文件 /etc/my.cnf,在 [mysqld] 下添加两项 server_id=1 和 log-bin (开启二进制日志功能)
[[email protected]17 ~]# vim /etc/my.cnf [mysqld] server-id=1 log-bin=/data/logbin/mariadb-bin ...以下省略
3). 在主服务器上创建用于同步的账号,同时将主服务器数据备份,备份后传到从服务器上
[[email protected]17 ~]# mysql MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> grant replication slave on *.* to [email protected]‘192.168.214.%‘ identified by ‘centos‘; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> exit Bye [[email protected]-17 ~]# mysqldump -A -F --single-transaction --master-data=1 > /data/all.sql #备份 [[email protected]-17 ~]# ll /data/all.sql -rw-r--r-- 1 root root 521809 Dec 5 19:41 /data/all.sql [[email protected]-17 ~]# scp /data/all.sql 192.168.214.18:/data #传到从服务器上
4). 在从服务器配置mariadb主配置文件 /etc/my.cnf,在 [mysqld] 下添加两项 server_id=2 和 read-only,并启动数据库服务
[[email protected]18 ~]# vim /etc/my.cnf [mysqld] server-id=2 read-only ...以下省略 [[email protected]-18 ~]# systemctl start mariadb
5). 修改主服务器传给从服务器的备份文件all.sql中 CHANGE MASTER TO... 这部分内容后,还原数据,之后启动复制线程
[[email protected] ~]# vim /data/all.sql -- -- Position to start replication or point-in-time recovery from -- #原数据 CHANGE MASTER TO MASTER_LOG_FILE=‘mariadb-bin.000003‘, MASTER_LOG_POS=7811; #修改为以下 CHANGE MASTER TO MASTER_HOST=‘192.168.214.17‘, MASTER_USER=‘repluser‘, MASTER_PASSWORD=‘centos‘, MASTER_PORT=3306, MASTER_LOG_FILE=‘mariadb-bin.000003‘, MASTER_LOG_POS=7811; [[email protected] ~]# mysql < /data/all.sql #恢复数据 [[email protected] ~]# mysqlMariaDB [(none)]> start slave; #启动线程Query OK, 0 rows affected (0.01 sec)MariaDB [(none)]> show slave status\G; #查看启动情况
6). 在主服务器上操作,测试同步复制效果
MariaDB [(none)]> create datebase db1; #主服务器建库 MariaDB [(none)]> show databases; #可以看到从服务器已同步复制 +--------------------+ | Database | +--------------------+ | information_schema | | db1 | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.00 sec)
2、一主多从
实验中用到三台主机,一台当作主服务器(192.168.214.17),两台作为从服务器(192.168.214.18与192.168.214.19),系统为CentOS7.6,数据库用的mariadb-server 5.5.60(光盘yum源安装)。
1). 三台主机都安装好mariadb-server数据库
[[email protected] ~]# yum install -y mariadb-server
2). 主服务器上配置mariadb主配置文件 /etc/my.cnf,在 [mysqld] 下添加两项 server_id=1 和 log-bin (开启二进制日志功能)
[[email protected] ~]# vim /etc/my.cnf [mysqld] server-id=1 log-bin=/data/logbin/mariadb-bin ...以下省略
3). 在主服务器上创建用于同步的账号,并记录主服务器log-bin日志的文件和位置,同时将主服务器数据备份,备份后传到从服务器上
[[email protected] ~]# mysql MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> grant replication slave on *.* to [email protected]‘192.168.214.%‘ identified by ‘centos‘; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> exit Bye [[email protected] ~]# mysqldump -A -F --single-transaction --master-data=1 > /data/all.sql #备份 [[email protected] ~]# ll /data/all.sql -rw-r--r-- 1 root root 521809 Dec 5 19:41 /data/all.sql [[email protected] ~]# scp /data/all.sql 192.168.214.18:/data #传到从服务器上[[email protected] ~]# scp /data/all.sql 192.168.214.19:/data #传到从服务器上
4). 在从服务器mariadb主配置文件 /etc/my.cnf,在 [mysqld] 下添加两项 server_id(一台设为2,一台设为3)和 read-only,并启动数据库服务
[[email protected] ~]# vim /etc/my.cnf [mysqld] server-id=2 read-only ...以下省略 [[email protected] ~]# systemctl start mariadb
[[email protected]19 ~]# cat /etc/my.cnf [mysqld] server-id=3 read-only ...以下省略 [[email protected]-19 ~]# systemctl start mariadb [[email protected]-19 ~]# mysql < /data/all.sql
5). 修改主服务器传给从服务器的备份文件all.sql中 CHANGE MASTER TO... 这部分内容后,还原数据,之后启动复制线程
[[email protected] ~]# vim /data/all.sql -- -- Position to start replication or point-in-time recovery from -- #原数据 CHANGE MASTER TO MASTER_LOG_FILE=‘mariadb-bin.000003‘, MASTER_LOG_POS=7811; #修改为以下 CHANGE MASTER TO MASTER_HOST=‘192.168.214.17‘, MASTER_USER=‘repluser‘, MASTER_PASSWORD=‘centos‘, MASTER_PORT=3306, MASTER_LOG_FILE=‘mariadb-bin.000003‘, MASTER_LOG_POS=7811; [[email protected] ~]# mysql < /data/all.sql #恢复数据 [[email protected] ~]# mysqlMariaDB [(none)]> start slave; #启动线程Query OK, 0 rows affected (0.01 sec)MariaDB [(none)]> show slave status\G; #查看启动情况 #另一台从服务器按上面相同操作
6). 在主服务器上操作,测试同步复制效果
3、级联复制
实验中用到三台主机,一台当作主服务器(192.168.214.17),一台作为中间服务器(192.168.214.18),一台作为中间服务器的从服务器(192.168.214.19),系统为CentOS7.6,数据库用的mariadb-server 5.5.60(光盘yum源安装)。
1). 三台主机都安装好mariadb-server数据库
[[email protected] ~]# yum install -y mariadb-server
2). 主服务器上配置mariadb主配置文件 /etc/my.cnf,在 [mysqld] 下添加两项 server_id=1 和 log-bin (开启二进制日志功能)
[[email protected] ~]# vim /etc/my.cnf [mysqld] server-id=1 log-bin=/data/logbin/mariadb-bin ...以下省略
3). 在主服务器上创建用于同步的账号,同时将主服务器数据备份,备份后传到中间服务器上
[[email protected] ~]# mysql MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> grant replication slave on *.* to [email protected]‘192.168.214.%‘ identified by ‘centos‘; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> exit Bye [[email protected] ~]# mysqldump -A -F --single-transaction --master-data=1 > /data/all.sql #备份 [[email protected] ~]# ll /data/all.sql -rw-r--r-- 1 root root 521809 Dec 5 19:41 /data/all.sql [[email protected] ~]# scp /data/all.sql 192.168.214.18:/data #传到中间服务器上
4). 在中间服务器配置mariadb主配置文件 /etc/my.cnf,在 [mysqld] 下添加四项 server_id=2、read-only、log-bin、log-slave-updates,并启动数据库服务
[[email protected]18 ~]# mkdir /data/logbin [[email protected]-18 ~]# chown -R mysql.mysql /data/logbin[[email protected] ~]# vim /etc/my.cnf[mysqld]server-id=2read-onlylog-bin=/data/logbin/mariadb-binlog-slave-updates...以下省略 [[email protected] ~]# systemctl start mariadb
5). 修改主服务器传给中间服务器的备份文件all.sql中 CHANGE MASTER TO... 这部分内容后,还原数据,之后启动复制线程
[[email protected] ~]# vim /data/all.sql -- -- Position to start replication or point-in-time recovery from -- #原数据 CHANGE MASTER TO MASTER_LOG_FILE=‘mariadb-bin.000003‘, MASTER_LOG_POS=7811; #修改为以下 CHANGE MASTER TO MASTER_HOST=‘192.168.214.17‘, MASTER_USER=‘repluser‘, MASTER_PASSWORD=‘centos‘, MASTER_PORT=3306, MASTER_LOG_FILE=‘mariadb-bin.000003‘, MASTER_LOG_POS=7811; [[email protected] ~]# mysql < /data/all.sql #恢复数据 [[email protected] ~]# mysqlMariaDB [(none)]> start slave; #启动线程Query OK, 0 rows affected (0.01 sec)MariaDB [(none)]> show slave status\G; #查看启动情况
6). 将中间服务器数据备份,备份后传到从服务器上
[[email protected]-18 ~]# mysqldump -A -F --single-transaction --master-data=1 > /data/all2.sql #备份中间服务器的数据 [[email protected]-18 ~]# scp /data/all2.sql 192.168.214.19:/data #传给从服务器
7). 在从服务器配置mariadb主配置文件 /etc/my.cnf,在 [mysqld] 下添加两项 server_id=3和 read-only,并启动数据库服务
[[email protected]19 ~]# vim /etc/my.cnf [mysqld] server-id=3 read-only ...以下省略 [[email protected] ~]# systemctl start mariadb
8). 修改中间服务器传给从服务器的备份文件all2.sql 中 CHANGE MASTER TO... 这部分内容后,还原数据,之后启动复制线程
[[email protected]19 ~]# vim /data/all2.sql ...以上省略 -- -- Position to start replication or point-in-time recovery from -- #原数据 CHANGE MASTER TO MASTER_LOG_FILE=‘mariadb-bin.000004‘, MASTER_LOG_POS=245; #修改为以下 CHANGE MASTER TO MASTER_HOST=‘192.168.214.18‘, #中间服务器IP MASTER_USER=‘repluser‘, MASTER_PASSWORD=‘centos‘, MASTER_PORT=3306, MASTER_LOG_FILE=‘mariadb-bin.000004‘, MASTER_LOG_POS=245; ...以下省略 [[email protected]-19 ~]# systemctl start mariadb [[email protected]-19 ~]# mysql < /data/all2.sql [[email protected]-19 ~]# mysql MariaDB [(none)]> start slave; #启动线程 Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> show slave status\G; #查看启动情况
注意:若使用 show slave status\G;查看启动情况时出现错误如:...Host ‘192.168.214.19‘ is not allowed to connect...时,可以中间服务器上登录数据库,使用flush privileges; 刷新下权限即可。
9). 在主服务器上操作,测试同步复制效果
4、半同步复制
默认情况下,MySQL的复制功能是异步的,异步复制可以提供最佳的性能,主库把binlog日志发送给从库即结束,并不验证从库是否接收完毕。这意味着当主服务器或从服务器端发生故障时,有可能从服务器没有接收到主服务器发送过来的binlog日志,这就会造成主服务器和从服务器的数据不一致,甚至在恢复时造成数据的丢失;半同步复制则是主库只需等待至少一个从库接收完毕即可,而不需要等待所有从库接收完成。
实验中用到三台主机,一台当作主服务器(192.168.214.17),两台作为从服务器(192.168.214.18与192.168.214.19),系统为CentOS7.6,数据库用的mariadb-server 5.5.60(光盘yum源安装),基本思路为先实现一主多从,再在主服务器与从服务器上装插件实现。
1). 三台主机都安装好mariadb-server数据库
[[email protected] ~]# yum install -y mariadb-server
2). 主服务器上配置mariadb主配置文件 /etc/my.cnf,在 [mysqld] 下添加两项 server_id=1 和 log-bin (开启二进制日志功能)
[[email protected]17 ~]# mkdir /data/logbin [[email protected]-17 ~]# chown -R mysql.mysql /data/logbin/ [[email protected]-17 ~]# vim /etc/my.cnf [mysqld] server-id=1 log-bin=/data/logbin/mariadb-bin ...以下省略
3). 在主服务器上创建用于同步的账号,然后将主服务器数据备份,备份后传到两个从服务器上
[[email protected]17 ~]# systemctl start mariadb [[email protected]-17 ~]# mysql < /data/hellodb_innodb.sql [[email protected]-17 ~]# mysql MariaDB [(none)]> grant replication slave on *.* to [email protected]‘192.168.214.%‘ identified by ‘centos‘; #建立帐号 Query OK, 0 rows affected (0.01 sec) [[email protected]-17 ~]# scp /data/all.sql 192.168.214.18:/data [[email protected]-17 ~]# scp /data/all.sql 192.168.214.19:/data
4). 在从服务器mariadb主配置文件 /etc/my.cnf,在 [mysqld] 下添加两项 server_id(一台设为2,一台设为3)和 read-only,然后启动数据库服务
[[email protected]18 ~]# vim /etc/my.cnf [mysqld] server-id=2 read-only ...以下省略 [[email protected]ntos7-18 ~]# systemctl start mariadb [[email protected]-19 ~]# vim /etc/my.cnf [mysqld] server-id=3 read-only ...以下省略 [[email protected]-19 ~]# systemctl start mariadb
5). 修改主服务器传过来的备份文件中 CHANGE MASTER TO... 这部分内容,还原数据,之后启动复制线程
[[email protected]18 ~]# vim /data/all.sql -- -- Position to start replication or point-in-time recovery from -- #原数据 CHANGE MASTER TO MASTER_LOG_FILE=‘mariadb-bin.000005‘, MASTER_LOG_POS=245; #修改为以下 CHANGE MASTER TO MASTER_HOST=‘192.168.214.17‘, MASTER_USER=‘repluser‘, MASTER_PASSWORD=‘centos‘, MASTER_PORT=3306, MASTER_LOG_FILE=‘mariadb-bin.000005‘, MASTER_LOG_POS=245; [[email protected]-19 ~]# vim /data/all.sql #另一台也按以上改即可
[[email protected]18 ~]# mysql < /data/all.sql [[email protected]-18 ~]# mysql MariaDB [(none)]> start slave; [[email protected]-19 ~]# mysql < /data/all.sql [[email protected]-19 ~]# mysql MariaDB [(none)]> start slave;
6). 在主服务器上操作,测试同步复制效果,如无问题即可进行下一步
7). 在主服务器上安装插件 rpl_semi_sync_master ,并启用插件;
[[email protected]17 ~]# mysql MariaDB [(none)]> install plugin rpl_semi_sync_master soname ‘semisync_master.so‘; #安装插件 Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> show global variables like ‘%semi%‘; #查询变量 +------------------------------------+-------+ | Variable_name | Value | +------------------------------------+-------+ | rpl_semi_sync_master_enabled | OFF | | rpl_semi_sync_master_timeout | 10000 | | rpl_semi_sync_master_trace_level | 32 | | rpl_semi_sync_master_wait_no_slave | ON | +------------------------------------+-------+ 4 rows in set (0.00 sec) MariaDB [(none)]> set global rpl_semi_sync_master_enabled=on ; #设置此变量为on Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show global variables like ‘%semi%‘; +------------------------------------+-------+ | 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_no_slave | ON | +------------------------------------+-------+ 4 rows in set (0.00 sec) MariaDB [(none)]> show global status like ‘%semi%‘; +--------------------------------------------+-------+ | 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)
8). 在两个从服务器上安装插件 rpl_semi_sync_slave,并启用插件
[[email protected]18 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 5.5.60-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. MariaDB [(none)]> stop slave; #先停止线程 MariaDB [(none)]> install plugin rpl_semi_sync_slave soname ‘semisync_slave.so‘; #安装插件 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show global variables like ‘%semi%‘; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | rpl_semi_sync_slave_enabled | OFF | | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+-------+ 2 rows in set (0.00 sec) MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=on ; #设置为on Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show global variables like ‘%semi%‘; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | rpl_semi_sync_slave_enabled | ON | #可以看到开启成功了 | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+-------+ 2 rows in set (0.00 sec) MariaDB [(none)]> show global status like ‘%semi%‘; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | Rpl_semi_sync_slave_status | OFF | #目前状态为OFF +----------------------------+-------+ 1 row in set (0.00 sec) MariaDB [(none)]> start slave; #开启线程 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show global status like ‘%semi%‘; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | Rpl_semi_sync_slave_status | ON | #可以看到状态为ON了 +----------------------------+-------+ 1 row in set (0.00 sec) MariaDB [(none)]> show slave status\G; #查看线程启动情况 #主服务器上查看,配置好了一个从服务器,Rpl_semi_sync_master_clients 值变为1了 MariaDB [(none)]> show global status like ‘%semi%‘; +--------------------------------------------+-------+ | Variable_name | Value | +--------------------------------------------+-------+ | Rpl_semi_sync_master_clients | 1 | | 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 | +--------------------------------------------+-------+ #另一从服务器按以上再配置一次即可 #配置好后 Rpl_semi_sync_master_clients 值变为2了,至此也就配置完成了 MariaDB [(none)]> show global status like ‘%semi%‘; +--------------------------------------------+-------+ | Variable_name | Value | +--------------------------------------------+-------+ | Rpl_semi_sync_master_clients | 2 | | 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)
9). 在主服务器上操作,测试效果即可。
原文地址:https://www.cnblogs.com/hovin/p/11990677.html