运维项目实训 - mysql主从复制+半同步复制

主从复制(常用方法)

主从复制(异步复制)过程

主数据库(Master)将变更信息写入到二进制日志文件中,这里需要注意的是旧版本的MySQL数据库默认是不开启二进制日志的,强烈建议在安装好数据库启动之前一定要先检查一下二进制日志文件是否开启,即使不做主从复制架构也要开启,否则当数据库启动之后再开启二进制日志时需要重新启动数据库。
从数据库(Slave)开启一个IO工作线程,通过该IO线程与主数据库建立一个普通客户端连接,主数据库会启动一个二进制日志转储线程(binglog dump thread),从数据库的IO线程通过这个转储线程读取主库上的变更事件,并将变更事件记录到中继日志中(relay_log),如果从数据库的IO线程读取速度追赶上主库的事件变更,在没有得到新变更的通知时,IO线程会进入Sleep状态。
从数据库还会启动一个SQL Thread线程,这个线程从中继日志(relay_log)中读取变更事件,并将变更同步到从数据库中。同时,可以通过配置选项,除了将变更存储到数据库中,也可以将变更事件同时存储在从数据库的二进制日志中
缺点
mysql主从复制存在的问题:
主库宕机后,数据可能丢失
从库只有一个sql Thread,主库写压力大,复制很可能延时

主从复制配置

配置环境

master机:server1(172.25.52.1)
slave机: server2(172.25.52.2)

master机和slave机安装mysql

master和slave安装方式一样

[root@server1 ~]# ls
mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
[root@server1 ~]# tar xf mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
[root@server1 ~]# ls
mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
mysql-community-client-5.7.17-1.el6.x86_64.rpm
mysql-community-common-5.7.17-1.el6.x86_64.rpm
mysql-community-devel-5.7.17-1.el6.x86_64.rpm
mysql-community-embedded-5.7.17-1.el6.x86_64.rpm
mysql-community-embedded-devel-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
mysql-community-server-5.7.17-1.el6.x86_64.rpm
mysql-community-test-5.7.17-1.el6.x86_64.rpm
[root@server1 ~]# yum install mysql-community-client-5.7.17-1.el6.x86_64.rpm
                              mysql-community-common-5.7.17-1.el6.x86_64.rpm
                              mysql-community-libs-5.7.17-1.el6.x86_64.rpm
                              mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
                              mysql-community-server-5.7.17-1.el6.x86_64.rpm

mysql初始化配置

[root@server1 mysql]# vim /etc/init.d/mysqld    ##注释以下mysqld的启动脚本里这几行,mysql可设置短密码。
112          #  [ $ret -ne 0 ] && return $ret   
113          #  initfile="$(install_validate_password_sql_file)"
114          #  action $"Installing validate password plugin: " /usr/sbin/mysqld --datadir="$datadir" --user=mysql --init-file="$in    itfile"
115          #  ret=$?
116          #  rm -f "$initfile"
117          #  chown -R mysql:mysql "$datadir"
[root@server1 mysql]# /etc/init.d/mysqld start  ##启动mysql
Initializing MySQL database:                               [  OK  ]
Starting mysqld:                                           [  OK  ]

mysql5.7版本默认会先自定义一个密码,过滤mysql.log找出密码

