Bootstrap

mysql 基于GTID的主从同步、半同步复制

master:server3 172.25.61.3
slave:server4 172.25.61.4


Server3:
yum install -y mysql-community-client-5.7.17-1.el6.x86_64.rpm \
mysql-community-server-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
vim /etc/my.cnf

server-id=1
log-bin=mysql-bin


/etc/init.d/mysqld start
grep password /var/log/mysqld.log

复制初始密码

mysql_secure_installation   ##输入初始密码,更改密码


mysql -p

show master status;

GRANT REPLICATION SLAVE ON *.* TO 'westos'@'172.25.61.%' identified by 'MoShuai123!';

 

Server4;

yum install -y mysql-community-client-5.7.17-1.el6.x86_64.rpm \
mysql-community-server-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

vim /etc/my.cnf

server-id=2

/etc/init.d/mysqld start

mysql_secure_installation

mysql -p

mysql> change master to master_host='172.25.61.3',
    -> master_user='westos',
    -> master_password='MoShuai123!',
    -> master_log_file='mysql-bin.000003',
    -> master_log_pos=1805;
Query OK, 0 rows affected, 2 warnings (1.13 sec)

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


mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.61.3
                  Master_User: westos
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 3317
               Relay_Log_File: server4-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

测试:

Server3;

mysql> create database test;
Query OK, 1 row affected (1.10 sec)

mysql> use test;
Database changed
mysql> create table user(
    -> username varchar(15) not null,
    -> password varchar(25) not null);
Query OK, 0 rows affected (0.49 sec)

mysql> insert into user values ('user2','123');
Query OK, 1 row affected (0.11 sec)

mysql> insert into user values ('user1','123');
Query OK, 1 row affected (0.47 sec)

mysql> insert into user values ('user3','123');
Query OK, 1 row affected (0.10 sec)

mysql> select * from user;
+----------+----------+
| username | password |
+----------+----------+
| user2    | 123      |
| user1    | 123      |
| user3    | 123      |
+----------+----------+
3 rows in set (0.00 sec)

 

server4:

mysql> use test;
Database changed

mysql> select * from user;
+----------+----------+
| username | password |
+----------+----------+
| user2    | 123      |
| user1    | 123      |
| user3    | 123      |
+----------+----------+
3 rows in set (0.00 sec)

在server3上创建数据库 test,在test上创建table user,并插入数据,可以在server4上查看到server3中数据库 test,table user,及数据。

 

基于GTID模式

概念:GTID(Global Transaction Identifier)称为全局事务标示符, 是由mysql服务器自动管理的在原始master上提交事务时被创建。 GTID需要在全局的主-备拓扑结构 中保持唯一性,每一个 GTID 代表一个数据库事务。 GTID由两部分组成: GTID = source_id:transaction_id source_id: 用于标示源服务器,用server_uuid来表示 transaction_id: 则是根据在源服务器上第几个提交的事务来确定。 transaction_id 是一个从 1 开始的自增计数,表示在这个主库上执行的第 n 个事务。 MySQL 会保证事务与 GTID 之间的 1 : 1 映射。 工作原理:前面在slave端配置,进行change master to操作时, 使用的是日志号(指定position),当master端的服务down掉了, 就会在slave端选择一个日志号与原来的master最接 近的作为master, 但是,在另一个slave上,并没有指定新的master的信息, 因此还要手动去指定,而使用gtid的话,slave通过寻找 next的值, 并不用指定master的二进制日志文件和日志号,所以使用gtid更能保证数据的完整性。

 

Server3:

vim /etc/my.cnf ##开启gtid模式

gtid_mode=ON
enforce-gtid-consistency=true

 

/etc/init.d/mysqld restart   ###重启mysql

 

Server4:

vim /etc/my.cnf ##开启gtid模式 

gtid_mode=ON 
enforce-gtid-consistency=true 

 

/etc/init.d/mysqld restart 

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

mysql> change master to master_host='172.25.61.3',master_user='westos',master_password='MoShuai123!',MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.18 sec)

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.61.3
                  Master_User: westos
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 154
               Relay_Log_File: server4-relay-bin.000002
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

半同步复制配置

MySQL默认的复制即是异步的,主库在执行完客户端提交的事务后会立即将结果返给给客户端,并不关心从库是否已经接收并处理,这样就会有一个问题,主如果crash掉了,此时主上已经提交的事务可能并没有传到从上,如果此时,强行将从提升为主,可能导致新主上的数据不完整。

指当主库执行完一个事务,所有的从库都执行了该事务才返回给客户端。因为需要等待所有从库执行完该事务才能返回,所以全同步复制的性能必然会收到严重的影响。

介于异步复制和全同步复制之间,主库在执行完客户端提交的事务后不是立刻返回给客户端,而是等待至少一个从库接收到并写到relay log中才返回给客户端。相对于异步复制,半同步复制提高了数据的安全性,同时它也造成了一定程度的延迟,这个延迟最少是一个TCP/IP往返的时间。所以,半同步复制最好在低延时的网络中使用。 

Master:

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.16 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.00 sec)

slave:

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.07 sec)

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

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

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

mysql> 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)

master:

mysql> use westos;
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> insert into usertb value ('user8','123');        ##插入value
Query OK, 1 row affected (0.08 sec)

mysql> show status like '%semi_sync%';
+--------------------------------------------+-------+
| 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      | 457   |
| Rpl_semi_sync_master_tx_wait_time          | 457   |
| 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.01 sec)

Rpl_semi_sync_master_yes_tx ##使用半同步成功的次数,数据一致性性能提高

Rpl_semi_sync_master_no_tx  ##使用半同步失败的次数,10s后没有得到反馈信息,会转为异步复制

 

Slave:       ##将slave的半同步关掉

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

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

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

mysql> 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)

master:

mysql> insert into usertb value ('user9','123');
Query OK, 1 row affected (10.12 sec)       ##当把slave的半同步,master会等待10秒来接收slave的反馈,如果接收不到反馈,会自动切换异步复制
mysql> show status like '%semi_sync%';
+--------------------------------------------+-------+
| 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      | 457   |
| Rpl_semi_sync_master_tx_wait_time          | 457   |
| 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)

 

;