基于Linux环境配置MySQL双主实践整理

/ Database / 没有评论 / 1413浏览

环境

masterA 10.0.0.5 (数据库A)

masterB 10.0.0.6 (数据库B)

注意: 两台机器在同一网络,防火墙关闭,本配置5.7 5.6版本一样适用

安装MYSQL

分别安装两台数据库,且成功启动

配置文件

masterA

# 除了必须的一些配置,还需要加入的
#主主复制模块
log-bin=mysql-bin 
server-id=1                  #这里是唯一的
#binlog-do-db=mydb            #需要记录进制日志的数据库.如果有多个数据库可用逗号分隔,或者使用多个binlog-do-db选项 

#binlog-ignore-db=mysql     #不需要记录进制日志的数据库.如果有多个数据库可用逗号分隔,或者使用多个binlog-do-db选项 
#replicate-do-db=mydb        #需要进行同步的数据库.如果有多个数据库可用逗号分隔,或者使用多个replicate-do-db选项 
#binlog-ignore-db=mysql      #不需要同步的数据库.如果有多个数据库可用逗号分隔,或者使用多个replicate-ignore-db选项 
#同步参数: 
#保证slave挂在任何一台master上都会接收到另一个master的写入信息
#
log-slave-updates=on
sync_binlog=1 
auto_increment_offset=2 
auto_increment_increment=2 
slave-skip-errors=all             #过滤掉一些没啥大问题的错误

masterB

 master B 除了必须的一些配置,还需要加入的
 
log-bin=mysql-bin 
server-id=2                   #这里必须是唯一的
#binlog-do-db=mydb            #需要记录进制日志的数据库.如果有多个数据库可用逗号分隔,或者使用多个binlog-do-db选项 

#binlog-ignore-db=mysql     #不需要记录进制日志的数据库.如果有多个数据库可用逗号分隔,或者使用多个binlog-do-db选项 
#replicate-do-db=mydb        #需要进行同步的数据库.如果有多个数据库可用逗号分隔,或者使用多个replicate-do-db选项 
#binlog-ignore-db=mysql      #不需要同步的数据库.如果有多个数据库可用逗号分隔,或者使用多个replicate-ignore-db选项 
#同步参数: 
#保证slave挂在任何一台master上都会接收到另一个master的写入信息
#
log-slave-updates =on
sync_binlog=1 
auto_increment_offset=2 
auto_increment_increment=2 
slave-skip-errors=all             #过滤掉一些没啥大问题的错误

建立账号

两个数据库分别执行

masterA

 grant replication slave on *.* to 'repl'@'%' identified by '123456';
 flush  privileges;

masterB

 grant replication slave on *.* to 'repl'@'%' identified by '123456';
 flush  privileges;

分别找出那个点

show  master status \G

#比如
mysql> show master status \G
*************************** 1. row ***************************
           File: mysql-bin.000019   #File后面
       Position: 191                #Position后面
   Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.00 sec)

主从开始连接

masterA

stop slave;

change master to master_host='10.0.0.6',master_user='repl',master_password='123456',master_log_file='mysql-bin.000020',master_log_pos=574;
start slave;

masterB

stop slave;

change master to master_host='10.0.0.5',master_user='repl',master_password='123456',master_log_file='mysql-bin.000020',master_log_pos=574;
start slave;

查看

show slave status  \G

主要看以下两个参数:

   Slave_IO_Running: Yes
   Slave_SQL_Running: Yes

测试下

两边分别新建一个库,看对面能不能同步这个操作

可能会出现的问题

  1. 主从账号建立连接的时候,有必要测试下从matserA机器 远程连接masterB机器上的数据库
  2. 建立账号需要注意权限,网段分配,如有必要需要测试下该账号能否连接上,像这样
[root@local var]# mysql -urepl -p123456 -h10.0.0.6
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.56-log Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

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>               #成功连接