[root@server1 mysql]# grep password /var/log/mysqld.log 
2018-07-12T01:22:49.679342Z 1 [Note] A temporary password is generated for root@localhost: ,ZcZg(GT=0_E
2018-07-12T01:23:00.656610Z 0 [Note] Execution of init_file '/var/lib/mysql/install-validate-password-plugin.Waszzi.sql' started.
2018-07-12T01:23:00.678366Z 0 [Note] Execution of init_file '/var/lib/mysql/install-validate-password-plugin.Waszzi.sql' ended.
2018-07-12T01:23:02.513360Z 0 [Note] Shutting down plugin 'sha256_password'
2018-07-12T01:23:02.513362Z 0 [Note] Shutting down plugin 'mysql_native_password'
2018-07-12T01:23:04.023665Z 3 [Note] Access denied for user 'UNKNOWN_MYSQL_USER'@'localhost' (using password: NO)
2018-07-12T01:31:32.263313Z 4 [Note] Access denied for user 'UNKNOWN_MYSQL_USER'@'localhost' (using password: NO)
2018-07-12T01:32:24.272071Z 5 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2018-07-12T01:32:26.536394Z 6 [Note] Access denied for user 'root'@'localhost' (using password: YES)
2018-07-12T01:32:36.354067Z 7 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2018-07-12T01:32:41.478044Z 8 [Note] Your password has expired. To log in you must change it using a client that supports expired passwords.
2018-07-12T01:46:57.955969Z 0 [Note] Shutting down plugin 'validate_password'
2018-07-12T01:46:59.789874Z 0 [Note] Shutting down plugin 'sha256_password'
2018-07-12T01:46:59.789876Z 0 [Note] Shutting down plugin 'mysql_native_password'
2018-07-12T01:47:04.315762Z 0 [Note] Shutting down plugin 'sha256_password'
2018-07-12T01:47:04.315767Z 0 [Note] Shutting down plugin 'mysql_native_password'
2018-07-12T01:50:01.774901Z 1 [Note] A temporary password is generated for root@localhost: lQ6pNCsD1q%0 ##这个位置就是密码。
2018-07-12T01:50:14.476215Z 3 [Note] Access denied for user 'UNKNOWN_MYSQL_USER'@'localhost' (using password: NO)
2018-07-12T01:50:24.742827Z 4 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2018-07-12T01:50:43.377987Z 5 [Note] Access denied for user 'root'@'localhost' (using password: YES)
[root@server1 ~]# mysql_secure_installation    ##初始化

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: 
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

以上master机和slave配置方式一样

master机配置

[root@server1 ~]# vim /etc/my.cnf
 29 server-id=1  
 30 log-bin=mysql-bin    ##开启二进制日志

[root@server1 mysql]# /etc/init.d/mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@server1 mysql]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> flush privileges;        ##同步
Query OK, 0 rows affected (0.01 sec)

mysql> show master status;          ##查看master机状态
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      306 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

slave机配置

[root@server2 mysql]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> change master to master_host='172.25.52.1',master_user='redhat',master_password='westos',master_log_file='mysql-bin.000002',master_log_pos=601;
Query OK, 0 rows affected, 2 warnings (0.36 sec)

mysql> start slave;
Query OK, 0 rows affected (0.07 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.52.1
                  Master_User: redhat
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 601
               Relay_Log_File: server2-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-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: 601
              Relay_Log_Space: 529
              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
                  Master_UUID: e3ff0601-8575-11e8-9d89-525400216ac0
             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)

ERROR: 
No query specified

如果 Slave_IO_Running 是 Connecting,原因:网络、授权、log_file/log_pos

测试

master机写入数据:

mysql> create database user;
Query OK, 1 row affected (0.14 sec)
mysql> use user;
Database changed
mysql> create table usertb (
    -> username varchar(20) not null,
    -> age varchar(4) not null);
Query OK, 0 rows affected (1.04 sec)

mysql> show tables;
+----------------+
| Tables_in_user |
+----------------+
| usertb         |
+----------------+
1 row in set (0.00 sec)
mysql> insert into usertb values('wuyanzu','18');
Query OK, 1 row affected (0.42 sec)

mysql> select * from usertb;
+----------+-----+
| username | age |
+----------+-----+
| wuyanzu  | 18  |
+----------+-----+
1 row in set (0.00 sec)

slave机查看:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| user               |
| westos             |
+--------------------+
6 rows in set (0.00 sec)

mysql> select * from user.usertb;
+----------+-----+
| username | age |
+----------+-----+
| wuyanzu  | 18  |
+----------+-----+
1 row in set (0.00 sec)

GTID方式配置主从复制

slave机配置

mysql> stop slave;
Query OK, 0 rows affected (0.05 sec)

mysql> ^DBye
[root@server2 ~]# vim /etc/my.cnf

 29 server-id=2
 30 gtid_mode = ON
 31 enforce-gtid-consistency = true
[root@server2 ~]# /etc/init.d/mysqld restart 
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

master机配置

[root@server1 ~]# vim /etc/my.cnf
 29 server-id=1
 30 log-bin=mysql-bin
 31 gtid_mode = ON
 32 enforce-gtid-consistency = true
[root@server1 ~]# /etc/init.d/mysqld restart 
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@server1 ~]# mysql -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> insert into user.usertb values('chenguanxi','19');
Query OK, 1 row affected (0.38 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+----------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                      |
+------------------+----------+--------------+------------------+----------------------------------------+
| mysql-bin.000002 |      422 |              |                  | 0e09c92a-8139-11e8-9d84-52540013d792:1 |
+------------------+----------+--------------+------------------+----------------------------------------+

slave机做认证配置

[root@server2 mysql]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

mysql> change master to master_host='172.25.52.1', master_user='redhat', master_password='westos', master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.10 sec)

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.25.52.1
                  Master_User: redhat
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 1285
               Relay_Log_File: server2-relay-bin.000002
                Relay_Log_Pos: 414
        Relay_Master_Log_File: mysql-bin.000003
             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: 1285
              Relay_Log_Space: 663
              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
                  Master_UUID: e3ff0601-8575-11e8-9d89-525400216ac0
             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: e3ff0601-8575-11e8-9d89-525400216ac0:1-5
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

