mysql雙主搭建-centos7


基於主從演變成爲雙主。

一、master1的配置

1、修改配置項,基於原來的Gtid主動模式設置 

 vim /etc/my.cnf 修改內容爲:

 log-bin=mysql-bin

Git_mode=ON

server-id=158

enforce_gtid_consistency=1

image.png

2、重啓master1的服務生效配置

   systemctl restart mysql

3、登陸系統,授權遠程登陸用戶

 GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'rep01'@'192.168.31.%' 

image.png

4、導出數據庫(mysqldump)

 先quit;退出。

執行導出語句:

mysqldump -uroot -pbgx --all-databases --single-transaction --master-data=1 --flush-logs > /root/backup/db-$(date +%F)-all.sql

   image.png

5、將數據庫發送給master2.

  

scp db-2018-08-24-all.sql root@slave:/root/backup/

image.png

在master2上面可以看到拷貝過去的數據:

image.png

------------

二、master2 的配置

1、和master1一樣,修改配置文件。

  

[mysqld]

log-bin=mysql-bin

server-id=175

basedir=/soft/mysql

datadir=/soft/mysql/data

gtid_mode = ON

enforce_gtid_consistency=1


2、重啓服務,配置生效。

 systemctl restart mysq


3、授權

 設置相同的授權賬號密碼,與master1保持一致。

GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'rep01'@'192.168.31.%' 

image.png

4、恢復master設置,導入數據

 mysql> reset master;

[root@MiWiFi-R1CL-srv backup]# mysql -uroot -pbgx -e "source /root/backup/db-2018-08-24-all.sql"


有一個錯誤,這個錯誤我們也不陌生了:

[root@MiWiFi-R1CL-srv backup]# mysql -uroot -pbgx -e "source /root/backup/db-2018-08-24-all.sql"

mysql: [Warning] Using a password on the command line interface can be insecure.


ERROR 1840 (HY000) at line 24 in file: '/root/backup/db-2018-08-24-all.sql': @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.



1、到master1上執行 reset master;

2、再導出master1的數據庫。執行第一點的第4步。

     mysqldump -uroot -pbgx --all-databases --single-transaction --master-data=1 --flush-logs > /root/backup/db-$(date +%F)-all.sql


    mysqldump: [Warning] Using a password on the command line interface can be insecure.

    Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the     database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.

查看結果:

總用量 788

-rw-r--r--. 1 root root 804118 8月  24 11:55 db-2018-08-24-all.sql

3、參照第一點的第5步。

4、開始執行第 二點的第4步。

以上錯誤還是存在。。。爲什麼?

ERROR 1776 (HY000) at line 30 in file: '/root/backup/db-2018-08-24-all.sql': Parameters MASTER_LOG_FILE, MASTER_LOG_POS, RELAY_LOG_FILE and RELAY_LOG_POS cannot be set when MASTER_AUTO_POSITION is active.


以上無法解決,那麼重新初始化數據庫。方法參考:

http://blog.51cto.com/13683138/2163647 中的“4、導入備份的數據到slave。中的解決方法”

初始化語句:

/soft/mysql/bin/mysqld --initialize --user=mysql --basedir=/soft/mysql --datadir=/soft/mysql/data


完成後,再執行:

[root@MiWiFi-R1CL-srv mysql]#  mysql -uroot -pbgx -e "source /root/backup/db-2018-08-24-all.sql"

mysql: [Warning] Using a password on the command line interface can be insecure.

ERROR 1840 (HY000) at line 24 in file: '/root/backup/db-2018-08-24-all.sql': @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.


檢查錯誤:

查看master1中的數據庫master 狀態

mysql> show master status\G

*************************** 1. row ***************************

             File: mysql-bin.000002

         Position: 154

     Binlog_Do_DB: 

 Binlog_Ignore_DB: 

Executed_Gtid_Set: 

1 row in set (0.00 sec)

並麼有reset master成功。

mysql> show master status\G

*************************** 1. row ***************************

             File: mysql-bin.000001

         Position: 154

     Binlog_Do_DB: 

 Binlog_Ignore_DB: 

Executed_Gtid_Set: 

1 row in set (0.00 sec)



再次執行一次reset master 後再導出數據。

mysqldump -uroot -pbgx --all-databases --single-transaction --master-data=1 --flush-logs > /root/backup/db-$(date +%F)-all.sql

scp db-2018-08-24-all.sql root@slave:/root/backup/


在master2的服務器上面,查看master狀態:

mysql> show master status\G

*************************** 1. row ***************************

             File: mysql-bin.000001

         Position: 154

     Binlog_Do_DB: 

 Binlog_Ignore_DB: 

Executed_Gtid_Set: 

1 row in set (0.00 sec)


導入數據;

 mysql -uroot -pbgx -e "source /root/backup/db-2018-08-24-all.sql"

[root@MiWiFi-R1CL-srv data]#  mysql -uroot -pbgx -e "source /root/backup/db-2018-08-24-all.sql"

mysql: [Warning] Using a password on the command line interface can be insecure.


此步驟執行成功。。


---------------


5、清理從主庫的二進制日誌

mysql> reset master;

Query OK, 0 rows affected (0.00 sec)


6、changemasterto master1

mysql> change master to master_host='master', master_user='rep01',master_password='Rep01', master_auto_position=1;

Query OK, 0 rows affected, 2 warnings (0.01 sec)


7、啓動 slave 角色查看狀態


mysql > start slave;

image.png


三、配置主mastser1 changemaster

1、刷新權限

   

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

2、changemaster

  change master to master_host='slave', master_user='rep01',master_password='Rep01', master_auto_position=1;


mysql> change master to master_host='slave', master_user='rep01',master_password='Rep01', master_auto_position=1;

Query OK, 0 rows affected, 2 warnings (0.01 sec)

3、啓動slave角色

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

4、查看狀態

image.png

這裏有個錯誤,正常應該是yes,不應爲connectiong。所以,去看看

是否有其他問題。

  查找問題入口:

查看日誌錯誤,發現是防火牆的原因。

關閉 master2的防火牆,就可以了。

[root@MiWiFi-R1CL-srv data]# systemctl stop firewalld

[root@MiWiFi-R1CL-srv data]# systemctl disable firewalld

Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.

Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

再查看master1 的狀態:

image.png

  

  四、對 master1與master2進行交換添加數據查看狀態結果

1、master1 添加數據

  

mysql> create database master11111DB;

查看master2已經有了master1創建的數據庫。

image.png


2、通過master2創建數據庫,看master1是否同步。

mysql> create database master22222DB;

查看master1 已經有了master2創建的數據庫。

image.png


以上,雙主同步設置完成。後續我們在進行 多源複製,M-M -S-S




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章