首页 > 数据库 >MySQL双主同步的实现

MySQL双主同步的实现

时间:2022-09-25 00:23:12浏览次数:54  
标签:同步 Log MySQL Replicate SSL Master mysql master 双主

双主复制:

  • 在两个节点上都可以写入数据,互为主从节点。

  • 解决单点失败的问题:一个主节点失败,所有节点都会失败。

双主配置:

  • (1) 各节点使用一个惟一server_id

  • (2) 都启动binary log和relay log

  • (3) 创建拥有复制权限的用户账号

  • (4) 定义自动增长id字段的数值范围各为奇偶

  • (5) 均把对方指定为主节点,并启动复制线程

注意:

双主复制的时候:考虑自动增长id

解决方法:
  • 配置一个节点使用奇数id
auto_increment_offset=1   #开始点
auto_increment_increment=2 #增长幅度
  • 另一个节点使用偶数id
auto_increment_offset=2
auto_increment_increment=2
master1存在数据的情况下,实现两个节点的主主复制模型
master1: 10.0.0.10
master2: 10.0.0.13
mysql8.0默认开启二进制日志,且二进制日志类型为:row
mysql默认开启中继日志

master1:
#开启二进制日志和配置serverid
[root@CentOS8 ~]# cat /etc/my.cnf.d/mysql-server.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid

server_id=10
log-bin=/data/logbin/mysql-logbin
auto_increment_offset=1   #开始点
auto_increment_increment=2 #增长幅度



#创建用于复制的账号
mysql> create user repluser@'10.0.0.%';
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to repluser@'10.0.0.%';
Query OK, 0 rows affected (0.00 sec)

#刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


#备份数据库所有文件
[root@CentOS8 ~]# mysqldump -uroot -A -F --single-transaction --master-data=1 > fullback_`date +%F-%T`

#将用于备份的文件传给master2
[root@CentOS8 ~]# scp ./fullback_2022-09-24-23\:11\:58 10.0.0.13:/root/


master2:
#配置serverid、开启二进制日志和中继日志
[root@CentOS8 ~]# cat /etc/my.cnf.d/mysql-server.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid

server_id=13
auto_increment_offset=2
auto_increment_increment=2
#relay-log=/data/relaylog/relay-log
#slave_skip_errors=1007|ALL

#默认值为1,临时关闭二进制日志,避免数据刚开始数据同步的时候将信息写入二进制日志造成空间浪费
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)

#执行数据恢复
mysql> source fullback_2022-09-24-23:15:14

#开启二进制日志
mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)

#刷新权限,如果不刷新权限,master1不能用change mster to连接过来
mysql> flush privileges;

#连接到master1
mysql> change master to
    -> master_host='10.0.0.10',
    -> master_user='repluser',
    -> master_port=3306,
    -> master_log_file='mysql-logbin.000005',
    -> master_log_pos=156;
Query OK, 0 rows affected, 2 warnings (0.12 sec)

#开启从节点的sql和io线程
mysql> start slave;


master1:
#连接到master2,因为上面mster2开启了复制线程,已经将信息同步到了master2,包括master1的repluser用户
mysql> change master to
    -> master_host='10.0.0.10',
    -> master_user='repluser',
    -> master_port=3306,
    -> master_log_file='mysql-logbin.000005',
    -> master_log_pos=156;
Query OK, 0 rows affected, 2 warnings (0.12 sec)

#开启从节点复制线程
start slave


master1;
#查看sql、io两个线程的状态,以及数据时差(Seconds_Behind_Master:)
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.13
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000001
          Read_Master_Log_Pos: 323
               Relay_Log_File: CentOS8-relay-bin.000002
                Relay_Log_Pos: 488
        Relay_Master_Log_File: binlog.000001
             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: 323
              Relay_Log_Space: 699
              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: 13
                  Master_UUID: f3544d44-3c1c-11ed-8ef3-000c29cbc410
             Master_Info_File: mysql.slave_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: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set (0.00 sec)

master2:
#查看master2的状态
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.10
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-logbin.000005
          Read_Master_Log_Pos: 759
               Relay_Log_File: CentOS8-relay-bin.000002
                Relay_Log_Pos: 538
        Relay_Master_Log_File: mysql-logbin.000005
             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: 759
              Relay_Log_Space: 749
              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: 10
                  Master_UUID: 4667b3c2-3c1a-11ed-9aa4-000c295a5cb9
             Master_Info_File: mysql.slave_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: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set (0.00 sec)

范例:master1不能存在数据,实现两个节点的主主复制模型
#在第一个master节点上实现
[root@master1 ~]#vim /etc/my.cnf.d/mariadb-server.cnf 
[mysqld]
server-id=8
log-bin
auto_increment_offset=1         #开始点
auto_increment_increment=2      #增长幅度   
[root@master1 ~]#systemctl start mariadb
[root@master1 ~]#mysql 
MariaDB [(none)]>show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |     28303 | | mariadb-bin.000002 |       386 |
+--------------------+-----------+
2 rows in set (0.000 sec)
MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.100.%' 
identified by 'magedu'; #在第二个master节点上实现
[rootmaster2 ~]#vim /etc/my.cnf.d/mariadb-server.cnf 
[mysqld]                                                                         
        server-id=18
log-bin
auto_increment_offset=2         #开始点
auto_increment_increment=2      #增长幅度
[root@master2 ~]#systemctl start mariadb
[root@master2 ~]#mysql 
MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.100.8',
    ->   MASTER_USER='repluser',
    ->   MASTER_PASSWORD='magedu',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='mariadb-bin.000002',
    ->   MASTER_LOG_POS=386;
