CentOS7-MySQL5.7.29搭建主主同步

一、主主模式

    也叫热备模式,通俗点讲,就是两台服务器都安装了MySQL,这两台服务器互为主备,A机器产生数据会同步到B机器,同理,B机器产生数据也会同步到A机器。

二、注意事项

    如果是虚拟机直接拷贝的服务器,要注意修改MySQL的uuid值,两台是不可以一样的

vi /usr/local/mysql/data/auto.cnf
# 随便修改其中一个数字或字母就行
# 修改保存后记得重启MySQL

    另外一个还需要注意的就是防火墙,要把MySQL的端口添加到防火墙里面,我这边直接关闭

# 查看防火墙状态,开启的话会有 active (running) 字样
systemctl status firewalld.service
# 关闭放获取,关闭的话会有 inactive (dead) 字样
systemctl stop firewalld.service

三、开始搭建

    1、环境

        系统:CentOS 7.0

        MySQL:5.7.29

        A服务器IP:192.168.74.132

        B服务器IP:192.168.74.140

    2、创建库 test

    A/B两台服务器分别创建数据库 test 和一张表 tb_mobile

# 创建库
create database test;
# 创建表
use test;
create table tb_mobile( mobile VARCHAR(20) comment'手机号码', time timestamp DEFAULT now() comment'时间' );

    3、在A服务器创建同步用户

# 创建用户
grant replication slave on *.* to 'demo'@'192.168.74.140' identified by '1qaz2wsx';
# 立即生效
flush privileges;

    4、在B服务器创建同步用户

# 创建用户
grant replication slave on *.* to 'demo'@'192.168.74.132' identified by '1qaz2wsx';
# 立即生效
flush privileges;

    5、在A/B服务器上分别用刚创建的demo用户远程登录

# 在A服务器上远程登录主机的MySQL
mysql -h192.168.74.140 -udemo -p

# 在B服务器上远程登录主机的MySQL
mysql -h192.168.74.132 -udemo -p

    6、修改A服务器的my.cnf文件

# ps:以前版本是没有 !includedir /etc/my.cnf.d 这一行的
 
[client]
default-character-set=utf8
socket=/usr/local/mysql/data/mysql.sock
[mysqld]
# 唯一ID标识,不可以相同
server-id = 1

# 打开binlog,打开该选项才可以通过I/O写到Slave的relay-log,也是可以进行replication的前提。
# MYSQL是基于二进制的日志来做同步的,每个日志文件默认大小为 1G
log-bin=mysql-bin

# 表示需要记录二进制日志的数据库。如果有多个数据可以用逗号分隔,或者使用多个binlog-do-dg选项
binlog-do-db = test

# 不需要记录二进制日志的数据库,如果有多个数据库可用逗号分隔,或者使用多binglog-ignore-db选项
binlog-ignore-db = mysql

# 用从属服务器上的日志功能
# 配置从库上的更新操作是否写入二进制文件,如果这台从库,还要做其他从库的主库,那么就需要打这个参数,以便从库的从库能够进行日志同步
log-slave-updates

# 日志写操作就把日志文件写入硬盘一次(对日志信息进行一次同步)。n=1是最安全的做法,但效率最低。默认设置是n=0
# 详细解释查看文章最下面
sync_binlog = 1

# auto_increment,控制自增列AUTO_INCREMENT的行为
# 用于MASTER-MASTER之间的复制,防止出现重复值
# auto_increment_offset=1设置步长,这里设置为1,这样Master的auto_increment字段产生的数值是:1, 3, 5, 7, …等奇数ID
# 设置2就是2、4、6、8等
auto_increment_offset = 1

# auto_increment_increment=n有多少台服务器,n就设置为多少
auto_increment_increment = 2

# 进行镜像处理的数据库
# 表示需要同步的数据库,如果有多个数据可用逗号分隔,或者使用多个replicate-do-db选项
replicate-do-db = test

# 不进行镜像处理的数据库
# 表示不需要同步的数据库,如果有多个数据库可用逗号分隔,或者使用多个replicate-ignore-db选项
replicate-ignore-db = mysql,information_schema

# MySQL端口
port=3306

# MySQL安装目录
basedir=/usr/local/mysql

# MySQL数据存放目录
datadir=/usr/local/mysql/data

# socket 文件是在 Unix/Linux 环境下才有的,用户在 Unix/Linux 环境下客户端连接可以不通过 TCP/IP 网络而直接使用 Unix Socket 来连接MySQL
socket=/usr/local/mysql/data/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
 
[mysqld_safe]

# MySQL的错误日志文件
log-error=/usr/local/mysql/mysql-log/error.log

# pid file 是 mysqld 应用程序在 Unix/Linux 环境下的一个进程文件,和许多其他Unix/Linux 服务端程序一样,存放着自己的进程 id
pid-file=/usr/local/mysql/data/mysql.pid
 
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

    7、修改B服务器的my.cnf文件

[client]
default-character-set=utf8
socket=/usr/local/mysql/data/mysql.sock
[mysqld]
# 不能和主机一样
server-id = 2
log-bin=mysql-bin
replicate-do-db = test
replicate-ignore-db = mysql,information_schema,performance_schema
binlog-do-db = test
binlog-ignore-db = mysql
log-slave-updates
sync_binlog = 1
auto_increment_offset = 2
auto_increment_increment = 2
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
 
[mysqld_safe]
log-error=/usr/local/mysql/mysql-log/error.log
pid-file=/usr/local/mysql/data/mysql.pid
 
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

    8、分别重启A/B两台服务器的MySQL

    9、在A/B两台服务器上执行锁定数据库表操作,防止加入新的数据

