1. 程式人生 > 實用技巧 >Mariadb/Mysql 主主複製架構

Mariadb/Mysql 主主複製架構

Centos7+MySQL主主複製架構

主主架構:就是互為主從的架構

node1:10.211.55.4 centos7 mariadb-5.5.65
node2:10.211.55.6 centos7 mariadb-5.5.65

主從架構配置如下:
https://www.cnblogs.com/zhangxingeng/p/10901452.html

1、安裝啟動mysql

all:

1)安裝mariadb軟體

yum install mariadb-server -y

3)啟動程序

systemctl start mariadb

2、mysql雙主配置

1)建立雙主配置檔案

node1:

[mysqld]
server-id=1
log-bin=mysql-bin
relay-log=mysql-relay-bin
binlog-do-db=test1
log-slave-updates
expire_logs_days=7
auto_increment_offset=1
auto_increment_increment=2
replicate-do-db=test1

node2:

[mysqld]
server-id=2
#<==標識伺服器id
log-bin=mysql-bin
#<==開啟mysql二進位制日誌
relay-log=mysql-relay-bin
#<==開啟mysql中繼日誌
binlog-do-db=test1
#<==指定資料庫binlog日誌記錄哪個db
log-slave-updates
#<==更新的資料寫入二進位制日誌,查詢show variables like 'log_slave_updates';
expire_logs_days=7
#<==binlog日誌自動過期清理設定為7天
auto_increment_offset=2
#<==控制列中值得增長值,也就是步長
auto_increment_increment=2
#<==確定increment_offset列值得起點值
replicate-do-db=test1
#<==指定slave需要複製哪個庫

注意:systemctl restart mariadb

2)新增同步賬戶

mysql
grant replication slave ,replication client on *.* to slave@'10.211.%.%' identified by 'centos';

3)檢視座標值:

node1:
show master status;

[root@node1 my.cnf.d]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

MariaDB [(none)]> 

node2:
show master status;

MariaDB [(none)]> show master status;     
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      245 | test1        |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> exit

4)從伺服器連線配置

node2:

MariaDB [(none)]> change master to
    -> master_host='10.211.55.4',
    -> master_user='slave',
    -> master_password='centos',
    -> master_log_file='mysql-bin.000001',
    #<==指明初始複製時的mysql1中的binlog檔案
    -> master_log_pos=245;
    #<==指明初始複製時binlog檔案的位置
Query OK, 0 rows affected (0.01 sec)

node1:

MariaDB [(none)]> change master to
    -> master_host='10.211.55.6',
    -> master_user='slave',
    -> master_password='centos',
    -> master_log_file='mysql-bin.000004',
    -> master_log_pos=245;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 

5)開啟開啟slave執行緒

all:

mysql 
MariaDB [(none)]> start slave;

6)查詢slave狀態

主要關注:

Slave_IO_Running: Yes #<==slave-IO執行緒是否yes

Slave_SQL_Running: Yes #<==slave-SQL執行緒是否yes

Replicate_Do_DB: test1 #<==同步的資料庫是否正確

node1:

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.211.55.6
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 245
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test1
          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: 245
              Relay_Log_Space: 823
              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: 2
1 row in set (0.00 sec)

ERROR: No query specified

node2:

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.211.55.4
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test1
          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: 245
              Relay_Log_Space: 823
              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: 1
1 row in set (0.00 sec)

ERROR: No query specified

MariaDB [(none)]> 

7)測試

mysql是雙向同步

在主上建立test_table資料表,插入master的資料

node1:

mysql
MariaDB [(none)]> use test1;
MariaDB [test1]> create table test_table (kk int auto_increment not null primary key, vv varchar(10)); 
MariaDB [test1]> insert into test_table(vv) values('master');      
MariaDB [test1]> select * from test_table;
+----+--------+
| kk | vv     |
+----+--------+
|  1 | master |
+----+--------+
1 row in set (0.00 sec)

備節點查詢

node2:

mysql
MariaDB [test1]> use test1;
MariaDB [test1]> select * from test_table;
+----+--------+
| kk | vv     |
+----+--------+
|  1 | master |
+----+--------+
1 row in set (0.00 sec)

備節點插入資料backup

node2:

MariaDB [test1]> insert into test_table(vv) values('backup');
MariaDB [test1]> select * from test_table;                   
+----+--------+
| kk | vv     |
+----+--------+
|  1 | master |
|  2 | backup |
+----+--------+
2 rows in set (0.00 sec)

主節點查詢backup有沒有同步過來

node1:

mysql
MariaDB [test1]> select * from test_table;
+----+--------+
| kk | vv     |
+----+--------+
|  1 | master |
|  2 | backup |
+----+--------+
2 rows in set (0.00 sec)