Query OK, 0 rows affected (0.019 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.003 sec)
MariaDB [(none)]> show master logs;  #查看二进制位置
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |     28303 | | mariadb-bin.000002 |       344 |
+--------------------+-----------+
2 rows in set (0.001 sec) #在第一个master节点上实现
MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.100.18',
    ->   MASTER_USER='repluser',
	  ->   MASTER_PASSWORD='magedu',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='mariadb-bin.000002',
    ->   MASTER_LOG_POS=344;
Query OK, 0 rows affected (0.007 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)
MariaDB [db1]> create table t1(id int auto_increment primary key,name char(10)); #两个节点分别插入数据
#在第一个节点上执行
MariaDB [db1]> create database db1;
MariaDB [db1]> insert t1 (name) values('user1'); #在第二个节点上执行
MariaDB [db1]> insert t1 (name) values('user2'); #两个节点同时插入数据
MariaDB [db1]> insert t1 (name) values('userX');
MariaDB [db1]> select * from t1;
+----+-------+
| id | name  |
+----+-------+
|  1 | user1 | |  2 | user2 | |  3 | userX | |  4 | userX |
+----+-------+
4 rows in set (0.001 sec) #两个节点同时创建数据库,发生复制冲突
MariaDB [db1]> create database db2;
MariaDB [db1]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                   Master_Host: 192.168.100.18
                   Master_User: repluser
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mariadb-bin.000002
           Read_Master_Log_Pos: 1029
               Relay_Log_File: mariadb-relay-bin.000002
                 Relay_Log_Pos: 1110
         Relay_Master_Log_File: mariadb-bin.000002
             Slave_IO_Running: Yes
             Slave_SQL_Running: No
               Replicate_Do_DB: 
           Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
       Replicate_Wild_Do_Table: 
   Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1007
                   Last_Error: Error 'Can't create database 'db2'; database
exists' on query. Default database: 'db2'. Query: 'create database db2'
                 Skip_Counter: 0
           Exec_Master_Log_Pos: 897
		    Relay_Log_Space: 1553
               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: NULL
 Master_SSL_Verify_Server_Cert: No
                 Last_IO_Errno: 0
                 Last_IO_Error: 
               Last_SQL_Errno: 1007
               Last_SQL_Error: Error 'Can't create database 'db2'; database 
exists' on query. Default database: 'db2'. Query: 'create database db2'
   Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 18
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
                   Using_Gtid: No
                   Gtid_IO_Pos: 
       Replicate_Do_Domain_Ids: 
   Replicate_Ignore_Domain_Ids: 
                 Parallel_Mode: conservative
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: 
             Slave_DDL_Groups: 2
Slave_Non_Transactional_Groups: 0
   Slave_Transactional_Groups: 2 1 row in set (0.003 sec)

标签:同步,Log,MySQL,Replicate,SSL,Master,mysql,master,双主
From: https://www.cnblogs.com/heyongshen/p/16727042.html

相关文章

  • MYSQL锁机制行锁、表锁、死锁的实现
    一、MYSQL锁是什么?有什么类别锁定义: 同一时间同一个资源只能被一个线程访问 乐观锁和悲观锁乐观锁用的最多的就是数据库中的表加了一个标识字段version 例如......
  • MySQL主从复制常见问题及解决方法
    在从节点清除信息前提:停止slave的相关线程:stopslaveRESETSLAVE#从服务器清除master.info,relay-log.info,relaylog,开始新的relaylogRESETSLAVE ALL#清除所......
  • 为什么MySQL 默认隔离级别是RR,又被阿里设置为RC
    我们知道,我们可以通过这个命令查看数据库当前的隔离级别,MySQL默认隔离级别是RR. select@@tx_isolation; ANSI/ISOSQL定义的标准隔离级别有四种,从高到底依次为:可序......
  • mysql将字符串类型字段后面的小数点和零去掉
    背景现有student表,表中的学生年龄student_age字段中的值,是通过读取excel中的信息后更新到数据库中,但是因为处理不当,导致年龄的均带有.0,如28.0实际上应该是28。我们需要将......
  • sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (2026, '
    sqlalchemy.exc.OperationalError:(MySQLdb._exceptions.OperationalError)(2026,'SSLconnectionerror:unknownerrornumber')问题:使用sqlalchemy查询mysql数据时......
  • MySQL面试题
    MySql项目中使用的存储引擎5.0之前默认存储引擎为MyISAM引擎索引只有一种,被索引的字段值作为索引数据,叶子节点还包含该记录数据页地址不支持事务没有undolog......
  • MySQL维护之连接数、线程等
    实际生产维护中可能还会查看如下内容:1、如何在MySQL中查询OS线程id(LWP轻量级线程)?OS系统提供ps-Lfpid命令查看LWP;在performance_schema.threads中有......
  • 老杜MySQL/days.01
    1.mysql数据库启动的时候,服务占有的默认端口号是3306;字符编码方式为UTF-8;2.mysql超级管理员用户名不改,一定是root,需要设置密码;3.mysql服务启动关闭指令:netstop/start......
  • 线程同步(线程锁)
    多线程的优势在于可以同时运行多个任务(至少感觉起来是这样)但是当线程需要共享数据时,可能存在数据不同步的问题。为了避免这种情况,引入了锁的概念。1lock=threading.Lo......
  • Mysql小版本升级
    MySQL小版本升级操作步骤Bing@DBA于2020-11-2517:35:07发布2493收藏19分类专栏:MySQL版权MySQL专栏收录该内容38篇文章5订阅订阅专栏一、前言业务场景中需要......