配置环境:rhel6.5 iptables selinx down
172.25.40.1 server1.example.com master
172.25.40.2 server2.example.com slave
安装包:mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
在master和slave上解压后安装mysql数据库:
tar xf mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
yum install -y 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
/etc/init.d/mysqld start
grep ‘password‘ /var/log/mysqld.log
``` #筛选初始化是数据库登陆密码
登陆数据库后修改密码:
`alter user root@localhost identified by ‘xxxxxxx‘;` ##修改本地用户密码
1.Master端的操作:
vim /etc/my.cnf
[mysqld]
server-id=1
log-bin=mysql-bin
gtid_mode=ON #使用gtid模式
enforce-gtid-consistency=true #强制使用gtid
Slave端的操作:
[mysqld]
server-id=2
gtid_mode=ON
enforce-gtid-consistency=true
注意:在修改两台服务器的mysql配置文件时,一定要保证server-id必须是不同的,server-id的取值范围是:2^32-1
2.重新启动两台服务器上的mysql
` /etc/init.d/mysqld restart`
3.进入主库创建认证用户并授权
`grant replication slave on *.* to redhat@‘172.25.40.%‘ identified by ‘xxxxxxxxxx‘;`
4.进入slave端指定master
stop slave;
change master to master_host=‘172.25.40.1‘,master_user=‘redhat‘,master_password=‘xxxxxxxxxx‘,master_auto_position=1;
start slave;
5.在设置半同步复制master端的操作
mysql> install plugin rpl_semi_sync_master soname ‘semisync_master.so‘;
Query OK, 0 rows affected (0.50 sec)
mysql> set global rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables like ‘%rpl_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_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)
6.5.在设置半同步复制slave端的操作
mysql> install plugin rpl_semi_sync_slave soname ‘semisync_slave.so‘;
Query OK, 0 rows affected (0.14 sec)
mysql> set global rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables like ‘%rpl_semi%‘;
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
2 rows in set (0.00 sec)
如果在slave端开启io线程后,会自动调转为半同步模式进行数据传输
![](http://i2.51cto.com/images/blog/201805/02/7687411e99cbec08048536a5589ece39.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
关闭io线程 在master上再进行事务时会等待10s后从半同步状态转为异步。
![](http://i2.51cto.com/images/blog/201805/02/3f2914d044166abddd25f04bacc788e1.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
当第二次进行数据插入时会变成异步同步
![](http://i2.51cto.com/images/blog/201805/02/21df15b4774e91b721b9856d9355ea15.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
Mysql 并行复制
多线程工作
Slave端修改配置文件:
[mysqld]
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=16 #开启16个线程工作
master_info_repository=TABLE #以表的形式存放master_info的信息
relay_log_info_repository=TABLE #以表的形式存放relay_log_info的信息
relay_log_recovery=ON #
重启服务
`/etc/init.d/mysqld restart`
当进入数据库的mysql库中,可以执行sql语句:
`select * from slave_master_info;`
可以看到原本在/var/lib/mysql下的relay-log.info的文件已经转为数据库中的表。
原文地址:http://blog.51cto.com/12183531/2110349