数据库
首页 > 数据库> > 专职DBA-MySQL双向主主复制

专职DBA-MySQL双向主主复制

作者:互联网

专职DBA-MySQL双向主主复制


MySQL双向主主复制
1.mysql主主复制是级联复制的特殊形式。
级联复制是A==>B==>C的单向复制形式,而主主复制实际上是将A和C合并为一个A。
mysql主主复制模型:A<==>B(AB库互为主从)
mysql主主复制也是使用mysql replication的复制方式,只不过复制方向是双向的。


有人说配置双主的可能原因是希望增加写并发的能力,双主是否真的能够增加写并发,我看未必吧。

建议即使配置了MySQL双主复制,最好还是单写,配置双主的目的并不是为了增加写并发,而是为了实现在主库宕机后角色能够快速切换提供服务。

正常主库宕机切换到从库,需要开启从库binlog功能,取消从库read-only参数,同时还需要更改Web用户授权等操作,而配置双主就不需要这些操作了,只要进行vip的自动切换即可。


2.mysql主主复制的企业级实现方案
(1).通过mysql的参数配置使表主键自增的方案。
(2).使用序列服务实现mysql双主方案。


3.mysql主主复制实践(自增ID)
主机规划
--------------------------------------------------------------
数据库角色       主机名     bond0(SSH连接IP)   bond1(内网通信IP)
--------------------------------------------------------------
主库1(master)    db01       10.0.0.11         192.168.10.11
--------------------------------------------------------------
主库2(master)    db02       10.0.0.12         192.168.10.12
--------------------------------------------------------------


[root@db01 ~]# cat /data/mysql/3306/my.cnf 
[mysqld]
# for mysql global
user                     = mysql
basedir                  = /usr/local/mysql
datadir                  = /data/mysql/3306/data
tmpdir                   = /data/mysql/3306/tmp
pid_file                 = /data/mysql/3306/3306.pid
socket                   = /data/mysql/3306/mysql.sock
port                     = 3306
server_id                = 113306
character-set-server     = utf8

# for binlog
binlog_format            = row
log_bin                  = /data/mysql/3306/logs/mysql-bin
log_slave_updates
expire_logs_days         = 7
auto_increment_increment = 2
auto_increment_offset    = 1
slave-skip-errors        = 1032,1062,1007,1008,1146,1049

# for error log
log_error                = /data/mysql/3306/error.log

# general log
general_log              = off
general_log_file         = /data/mysql/3306/general.log

# for slow query log
slow_query_log           = on
slow_query_log_file      = /data/mysql/3306/slow.log
long_query_time          = 1.000000

# for gtid
gtid_mode                = off
enforce_gtid_consistency = off

# for replication
# for innodb

[mysql]
character-set-client     = utf8
#prompt                  ="\\u@\\h [\\d]> \"
#prompt                  ="mysql [\\d]> \"
#prompt                  ="Master [\\d]> \"
#prompt                  ="Slave [\\d]> \"


重点参数
[root@db01 ~]# egrep "server_id|log_bin|log_slave|days|auto|skip" /data/mysql/3306/my.cnf
server_id                = 113306
log_bin                  = /data/mysql/3306/logs/mysql-bin
log_slave_updates
expire_logs_days         = 7
auto_increment_increment = 2
auto_increment_offset    = 1
slave-skip-errors        = 1032,1062,1007,1008,1146,1049


[root@db01 ~]# vim /etc/my.cnf
[mysql]
prompt                   ="Master01 [\\d]> \"


启动数据库实例
[root@db01 ~]# mysqld --defaults-file=/data/mysql/3306/my.cnf &


[root@db01 ~]# mysql -S /data/mysql/3306/mysql.sock -p
Enter password:


Master01 [(none)]> grant replication slave on *.* to 'rep'@'192.168.10.%' identified by '123';
Query OK, 0 rows affected, 1 warning (0.00 sec)



Master01 [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)


Master01 [(none)]> reset master;
Query OK, 0 rows affected (0.01 sec)


Master01 [(none)]> exit;
Bye


[root@db01 ~]# mysqldump -S /data/mysql/3306/mysql.sock -p -A -B -x --master-data=1 | gzip > /backup/bak1_$(date +%F).sql.gz
Enter password:


[root@db01 ~]# ls -lh /backup/
total 200K
-rw-r--r-- 1 root root 193K Jul 16 06:42 bak1_2019-07-16.sql.gz
drwxr-xr-x 2 root root   29 Jul 12 03:50 conf
-rw-r--r-- 1 root root 2.2K Jul 15 05:28 eth0123.zip
drwxr-xr-x 2 root root   54 Jul 12 05:39 sh
drwxr-xr-x 2 root root   49 Jul 12 03:52 sql


[root@db01 ~]# scp -rp /backup/bak1_$(date +%F).sql.gz root@192.168.10.12:/backup/
The authenticity of host '192.168.10.12 (192.168.10.12)' can't be established.
ECDSA key fingerprint is SHA256:auBeaQTHqMvqa5fgR9w9UDEho26UWpxRqc0bHUwCv/Y.
ECDSA key fingerprint is MD5:fc:ad:3a:4d:d7:83:20:a5:a1:49:c6:59:39:0b:1c:e5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.10.12' (ECDSA) to the list of known hosts.
root@192.168.10.12's password: 
bak1_2019-07-16.sql.gz                                                 100%  192KB  22.3MB/s   00:00
[root@db01 ~]#


[root@db02 ~]# ls -lh /backup/
total 200K
-rw-r--r-- 1 root root 193K Jul 16 06:42 bak1_2019-07-16.sql.gz
drwxr-xr-x 2 root root   29 Jul 12 03:50 conf
-rw-r--r-- 1 root root 2.2K Jul 15 05:45 eth0123.zip
drwxr-xr-x 2 root root   54 Jul 12 05:39 sh
drwxr-xr-x 2 root root   49 Jul 12 03:52 sql


[root@db02 ~]# cat /data/mysql/3306/my.cnf
[mysqld]
# for mysql global
user                     = mysql
basedir                  = /usr/local/mysql
datadir                  = /data/mysql/3306/data
tmpdir                   = /data/mysql/3306/tmp
pid_file                 = /data/mysql/3306/3306.pid
socket                   = /data/mysql/3306/mysql.sock
port                     = 3306
server_id                = 123306
character-set-server     = utf8

# for binlog
binlog_format            = row
log_bin                  = /data/mysql/3306/logs/mysql-bin
log_slave_updates
expire_logs_days         = 7
auto_increment_increment = 2
auto_increment_offset    = 2
slave-skip-errors        = 1032,1062,1007,1008,1146,1049

# for error log
log_error                = /data/mysql/3306/error.log

# general log
general_log              = off
general_log_file         = /data/mysql/3306/general.log

# for slow query log
slow_query_log           = on
slow_query_log_file      = /data/mysql/3306/slow.log
long_query_time          = 1.000000

# for gtid
gtid_mode                = off
enforce_gtid_consistency = off

# for replication
# for innodb

[mysql]
character-set-client     = utf8
#prompt                  ="\\u@\\h [\\d]> \"
#prompt                  ="mysql [\\d]> \"
#prompt                  ="Master [\\d]> \"
#prompt                  ="Slave [\\d]> \"


重点参数
[root@db02 ~]# egrep "server_id|log_bin|log_slave|days|auto|skip" /data/mysql/3306/my.cnf 
server_id                = 123306
log_bin                  = /data/mysql/3306/logs/mysql-bin
log_slave_updates
expire_logs_days         = 7
auto_increment_increment = 2
auto_increment_offset    = 2
slave-skip-errors        = 1032,1062,1007,1008,1146,1049


[root@db02 ~]# cat /etc/my.cnf 
[mysql]
prompt                   ="Master02 [\\d]> \"


[root@db02 ~]# mysqld --defaults-file=/data/mysql/3306/my.cnf &


[root@db02 ~]# mysql -S /data/mysql/3306/mysql.sock -p
Enter password:


Master02 [(none)]> reset master;
Query OK, 0 rows affected (0.02 sec)


Master02 [(none)]> exit;
Bye


[root@db02 ~]# zcat /backup/bak1_$(date +%F).sql.gz | mysql -S /data/mysql/3306/mysql.sock -p
Enter password:


[root@db02 ~]# mysql -S /data/mysql/3306/mysql.sock -p
Enter password:


CHANGE MASTER TO
MASTER_HOST='192.168.10.11',
MASTER_PORT=3306,
MASTER_USER='rep',
MASTER_PASSWORD='123';


Master02 [(none)]> CHANGE MASTER TO
    -> MASTER_HOST='192.168.10.11',
    -> MASTER_PORT=3306,
    -> MASTER_USER='rep',
    -> MASTER_PASSWORD='123';
Query OK, 0 rows affected, 2 warnings (0.01 sec)


Master02 [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)


Master02 [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.11
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: db02-relay-bin.000005
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.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: 154
              Relay_Log_Space: 739
              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: 113306
                  Master_UUID: 7c145945-a680-11e9-baea-000c29a14cf7
             Master_Info_File: /data/mysql/3306/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)


Master02 [(none)]> exit;
Bye


[root@db02 ~]# mysql -S /data/mysql/3306/mysql.sock -p -e "show slave status\G"|egrep "IO_Running|SQL_Running:|Behind_Master"
Enter password: 
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
        Seconds_Behind_Master: 0


在首次开启复制的过程中,如果出现复制错误,可以执行如下语句:
stop slave;
set global sql_slave_skip_counter = 1; #将同步指针向下移动一个,如果多次移动仍不同步,可以重复操作。
start slave;


[root@db02 ~]# mysql -S /data/mysql/3306/mysql.sock -p
Enter password:


Master02 [(none)]> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |   775259 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


CHANGE MASTER TO
MASTER_HOST='192.168.10.12',
MASTER_PORT=3306,
MASTER_USER='rep',
MASTER_PASSWORD='123',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS= 775259;


Master01 [(none)]> CHANGE MASTER TO
    -> MASTER_HOST='192.168.10.12',
    -> MASTER_PORT=3306,
    -> MASTER_USER='rep',
    -> MASTER_PASSWORD='123',
    -> MASTER_LOG_FILE='mysql-bin.000001',
    -> MASTER_LOG_POS= 775259;
Query OK, 0 rows affected, 2 warnings (0.01 sec)


Master01 [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)


Master01 [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.12
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 154
               Relay_Log_File: db01-relay-bin.000010
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-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: 154
              Relay_Log_Space: 786
              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: 123306
                  Master_UUID: ab0bfc5a-a681-11e9-bf5d-000c296165a9
             Master_Info_File: /data/mysql/3306/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)


[root@db01 ~]# mysql -S /data/mysql/3306/mysql.sock -p -e "show slave status\G"|egrep "IO_Running|SQL_Running:|Behind_Master" 
Enter password: 
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
        Seconds_Behind_Master: 0


Master01 [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)


Master01 [(none)]> create database zhuzhu;
Query OK, 1 row affected (0.01 sec)


