CentOS7-MySQL5.7.29搭建主备同步

一、主备模式

    也叫冷备模式,通俗点讲,就是两台服务器都安装了MySQL,一台当作主服务器,一台当作备服务器,备服务器会把主服务器的MySQL数据同步过来,当主服务器故障或者挂掉的时候可以让备服务器顶上,也保证数据不会丢失

二、注意事项

    如果是虚拟机直接拷贝的服务器,要注意修改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

        主服务器IP:192.168.74.132

        备服务器IP:192.168.74.140

    2、创建库 test

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

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

    3、在主机创建同步用户

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

    4、在备机上用刚创建的demo用户远程登录

# 在备机上远程登录主机的MySQL
mysql -h192.168.74.132 -udemo -p

    5、修改主机的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
log-bin=mysql-bin
# 记录日志的数据库
binlog-do-db = test
# 不记录日志的数据库
binlog-ignore-db = mysql
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

    6、重启MySQL

    7、在主机上锁定数据库表

flush tables with read lock;

   8、查看主机状态

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)

    9、修改备服务器的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
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

    10、重启备机MySQL服务

    11、在备机进入MySQL执行 change master命令

# 先关闭 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、查看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)

四、开始同步测试

    1、先在主机执行解锁

unlock tables;

    2、在主机添加数据

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)

    3、在备机上查看同步数据

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)

    4、同步成功。结束

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