flush tables with read lock;

    10、查看A/B两台服务器上的 master 状态

show master status\G;
 
 
 
# 结果如下,记录一下 File 参数 和 Position 参数
# 这里我只展示一个,结构都是一样的,以自己查出来的为准
mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 154
     Binlog_Do_DB: test
 Binlog_Ignore_DB: mysql
Executed_Gtid_Set: 
1 row in set (0.00 sec)

    11、分别在A/B两台服务器上执行 change master to 指令指定同步位置

# 在A服务器上执行

# 先关闭 slave
stop slave;
 
# 设置连接
change master to master_host='192.168.74.140',master_user='demo',master_password='1qaz2wsx',master_log_file='mysql-bin.000001',master_log_pos=154;
 
# 再开启 slave
start slave;

-------------------------------------------------------------------------------- 

# 在B服务器上执行

# 先关闭 slave
stop slave;
 
# 设置连接
change master to master_host='192.168.74.132',master_user='demo',master_password='1qaz2wsx',master_log_file='mysql-bin.000001',master_log_pos=154;
 
# 再开启 slave
start slave;

--------------------------------------------------------------------------------
 
# 如果遇到报错,先关闭
stop slave
# 重置 slave
reset slave
# 再重复前面 关闭-设置连接-开启 的步骤

    12、分别在A/B服务器上查看slave 的状态

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.74.132
                  Master_User: demo
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
 
# 主要是这两个 yes 代表通了
 
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql,information_schema,performance_schema
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 531
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: be8b52d3-7f04-11ea-a454-000c29e3bb87
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

    13、分别在A/B两台服务器上解锁

unlock tables;

四、开始同步测试

    1、分别在A/B服务器上添加数据

# 这里只写一个,剩下的自己写

mysql> use test;
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> 
mysql> 
mysql> 
mysql> insert into tb_mobile values('1111',now());
Query OK, 1 row affected (0.01 sec)
 
mysql> select * from tb_mobile;
+--------+---------------------+
| mobile | time                |
+--------+---------------------+
| 1111   | 2020-04-16 22:06:46 |
+--------+---------------------+
1 row in set (0.00 sec)

    2、分别在A/B两台服务器上查看数据有没有同步过来

mysql> use test;
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 tb_mobile;
+--------+---------------------+
| mobile | time                |
+--------+---------------------+
| 1111   | 2020-04-16 22:06:46 |
+--------+---------------------+
1 row in set (0.00 sec)

    3、同步成功,结束

 

五、名词解释

    1、Sync_binlog=1 Or N

Sync_binlog的默认值是0,这种模式下,MySQL不会同步到磁盘中去。这样的话,Mysql依赖操作系统来刷新二进制日志binary log,就像操作系统刷新其他文件的机制一样。因此如果操作系统或机器(不仅仅是Mysql服务器)崩溃,有可能binlog中最后的语句丢失了。要想防止这种情况,可以使用sync_binlog全局变量,使binlog在每N次binlog写入后与硬盘同步。当sync_binlog变量设置为1是最安全的,因为在crash崩溃的情况下,你的二进制日志binary log只有可能丢失最多一个语句或者一个事务。但是,这也是最慢的一种方式(除非磁盘有使用带蓄电池后备电源的缓存cache,使得同步到磁盘的操作非常快)。

即使sync_binlog设置为1,出现崩溃时,也有可能表内容和binlog内容之间存在不一致性。如果使用InnoDB表,Mysql服务器处理COMMIT语句,它将整个事务写入binlog并将事务提交到InnoDB中。如果在两次操作之间出现崩溃,重启时,事务被InnoDB回滚,但仍然存在binlog中。可以用-innodb-safe-binlog选项来增加InnoDB表内容和binlog之间的一致性。(注释:在Mysql 5.1版本中不需要-innodb-safe-binlog;由于引入了XA事务支持,该选项作废了),该选项可以提供更大程度的安全,使每个事务的binlog(sync_binlog=1)和(默认情况为真)InnoDB日志与硬盘同步,该选项的效果是崩溃后重启时,在滚回事务后,Mysql服务器从binlog剪切回滚的InnoDB事务。这样可以确保binlog反馈InnoDB表的确切数据等,并使从服务器保持与主服务器保持同步(不接收回滚的语句)。

    2、Auto_increment_offset和Auto_increment_increment

Auto_increment_increment和auto_increment_offset用于主-主服务器(master-to-master)复制,并可以用来控制AUTO_INCREMENT列的操作。两个变量均可以设置为全局或局部变量,并且假定每个值都可以为1到65,535之间的整数值。将其中一个变量设置为0会使该变量为1。

这两个变量影响AUTO_INCREMENT列的方式:auto_increment_increment控制列中的值的增量值,auto_increment_offset确定AUTO_INCREMENT列值的起点。

如果auto_increment_offset的值大于auto_increment_increment的值,则auto_increment_offset的值被忽略。例如:表内已有一些数据,就会用现在已有的最大自增值做为初始值。

    3、Master-connect-retry

master-connect-retry=n表示从服务器与主服务器的连接没有成功,则等待n秒(s)后再进行管理方式(默认设置是60s)。如果从服务器存在mater.info文件,它将忽略些选项。

   4、参考文档

# 解释 Replication 概念
https://blog.csdn.net/francis123580/article/details/80809596

# 参考的双机搭建文章和名词解释
https://www.cnblogs.com/jianmingyuan/p/10903682.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章