测试

master机写入:

mysql> insert into user.usertb values('lee','20');
Query OK, 1 row affected (0.03 sec)

mysql> select * from user.usertb;
+----------+-----+
| username | age |
+----------+-----+
| westos   | 18  |
| redhat   | 21  |
| lee      | 20  |
+----------+-----+
3 rows in set (0.00 sec)

slave机查看:

mysql> select * from user.usertb;
+----------+-----+
| username | age |
+----------+-----+
| westos   | 18  |
| redhat   | 21  |
| lee      | 20  |
+----------+-----+
3 rows in set (0.00 sec)

半同步复制

这里写图片描述

在GTID主从复制的基础上导入半同步模块模块

master机:

mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';  ##导入半同步master模块
Query OK, 0 rows affected (0.02 sec)

mysql> set global rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'rpl%';
+-------------------------------------------+------------+
| 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 |
| rpl_stop_slave_timeout                    | 31536000   |
+-------------------------------------------+------------+
7 rows in set (0.00 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)

slave机:

mysql> select * from mysql.gtid_executed;
+--------------------------------------+----------------+--------------+
| source_uuid                          | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| e3ff0601-8575-11e8-9d89-525400216ac0 |              1 |            1 |
| e3ff0601-8575-11e8-9d89-525400216ac0 |              2 |            2 |
| e3ff0601-8575-11e8-9d89-525400216ac0 |              3 |            3 |
| e3ff0601-8575-11e8-9d89-525400216ac0 |              4 |            4 |
| e3ff0601-8575-11e8-9d89-525400216ac0 |              5 |            5 |
| e3ff0601-8575-11e8-9d89-525400216ac0 |              6 |            6 |
+--------------------------------------+----------------+--------------+
6 rows in set (0.00 sec)

mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.02 sec)

mysql> show variables like 'rpl%';
+---------------------------------+----------+
| Variable_name                   | Value    |
+---------------------------------+----------+
| rpl_semi_sync_slave_enabled     | OFF      |
| rpl_semi_sync_slave_trace_level | 32       |
| rpl_stop_slave_timeout          | 31536000 |
+---------------------------------+----------+
3 rows in set (0.00 sec)

mysql> set global rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'rpl%';
+---------------------------------+----------+
| Variable_name                   | Value    |
+---------------------------------+----------+
| rpl_semi_sync_slave_enabled     | ON       |
| rpl_semi_sync_slave_trace_level | 32       |
| rpl_stop_slave_timeout          | 31536000 |
+---------------------------------+----------+
3 rows in set (0.00 sec)

mysql> show status like 'rpl%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | OFF   |
+----------------------------+-------+
1 row in set (0.00 sec)

mysql> stop slave io_thread;  
Query OK, 0 rows affected (0.01 sec)

mysql> start slave io_thread;
Query OK, 0 rows affected (0.00 sec)

mysql>  show status like 'rpl%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |
+----------------------------+-------+
1 row in set (0.00 sec)

测试:

master机写入数据,并接受到slave机返回的ack值

mysql> insert into user.usertb values('liudehua','21');
Query OK, 1 row affected (0.26 sec)

mysql> show status like 'rpl%';
+--------------------------------------------+-------+
| 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             | 1     |
| 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      | 1372  |
| Rpl_semi_sync_master_tx_wait_time          | 1372  |
| Rpl_semi_sync_master_tx_waits              | 1     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 1     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)

将slave机的io线程停掉,master机写入数据,会默认等待10s,如果超时还未等到slave机的ack,将自动切换到异步复制,如果slave机的io线程再次开启,复制将自动切换到半同步方式

slave机:

mysql> stop slave io_thread;
Query OK, 0 rows affected (0.04 sec)

master机:

mysql> insert into user.usertb values('zhangxueyou','22');
Query OK, 1 row affected (10.45 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             | 1     |
| Rpl_semi_sync_master_no_times              | 1     |
| Rpl_semi_sync_master_no_tx                 | 1     |
| Rpl_semi_sync_master_status                | OFF   |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 1372  |
| Rpl_semi_sync_master_tx_wait_time          | 1372  |
| Rpl_semi_sync_master_tx_waits              | 1     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 1     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/lx543733371/article/details/81014972