專職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]> \"
相關文章
相關標籤/搜索