部署:mysql搭建多主一从源复制环境

问题描述:搭建过一主多从的环境,由于数据库数据一致性要求高,有些情景会搭建一主多从的架构,搭建多主一从的模式,相对来说适合数据整合,将多个业务的库整合到一起,方便做查询,也可以当做一个监控其他主库数据的作用。但是搭建这样的模式极易造成数据冲突,同步的库名一致,导致主从异常,同时也对网络并发提出一定得要求

 

环境介绍:

正常可以将数据库分别放在不通的服务器上,本文方便测试,以下三个数据库都放在了同一台服务器上

IP:192.168.163.21

3306--主库  server_id:29

3307--主库  server_id:27

3308--收集从库  server_id:28

 

 

 

1.配置3306主库参数

[mysqld]
basedir=/application/mysql
datadir=/data/3306/data
socket=/data/3306/mysql.sock
port=3306
log-error=/data/3306/mysql.log
log_bin=/data/3306/mysql-bin
binlog_format=row
#skip-name-resolve
server-id=29
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1

# 需要同步库
binlog-do-db = dev3306
# 不需要同步库
binlog-ignore-db = mysql
binlog_ignore_db = information_schema
binlog_ignore_db = performation_schema
binlog_ignore_db = sys

 

配置复制用户

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'root1234';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

 

重启3306端口

systemctl restart mysqld3306

 

2.配置3307主库参数

[mysqld]
basedir=/application/mysql
datadir=/data/3307/data
socket=/data/3307/mysql.sock
port=3307
log-error=/data/3307/mysql.log
log_bin=/data/3307/mysql-bin
binlog_format=row
skip-name-resolve
server-id=27
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1

# 需要同步库
binlog-do-db = dev3307
# 不需要同步库
binlog-ignore-db = mysql
binlog_ignore_db = information_schema
binlog_ignore_db = performation_schema
binlog_ignore_db = sys

 

 

配置复制用户

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'root1234';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

 

重启3307端口

systemctl restart mysqld3307

 

mysql> show master status;
+------------------+----------+--------------+--------------------------------------------------+-----------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                 | Executed_Gtid_Set                             |
+------------------+----------+--------------+--------------------------------------------------+-----------------------------------------------+
| mysql-bin.000015 |      362 | dev3306      | mysql,information_schema,performation_schema,sys | 55826e3e-7f15-11eb-a9e3-000c2959ac6a:1-236538 |
+------------------+----------+--------------+--------------------------------------------------+-----------------------------------------------+
1 row in set (0.00 sec)

 

3.配置3307从库参数

[mysqld]
basedir=/application/mysql
datadir=/data/3308/data
port=3308
socket=/data/3308/mysql.sock
log-error=/data/3308/mysql.log
log_bin=/data/3308/mysql-bin
binlog_format=row
skip-name-resolve
server-id=28
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1

lower_case_table_names = 1 # 表名不区分大小写 
master_info_repository = table
relay_log_info_repository = table

 

重启生效

 

systemctl restart mysqld3308

 

mysql> show master status;
+------------------+----------+--------------+--------------------------------------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                 | Executed_Gtid_Set                         |
+------------------+----------+--------------+--------------------------------------------------+-------------------------------------------+
| mysql-bin.000011 |      362 | dev3307      | mysql,information_schema,performation_schema,sys | 1a0da16a-7f15-11eb-a6b4-000c2959ac6a:1-16 |
+------------------+----------+--------------+--------------------------------------------------+-------------------------------------------+
1 row in set (0.00 sec)

 

4.开启同步

mysql> CHANGE MASTER TO  
    -> MASTER_HOST='192.168.163.21',
    -> MASTER_PORT=3306, MASTER_USER='slave', 
    -> MASTER_PASSWORD='root1234',
    -> MASTER_LOG_FILE='mysql-bin.000015', MASTER_LOG_POS=362 for channel '29';
7, MASTER_USER='slave', 
MASTER_PASSWORD='root1234',
MASTER_LOG_FILE='mysql-bin.000011', MASTER_LOG_POS=362 for channel '27';Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> 
mysql> 
mysql> CHANGE MASTER TO  
    -> MASTER_HOST='192.168.163.21',
    -> MASTER_PORT=3307, MASTER_USER='slave', 
    -> MASTER_PASSWORD='root1234',
    -> MASTER_LOG_FILE='mysql-bin.000011', MASTER_LOG_POS=362 for channel '27';
Query OK, 0 rows affected, 2 warnings (0.02 sec)

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

 

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.163.21
                  Master_User: slave
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000010
          Read_Master_Log_Pos: 630
               Relay_Log_File: mha4-relay-bin-27.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000010
             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: 630
              Relay_Log_Space: 529
              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: 27
                  Master_UUID: 1a0da16a-7f15-11eb-a6b4-000c2959ac6a
             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: 1bf45cd1-7f15-11eb-a810-000c2959ac6a:1-13
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 27
           Master_TLS_Version: 
*************************** 2. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.163.21
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000014
          Read_Master_Log_Pos: 630
               Relay_Log_File: mha4-relay-bin-29.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000014
             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: 630
              Relay_Log_Space: 529
              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: 29
                  Master_UUID: 55826e3e-7f15-11eb-a9e3-000c2959ac6a
             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: 1bf45cd1-7f15-11eb-a810-000c2959ac6a:1-13
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 29
           Master_TLS_Version: 
2 rows in set (0.01 sec)

 

5.验证同步

在从库上手动创建dev3306和dev3307

3306

mysql> use dev3306;
Database changed
mysql> 
mysql> show tables;
Empty set (0.00 sec)

mysql> 
mysql> create table t1;
ERROR 1113 (42000): A table must have at least 1 column
mysql> 
mysql> 
mysql> create table t1 (id int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values(1),(2),(3);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

3307

mysql> use dev3307;
Database changed
mysql> 
mysql> create table t2(id int);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t2 values(4),(5),(6);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

3308

mysql> select * from dev3306.t1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

mysql> select * from dev3307.t2;
+------+
| id   |
+------+
|    4 |
|    5 |
|    6 |
+------+
3 rows in set (0.00 sec)

 

6.启动和停止单独slave进程

 

mysql> stop slave for channel '27';
Query OK, 0 rows affected (0.01 sec)

mysql> start slave for channel '27';
Query OK, 0 rows affected (0.00 sec)

 

 

7.但是以上主库参数设置binlog-do-db也会带来弊端,用这个参数限制同步哪些库的话,但是又想整体对mysql中所有的库做主从,就会比较麻烦。限制了同步某一个库,其他的库没法做实时同步,就不能保证mysql中其他库的安全性。另一方面如果不加这个参数进行多主一从的模式,进行全库都复制同步,就会造成数据冲突,从而主从异常

binlog-do-db=需要复制的数据库名,如果复制多个数据库,重复设置这个选项即可

binlog-ignore-db=不需要复制的数据库苦命,如果复制多个数据库,重复设置这个选项即可

 

参考文档:https://blog.csdn.net/wangsdsdfds/article/details/105809232

 

posted @ 2021-05-12 16:03  我爱睡莲  阅读(306)  评论(0编辑  收藏  举报