centos7上配置mysql8的双主互写

Posted lavezhang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7上配置mysql8的双主互写相关的知识,希望对你有一定的参考价值。

注意:
1、主库1:10.1.131.75,主库2:10.1.131.76
2、server-id必须是纯数字,并且主从两个server-id在局域网内要唯一。

【主节点1】
vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=2019001
log_slave_updates=1

重启服务
service mysqld restart

【主节点2】
vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=2019002
log_slave_updates=1

重启服务
service mysqld restart

【主节点1】
mysql> CREATE USER ‘repl‘@‘10.1.131.76‘ IDENTIFIED WITH mysql_native_password BY ‘XXXXXX‘;

mysql> GRANT REPLICATION SLAVE ON *.* TO ‘repl‘@‘10.1.131.76‘;
mysql> flush privileges;

获取主节点当前binary log文件名和位置(position)
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 155 | | | |
+------------------+----------+--------------+------------------+-------------------+

【主节点2】
mysql> CHANGE MASTER TO MASTER_HOST=‘10.1.131.75‘,MASTER_USER=‘repl‘,MASTER_PASSWORD=‘XXXXXX‘,MASTER_LOG_FILE=‘mysql-bin.000001‘,MASTER_LOG_POS=155;


【主节点2】
mysql> CREATE USER ‘repl‘@‘10.1.131.75‘ IDENTIFIED WITH mysql_native_password BY ‘XXXXXX‘;

mysql> GRANT REPLICATION SLAVE ON *.* TO ‘repl‘@‘10.1.131.75‘;
mysql> flush privileges;

获取主节点当前binary log文件名和位置(position)
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 178 | | | |
+------------------+----------+--------------+------------------+-------------------+

【主节点1】
mysql> CHANGE MASTER TO MASTER_HOST=‘10.1.131.76‘,MASTER_USER=‘repl‘,MASTER_PASSWORD=‘XXXXXX‘,MASTER_LOG_FILE=‘mysql-bin.000001‘,MASTER_LOG_POS=178;

【主节点2】
开启主从同步
mysql> start slave;

再查看主从同步状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.131.75
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 155
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 322
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

【主节点1】
开启主从同步
mysql> start slave;

再查看主从同步状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.131.76
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 178
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 322
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

注意看Slave_IO_Running和Slave_SQL_Running,必须都是YES,如果Slave_IO_Running是No,通常都是server-id配置有问题。

以上是关于centos7上配置mysql8的双主互写的主要内容,如果未能解决你的问题,请参考以下文章

Mysql5.7.22+Keepalived双主互备高可用集群

配置MySQL实现主主互备模式并利用keepalived实现双主高可用

keepalived双主互备和自定义日志

MySQL双主互备模式架构

Mysql双主互备+keeplived高可用架构(部分)

keepalived实现mysql双主架构