Mysql主从配置
MySql安装在mysql专栏在上一节
-
Master节点
-
修改my.cnf文件
vi /etc/my.cnf #bin_log配置 log_bin=mysql-bin server-id=1 sync-binlog=1 binlog-ignore-db=information_schema binlog-ignore-db=mysql binlog-ignore-db=performance_schema binlog-ignore-db=sys #relay_log配置 relay_log=mysql-relay-bin log_slave_updates=1 relay_log_purge=0
-
重启mysql
systemctl restart mysqld
-
进入mysql,主库给从库授权(要在每一台mysql中执行,因为宕机漂移后不一定哪台机器会变成主,所以都需要添加权限。)
mysql> grant replication slave on *.* to root@'%' identified by '上面设定的密码'; mysql> grant all privileges on *.* to root@'%' identified by '上面设定的密码'; mysql> flush privileges; #查看主库状态信息,例如master_log_file='mysql-bin.000007',master_log_pos=154 mysql> show master status
-
-
Slave
-
修改my.cnf(这里注意需要将server-id修改,不能相同)
#bin_log配置 log_bin=mysql-bin #服务器ID,从库1是2,从库2是3 server-id=2 sync-binlog=1 binlog-ignore-db=information_schema binlog-ignore-db=mysql binlog-ignore-db=performance_schema binlog-ignore-db=sys #relay_log配置 relay_log=mysql-relay-bin log_slave_updates=1 relay_log_purge=0 read_only=1
-
重启服务
systemctl restart mysqld
-
进入从库,开启同步
#master_log_file是主库状态信息中的日志文件,master_log_pos是主库状态信息的指针 mysql> change master to master_host='192.168.0.199',master_port=3306,master_user='root',master_password ='root',master_log_file='mysql-bin.000001',master_log_pos=869; #开启同步 start slave;
-
-
配置半同步复制:
-
Master
-
登陆mysql,执行:
#安装 install plugin rpl_semi_sync_master soname 'semisync_master.so'; #查看信息 show variables like '%semi%';
-
退出mysql修改/etc/my.cnf文件,并且重启mysql
# 自动开启半同步复制 rpl_semi_sync_master_enabled=ON rpl_semi_sync_master_timeout=1000
-
-
Slave(所有slave都要执行)
-
在mysql中执行
install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
-
退出mysql修改/etc/my.cnf文件,并且重启mysql
# 自动开启半同步复制 rpl_semi_sync_slave_enabled=ON
-
-
测试半同步
-
首先通过mysql命令查看半同步是否开启
show variables like '%semi%';
-
继续通过MySQL日志再次确认
cat /var/log/mysqld.log
日志中一下信息则证明半同步有效
Start semi-sync binlog_dump to slave (server_id: 2), pos(mysql-bin.000002, 154
-
-