文章目录
- 前言
- 一、mysql源码编译安装
- 二、主从复制
- 1.主从复制的作用、原理
- 2.实验过程
- 三、gtid模式
- 四、半同步模式:优化IO线程
- 五、并行复制/多线程复制:优化SQL线程
- 六、延迟复制
前言
mysql是现在普遍使用的数据库,但是如果宕机了必然会造成数据丢失。为了保证mysql数据库的可靠性。就要会一些提高可靠性的技术。
为什么要主从复制、读写分离
主从复制、读写分离一般是一起使用的。目的很简单,就是为了提高数据库的并发性能。你想,假设是单机,读写都在一台MySQL上面完成,性能肯定不高。如果有三台MySQL,一台mater只负责写操作,两台salve只负责读操作,性能不就能大大提高了吗?
所以主从复制、读写分离就是为了数据库能支持更大的并发。
随着业务量的扩展、如果是单机部署的MySQL,会导致I/O频率过高。采用主从复制、读写分离可以提高数据库的可用性。
一、mysql源码编译安装
官网: www.mysql.com
下载之后,解压编译安装
[root@server1 ~]# tar xf mysql-boost-5.7.40.tar.gz
[root@server1 ~]# cd mysql-5.7.40/
需要使用cmake进行mysql的编译安装,所以我们先下载好cmake、gcc-c++
然后用cmake、make、make install来编译安装,在这个过程中我们可以看到进度百分比,这是比之前方便的地方
[root@server1 mysql-5.7.40]# yum install -y cmake gcc-c++ bsion
[root@server1 mysql-5.7.40]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_EXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_unicode_ci -DWITH_BOOST=/root/mysql-5.7.40/boost/boost_1_59_0
[root@server1 mysql-5.7.40]# make
也可以用make -j2编译,意思为起两个进程一起编译
[root@server1 mysql-5.7.40]# make install
拷贝启动脚本
[root@server1 mysql-5.7.40]# cd /usr/local/mysql/
[root@server1 mysql]# cd support-files/
[root@server1 support-files]# cp mysql.server /etc/init.d/mysqld
修改mysql配置文件
初始化数据库
[root@server1 ~]# mkdir /data/mysql -p
创建仅用于启动mysql的用户;-M默认不创建用户主目录,-s给不给交互式权限
[root@server1 ~]# useradd -M -d /data/mysql/ -s /sbin/nologin mysql
用mysql的用户的身份写数据
[root@server1 ~]# chown mysql.mysql /data/mysql/
方便调用命令
[root@server1 ~]# source .bash_profile
mysqld是后台参数,数据库启动前必须做初始化
[root@server1 ~]# mysqld --initialize --user=mysql
启动服务
[root@server1 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to ‘/data/mysql/server1.err’.
SUCCESS!
[root@server1 ~]# netstat -antlp|grep :3306
tcp6 0 0 :::3306 ::😗 LISTEN 13856/mysqld
安全初始化
[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.
#是否删除匿名用户,输入 y
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.
#是否禁用root远程登录,输入 y
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.
#是否删除test数据库,输入 y
- 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!
登录数据库
[root@server1 ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.40 Source distribution
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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 databases; ##显示库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
二、主从复制
1.主从复制的作用、原理
作用:
做数据的热备,作为后备数据库,主数据库服务器故障后,可切换到从数据库继续工作,避免数据丢失。
架构的扩展。业务量越来越大,I/O访问频率过高,单机无法满足,此时做多库的存储,降低磁盘I/O访问的评率,提高单个机器的I/O性能。
读写分离,使数据库能支持更大的并发。在报表中尤其重要。由于部分报表sql语句非常的慢,导致锁表,影响前台服务。如果前台使用master,报表使用slave,那么报表sql将不会造成前台锁,保证了前台速度。
1–在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力;(主库写,从库读,降压)
2–在从主服务器进行备份,避免备份期间影响主服务器服务;(确保数据安全)
3–当主服务器出现问题时,可以切换到从服务器。(提升性能)
原理:
当Master节点进行insert、update、delete操作时,会按顺序写入到binlog中。
salve从库连接master主库,Master有多少个slave就会创建多少个binlog dump线程。
当Master节点的binlog发生变化时,binlog dump 线程会通知所有的salve节点,并将相应的binlog内容推送给slave节点。
I/O线程接收到 binlog 内容后,将内容写入到本地的 relay-log,即中继日志,持久化到磁盘。
SQL线程读取I/O线程写入的relay-log,并且根据 relay-log 的内容对从数据库做对应的操作,即回放。
2.实验过程
server2直接建立为slave,server3先演示数据库的备份,在做成slave;
master配置
参数从官方网站复制而来;以下为生成二进制文件
重启服务
[root@server1 ~]# /etc/init.d/mysqld restart
创建复制用户
[root@server1 ~]# mysql -pwestos
mysql> CREATE USER ‘repl’@‘%’ IDENTIFIED BY ‘westos’; ##创建专门用于复制的用户repl,允许从除了localhost之外的所有主机,密码为westos
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON . TO ‘repl’@‘%’; ##repl用户可以从任何主机登录,具备所有库所有表的复制权限
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
±-----------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±-----------------±---------±-------------±-----------------±------------------+
| mysql-bin.000001 | 595 | | | | ##mysql-bin.000001生成的二进制日志
±-----------------±---------±-------------±-----------------±------------------+
1 row in set (0.00 sec)
slave配置
新建数据库实例server2
[root@server1 ~]# scp -r /usr/local/mysql/ server2:/usr/local/
[root@server2 ~]# vim .bash_profile(和server1步骤一致)
[root@server2 ~]# source .bash_profile
[root@server2 ~]# cd /usr/local/mysql
[root@server2 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@server2 mysql]# useradd -M -d /data/mysql/ -s /sbin/nologin mysql
[root@server2 mysql]# mkdir -p /data/mysql
[root@server2 mysql]# chown mysql.mysql /data/mysql/
[root@server2 ~]# vim /etc/my.cnf【数据从master,不需要激活二进制】
初始化数据库
[root@server2 mysql]# mysqld --initialize --user=mysql
开启数据库
[root@server2 mysql]# /etc/init.d/mysqld start
安全初始化
[root@server2 mysql]# mysql_secure_installation
测试
测试数据同步(仅在server2测试,server3的设置和测试在后面)
master创建测试数据库
[root@server1 ~]# mysql -pwestos
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database westos; ##创建westos库
Query OK, 1 row affected (0.00 sec)
mysql> use westos;
mysql> create table user_tb ( ##创建user_tb表
-> username varchar(25) not null,
-> password varchar(50) not null);
Query OK, 0 rows affected (0.00 sec)
mysql> desc user_tb;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(25) | NO | | NULL | |
| password | varchar(50) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> insert into user_tb values ('user1','123'); ##插入数据
Query OK, 1 row affected (0.00 sec)
mysql> insert into user_tb values ('user2','456'); ##插入数据
Query OK, 1 row affected (0.00 sec)
slave server2查看数据:
[root@server2 mysql]# mysql -pwestos
mysql> show databases; ##westos库已同步
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| westos |
+--------------------+
5 rows in set (0.00 sec)
mysql> use westos ##进入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> show tables; ##显示表
+------------------+
| Tables_in_westos |
+------------------+
| user_tb |
+------------------+
1 row in set (0.00 sec)
mysql> select * from user_tb; ##查看表内数据,已同步
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
| user2 | 456 |
+----------+----------+
2 rows in set (0.01 sec)
再添加一个slave server3
[root@server1 ~]# scp -r /usr/local/mysql/ server3:/usr/local/
[root@server3 ~]# vim .bash_profile
[root@server3 ~]# source .bash_profile
[root@server3 ~]# mkdir -p /data/mysql
[root@server3 ~]# useradd -M -d /data/mysql/ -s /sbin/nologin mysql
[root@server3 ~]# chown mysql.mysql /data/mysql/
[root@server3 ~]# vim /etc/my.cnf
[root@server3 ~]# mysqld --initialize --user=mysql
[root@server3 ~]# cd /usr/local/mysql/
[root@server3 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@server3 mysql]# /etc/init.d/mysqld start
[root@server3 mysql]# mysql_secure_installation
master备份数据库
[root@server1 ~]# mysqldump -pwestos westos > dump.sql
[root@server1 ~]# scp dump.sql server3:
注意:
生产环境中备份时master主机需要锁表,保证备份前后的数据一致
注意:
mysqldump命令备份的数据文件,在还原时先DROP TABLE,需要合并数据时需要删除此语句,要不然会造成数据丢失
[root@server1 ~]# mysql -pwestos
mysql> show master status;
±-----------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±-----------------±---------±-------------±-----------------±------------------+
| mysql-bin.000001 | 1534 | | | |
±-----------------±---------±-------------±-----------------±------------------+
1 row in set (0.00 sec)
salve server3还原数据库、并设为slave
[root@server3 ~]# mysqladmin -pwestos create westos
[root@server3 ~]# mysql -pwestos westos < dump.sql
[root@server3 ~]# mysql -pwestos
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| westos |
+--------------------+
5 rows in set (0.00 sec)
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> select * from user_tb;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
| user2 | 456 |
+----------+----------+
2 rows in set (0.00 sec)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.56.11', MASTER_USER='repl', MASTER_PASSWORD='westos', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=1534; ##因为数据已经备份,MASTER_LOG_POS=1534号比较server2有改变,从1534开始复制即可
Query OK, 0 rows affected, 2 warnings (0.01 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: 192.168.56.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1534
Relay_Log_File: server3-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
测试主从同步:
一主两从架构完成,更新master,slave的数据会同步;复制是单向的,只能在master上操作
[root@server1 ~]# mysql -pwestos
mysql> insert into westos.user_tb values (‘user3’,‘666’);
Query OK, 1 row affected (0.00 sec)
[root@server2 mysql]# mysql -pwestos
mysql> select * from westos.user_tb;
±---------±---------+
| username | password |
±---------±---------+
| user1 | 123 |
| user2 | 456 |
| user3 | 666 |
±---------±---------+
3 rows in set (0.00 sec)
[root@server3 mysql]# mysql -pwestos
mysql> select * from westos.user_tb;
±---------±---------+
| username | password |
±---------±---------+
| user1 | 123 |
| user2 | 456 |
| user3 | 666 |
±---------±---------+
3 rows in set (0.00 sec)
三、gtid模式
gtid,让我们在设置主从模式时,降低了复杂度,更加方便,尤其是主从切换时更加方便
GTID概念介绍
GTID即全局事务ID (global transaction identifier), 其保证为每一个在主上提交的事务在复制集群中可以生成一个唯一的ID。GTID最初由google实现,官方MySQL在5.6才加入该功能。mysql主从结构在一主一从情况下对于GTID来说就没有优势了,而对于2台主以上的结构优势异常明显,可以在数据不丢失的情况下切换新主。使用GTID需要注意: 在构建主从复制之前,在一台将成为主的实例上进行一些操作(如数据清理等),通过GTID复制,这些在主从成立之前的操作也会被复制到从服务器上,引起复制失败。也就是说通过GTID复制都是从最先开始的事务日志开始,即使这些操作在复制之前执行。比如在server1上执行一些drop、delete的清理操作,接着在server2上执行change的操作,会使得server2也进行server1的清理操作。
GTID实际上是由UUID+TID (即transactionId)组成的。其中UUID(即server_uuid) 产生于auto.conf文件(cat /data/mysql/data/auto.cnf),是一个MySQL实例的唯一标识。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增,所以GTID能够保证每个MySQL实例事务的执行(不会重复执行同一个事务,并且会补全没有执行的事务)。GTID在一组复制中,全局唯一。
gtid在官网位置
master配置:
[root@server1 mysql]# vim /etc/my.cnf
...
log-bin=mysql-bin
server-id=1
gtid_mode=ON ##gtid模式开启
enforce-gtid-consistency=ON ##强制使用gtid模式
重启服务
[root@server1 mysql]# /etc/init.d/mysqld restart
slave配置:server2和server3配置一致
[root@server2 ~]# vim /etc/my.cnf
...
server-id=2
gtid_mode=ON
enforce-gtid-consistency=ON
重启服务
[root@server2 ~]# /etc/init.d/mysqld restart
[root@server2 ~]# mysql -pwestos
#首先停止slave
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
#重新配置
mysql> CHANGE MASTER TO MASTER_HOST='192.168.56.11', MASTER_USER='repl', MASTER_PASSWORD='westos', MASTER_AUTO_POSITION = 1;
##MASTER_AUTO_POSITION = 1:自动
Query OK, 0 rows affected, 2 warnings (0.00 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: 192.168.56.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes ##出现yes即可
Slave_SQL_Running: Yes ##出现yes即可
概念:
异步:
一般情况下MySQL默认复制模式为异步,何为异步?简单的说就是主服务器上的I/O threads 将binlog写入二进制日志中就返回给客户端一个结果,无需等待二进制日志是否成功发送到从库和从库上是否成功完成relay log的写入以及SQL threads从relay log中提取二进制日志写入自己binlog的过程,异步模式的缺点就是一旦主库写入binlog日志后发生宕机,此时从库还未完成对主库传送过来binlog的读写存储操作,那么从库就会发生数据丢失的现象。所以这里就出现了另外一种复制模式,即半同步模式。
半同步:
在半同步模式下,主库将binlog日志信息写入到日志中,然后等待从库完成后面的接收读取和存储操作,并返回主库一个明确信息,即从库接收完主库传递过来的binlog内容已经写入到自己的relay log中,才会通知主库上的等待线程,该操作已经完成,此时主库接收到从库发过来的信息就会进入下一个同步操作动作,如果在此时等待超时,超过主库中设置的超时时间限制(一般是rpl_semi_sync_master_timeout的值),则关闭半同步,并切换为异步模式,直到至少有一台从库告诉主库已经接收到主库传送过来的binlog信息为止。
四、半同步模式:优化IO线程
半同步模式必须走gtid
在异步复制已设置完成的前提下,继续配置半同步复制。半同步复制是使用插件实现的,所以插件必须安装到服务器才能使用
master发送日志后,等待slave回复ACK
master配置:
#安装半同步模块
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT PLUGIN_NAME, PLUGIN_STATUS
-> FROM INFORMATION_SCHEMA.PLUGINS
-> WHERE PLUGIN_NAME LIKE '%semi%'; ##查看节点是否安装半同步模块
+----------------------+---------------+
| PLUGIN_NAME | PLUGIN_STATUS |
+----------------------+---------------+
| rpl_semi_sync_master | ACTIVE |
+----------------------+---------------+
1 row in set (0.00 sec)
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1; #激活master半同步模式
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'rpl_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 | ##1个slave数量回应就可以进行引擎提交
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_master_wait_point | AFTER_SYNC |
+-------------------------------------------+------------+
6 rows in set (0.00 sec)
mysql> SHOW STATUS LIKE 'Rpl_semi_sync%'; #查看半同步状态
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 2 | ##2个slave
| 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)
半同步参数写入配置文件,确保重启后依然生效
[root@server1 mysql]# vim /etc/my.cnf
...
log-bin=mysql-bin
server-id=1
gtid_mode=ON
enforce-gtid-consistency=ON
rpl_semi_sync_master_enabled=1 ##保证重启后依然生效1=on
slave配置:
安装半同步模块
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1;
Query OK, 0 rows affected (0.00 sec)
mysql> STOP SLAVE IO_THREAD; #需要重启IO线程,slave端的半同步才能生效
Query OK, 0 rows affected (0.00 sec)
mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'rpl_semi_sync%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
2 rows in set (0.00 sec)
mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.00 sec)
#半同步参数写入配置文件
[root@server2 ~]# vim /etc/my.cnf
server-id=2
gtid_mode=ON
enforce-gtid-consistency=ON
rpl_semi_sync_slave_enabled=1 ##确保重启生效
测试
master写入数据:
mysql> insert into westos.user_tb values ('user6','666');
Query OK, 1 row affected (0.01 sec)
mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';
+--------------------------------------------+-------+
| 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 | 2 |
| 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 | 2096 |
| Rpl_semi_sync_master_tx_wait_time | 2096 |
| 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 | #显示通过半同步复制的数据有1笔
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
停止所有slave节点的IO线程:
mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
master节点再次写入数据:
mysql> insert into westos.user_tb values ('user7','666');
Query OK, 1 row affected (10.00 sec)
##等待默认超时时间10秒后,mysql自动切换为异步模式;实际生产环境,超时时间为无穷大。
mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';
+--------------------------------------------+-------+
| 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 | 4 |
| Rpl_semi_sync_master_no_times | 1 |
| Rpl_semi_sync_master_no_tx | 1 | #显示未通过半同步模式复制的数据有1笔
| Rpl_semi_sync_master_status | OFF | #半同步模式关闭
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 2096 |
| Rpl_semi_sync_master_tx_wait_time | 2096 |
| 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)
所有slave节点再次启动IO线程,mysql会自动切回半同步模式
mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
五、并行复制/多线程复制:优化SQL线程
在slave端设置,只能降低延时,不能消除延时
默认slave节点sql单线程回放,会造成数据同步延时较高
slave节点添加以下参数
[root@server2 ~]# vim /etc/my.cnf
…
slave-parallel-type=LOGICAL_CLOCK###基于组提交
slave-parallel-workers=16###启用16个work,根据实际需求修改
master_info_repository=TABLE ###记录master信息至表(默认是file)
relay_log_info_repository=TABLE###记录relay信息至表(默认是file)
relay_log_recovery=ON
重启服务
[root@server2 ~]# /etc/init.d/mysqld restart
Shutting down MySQL… SUCCESS!
Starting MySQL. SUCCESS!
[root@server2 ~]# mysql -pwestos
可以看到多开了16个线程
六、延迟复制
延迟复制可以设置副本服务器故意落后于源至少指定的时间
STOP SLAVE SQL_THREAD;
CHANGE MASTER TO MASTER_DELAY=60; ##延迟60秒
START SLAVE SQL_THREAD;