Master01 [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zhuzhu             |
+--------------------+
5 rows in set (0.00 sec)


Master01 [(none)]> use zhuzhu;
Database changed


create table t1(
    id bigint(12) not null auto_increment comment '主键',
    name varchar(12) not null comment '姓名',
    primary key (id)
);


Master01 [zhuzhu]> create table t1(
    ->     id bigint(12) not null auto_increment comment '主键',
    ->     name varchar(12) not null comment '姓名',
    ->     primary key (id)
    -> );
Query OK, 0 rows affected (0.03 sec)


Master01 [zhuzhu]> show tables;
+------------------+
| Tables_in_zhuzhu |
+------------------+
| t1               |
+------------------+
1 row in set (0.00 sec)


Master01 [zhuzhu]> insert into t1(name) values('学生'),('护士'),('空姐');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0


Master01 [zhuzhu]> select * from t1;
+----+--------+
| id | name   |
+----+--------+
|  1 | 学生   |
|  3 | 护士   |
|  5 | 空姐   |
+----+--------+
3 rows in set (0.00 sec)


Master02 [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zhuzhu             |
+--------------------+
5 rows in set (0.01 sec)


Master02 [zhuzhu]> use zhuzhu;
Database changed
Master02 [zhuzhu]> select * from t1;
+----+--------+
| id | name   |
+----+--------+
|  1 | 学生   |
|  3 | 护士   |
|  5 | 空姐   |
+----+--------+
3 rows in set (0.00 sec)


Master02 [zhuzhu]> insert into t1(name) values('人妖'),('少妇'),('小姨');    
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

Master02 [zhuzhu]> select * from t1;
+----+--------+
| id | name   |
+----+--------+
|  1 | 学生   |
|  3 | 护士   |
|  5 | 空姐   |
|  6 | 人妖   |
|  8 | 少妇   |
| 10 | 小姨   |
+----+--------+
6 rows in set (0.01 sec)
新插入的数据是从当前表中最大数字偶数6的基础上开始进行插入的。


Master01 [zhuzhu]> select * from t1;
+----+--------+
| id | name   |
+----+--------+
|  1 | 学生   |
|  3 | 护士   |
|  5 | 空姐   |
|  6 | 人妖   |
|  8 | 少妇   |
| 10 | 小姨   |
+----+--------+
6 rows in set (0.00 sec)
//---------------------------------->MySQL双向主从复制测试完成。


mysql双主复制my.cnf完整配置对比
[root@db01 ~]# cp /data/mysql/3306/my.cnf /root/db01_my.cnf
[root@db02 ~]# scp /data/mysql/3306/my.cnf root@db01:/root/db02_my.cnf

[root@db01 ~]# sz db01_my.cnf
[root@db02 ~]# sz db02_my.cnf

[root@db01 ~]# cat /data/mysql/3306/my.cnf
[mysqld]
# for mysql global
user                     = mysql
basedir                  = /usr/local/mysql
datadir                  = /data/mysql/3306/data
tmpdir                   = /data/mysql/3306/tmp
pid_file                 = /data/mysql/3306/3306.pid
socket                   = /data/mysql/3306/mysql.sock
port                     = 3306
server_id                = 113306
character-set-server     = utf8

# for binlog
binlog_format            = row
log_bin                  = /data/mysql/3306/logs/mysql-bin
log_slave_updates
expire_logs_days         = 7
auto_increment_increment = 2
auto_increment_offset    = 1
slave-skip-errors        = 1032,1062,1007,1008,1146,1049

# for error log
log_error                = /data/mysql/3306/error.log

# general log
general_log              = off
general_log_file         = /data/mysql/3306/general.log

# for slow query log
slow_query_log           = on
slow_query_log_file      = /data/mysql/3306/slow.log
long_query_time          = 1.000000

# for gtid
gtid_mode                = off
enforce_gtid_consistency = off

# for replication
# for innodb

[mysql]
character-set-client     = utf8
#prompt                  ="\\u@\\h [\\d]> \"
#prompt                  ="mysql [\\d]> \"
#prompt                  ="Master [\\d]> \"
#prompt                  ="Slave [\\d]> \"




[root@db02 ~]# cat /data/mysql/3306/my.cnf
[mysqld]
# for mysql global
user                     = mysql
basedir                  = /usr/local/mysql
datadir                  = /data/mysql/3306/data
tmpdir                   = /data/mysql/3306/tmp
pid_file                 = /data/mysql/3306/3306.pid
socket                   = /data/mysql/3306/mysql.sock
port                     = 3306
server_id                = 123306
character-set-server     = utf8

# for binlog
binlog_format            = row
log_bin                  = /data/mysql/3306/logs/mysql-bin
log_slave_updates
expire_logs_days         = 7
auto_increment_increment = 2
auto_increment_offset    = 2
slave-skip-errors        = 1032,1062,1007,1008,1146,1049

# for error log
log_error                = /data/mysql/3306/error.log

# general log
general_log              = off
general_log_file         = /data/mysql/3306/general.log

# for slow query log
slow_query_log           = on
slow_query_log_file      = /data/mysql/3306/slow.log
long_query_time          = 1.000000

# for gtid
gtid_mode                = off
enforce_gtid_consistency = off

# for replication
# for innodb

[mysql]
character-set-client     = utf8
#prompt                  ="\\u@\\h [\\d]> \"
#prompt                  ="mysql [\\d]> \"
#prompt                  ="Master [\\d]> \"
#prompt                  ="Slave [\\d]> \"

 

标签:log,DBA,root,MySQL,主主,Master,mysql,3306,data
来源: https://www.cnblogs.com/zhouwanchun/p/11195306.html