Mysql

关注公众号 jb51net

关闭
首页 > 数据库 > Mysql > MySQL互为主从数据同步部署

MySQL互为主从数据同步部署操作方式

作者:玄骨小子

本文介绍了在两台MySQL服务器间配置互为主从,实现高可用性和数据冗余,通过修改配置文件、创建授权用户及主从设置,实现了双向数据同步,有效提升了数据库的可靠性

在MySQL数据库管理中,实现互为主从的配置是一种常见的高可用性和数据冗余策略。

这种配置允许两个MySQL服务器相互复制数据,每个服务器既是对方的主服务器也是从服务器。

这样,无论哪个服务器发生故障,另一个都可以接管,保证数据的持续可用性和一致性。

一、环境准备

二、配置互为主从

1.编辑/etc/my.cnf文件

vim /etc/my.cnf

在[mysqld]模块下添加以下代码段

[mysqld]
server-id=51  #标识
log_bin=/var/lib/mysql/mysql_log_bin  #开启binlog并指定路径
binlog_format=mixed  #binlog 格式(mixed 兼容 row 和 statement)
log_slave_updates  #开启主从
auto_increment_increment = 2 #主键自增量
auto_increment_offset = 1 #主键起始偏量

两台虚拟机操作一样 只是主键起始偏量不同为了避免主键相同而让其错开

让51主机的主键成 1 , 3 ,5 ,7…

而52主机的主键成 2 , 4, 6, 8…

vim /etc/my.cnf
[mysqld]
server-id=52  #标识
log_bin=/var/lib/mysql/mysql_log_bin  #开启binlog并指定路径
binlog_format=mixed  #binlog 格式(mixed 兼容 row 和 statement)
log_slave_updates  #开启主从
auto_increment_increment = 2 #主键自增量
auto_increment_offset = 2 #主键起始偏量

重启MySQL服务

[root@host51 ~]# systemctl restart mysqld 

2.创建授权用户

两台虚拟机操作一样

mysql -uroot -p你的密码
创建连接用户
mysql> create user 'repl'@'%' identified by '123qqq...A';
Query OK, 0 rows affected (0.00 sec)
授权连接用户
mysql>  grant replication slave on *.* to 'repl'@'%';
Query OK, 0 rows affected (0.00 sec)
刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

3.查看binlog日志信息

查看192.168.4.51

mysql> show master status;
+----------------------+----------+--------------+------------------+-------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| mysql_log_bin.000004 |     1007 |              |                  |                   |
+----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

查看192.168.4.52

mysql> show master status
    -> ;
+----------------------+----------+--------------+------------------+-------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| mysql_log_bin.000004 |     1007 |              |                  |                   |
+----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

4.配置主从

首先在51主机上配置(52为主机)

mysql> change master to
master_host='192.168.4.52', #设置主的IP地址
master_user='repl', #设置连接用户名
master_password='123qqq...A', #设置连接用户密码
master_log_file='mysql_log_bin.000004', #设置binlog文件名
master_log_pos=1007; #设置binlog 同步位置
Query OK, 0 rows affected, 2 warnings (0.00 sec)

其次再在52主机上配置(51为主机)

mysql> change master to 
master_host='192.168.4.51',
master_user='repl',
master_password='123qqq...A',
master_log_file='mysql_log_bin.000004',
master_log_pos=1007;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

启动并查看

mysql> start slave ;
Query OK, 0 rows affected (0.00 sec)

mysql> show  slave status\G ;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.4.52
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_log_bin.000004
          Read_Master_Log_Pos: 1007
               Relay_Log_File: host51-relay-bin.000003
                Relay_Log_Pos: 324
        Relay_Master_Log_File: mysql_log_bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           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: 1007
              Relay_Log_Space: 702
              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: 52
                  Master_UUID: 888f4a92-acd3-11f0-8b2a-000c29a32fbd
             Master_Info_File: /var/lib/mysql/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)

只要Slave_IO_Running: Yes与Slave_SQL_Running: Yes都为yes即为同步成功

三、同步测试

在51主机上创建库表并插入数据

mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

mysql> create table db1.user(id int  primary key auto_increments);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into db1.user values(1);
Query OK, 1 row affected (0.00 sec)

在52主机上查询是否数据同步

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> select * from db1.user;
+------+
| id   |
+------+
|  1   |
+------+
1 row in set (0.00 sec)

然后再在52主机上创建库表并插入数据

mysql> create database db2;
Query OK, 1 row affected (0.00 sec)

mysql> create table db2.user(id int  primary key auto_increments);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into db2.user values(2);
Query OK, 1 row affected (0.00 sec)

在51主机上查询是否数据同步

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> select * from db2.user;
+------+
| id   |
+------+
|  2   |
+------+
1 row in set (0.00 sec)

可看到两次数据的同步都成功了

至此互为主从配置成功数据同步成功!

总结

通过以上步骤我们完成了MySQL的互为主从配置并实现数据同步。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文