溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL主主(雙主)數據同步

發布時間:2020-06-05 13:51:30 來源:網絡 閱讀:5841 作者:若白衣 欄目:MySQL數據庫

1. 兩臺mysql都可以讀寫,互為主備,默認只是用一臺(masterA)負責數據的寫入,另一臺(masterB)備用;
2. masterA是masterB的主庫,masterB又是masterA的主庫,他們互為主從;

不足之處:
1. masterB可能處于抑制空閑狀態(可以用他當從庫,負責部分查詢);
2. 主庫后面提供服務的從庫要等masterB先同步完了數據后才能去masterB上去同步數據,肯能會造成一定程度的同步延遲;

搭建環境

系統:CentOS7
數據庫版本:5.6.36
masterA地址:192.168.188.2
masterB地址:192.168.188.3
安裝目錄:/usr/local/mysql/
數據目錄:/data/mysql/
A與B機器都安裝好了MySQL,數據庫與表都提前同步好;

防火墻配置

iptables

添加mysql同學端口(默認3306)

vim /etc/sysconfig/iptables                //編輯iptables文件,也可以用iptables命令來添加,不過要保存命令
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT        //添加允許3306端口通過
service iptables restart            //重啟iptables服務

關閉selinux

vim /etc/selinux/config                //編輯SELinux配置文件
SELINUX=disabled                //修改值為disabled為關閉

服務器設置

masterA設置(192.168.188.2)

vim /etc/my.cnf                //編輯my.cnf文件
server-id=2 //增加server-id為2,一般為自己ip
log_bin=test01 //設置log_bin名為test01
/etc/init.d/mysqld restart        //重啟mysql服務
mysql -uroot -p123456            //登錄mysql,不要照抄,-u后面是賬號,-p后面是密碼
grant replication slave on *.* to 'repl'@'192.168.188.3' identified by '123456';        //創建mysql賬號repl,只容許指定ip訪問,也可以指定ip范圍192.168.188.%,其中%為通配符,表示所有;
flush privileges;            //刷新授權表信息
flush tables with read lock;    //鎖定數據庫表暫時無法寫服務;
show master status;        //查看binlog文件值與pos值
stop slave;            //關閉同步

masterB設置(192.168.188.3)

vim /etc/my.cnf
server-id=3                        //增加server-id為3,一般為自己ip
log_bin=test02                    //設置log_bin名為test02
/etc/init.d/mysqld restart    //重啟mysql服務
mysql -uroot -p123456        //登錄mysql
grant replication slave on *.* to 'repl'@'192.168.188.2' identified by '123456';              //創建用戶,允許192.168.188.2登錄本機器
flush privileges;                //刷新授權表
stop slave;                        //關閉同步
show master status;        //查看binlog文件值與pos值
change master to master_host='192.168.188.2', master_user='repl', master_password='123456', master_log_file='test01.000001', master_log_pos=664383;        //這里注意log_file與pos值都要對應對應A的show master status;值
start slave;        //開啟同步

masterA設置

change master to master_host='192.168.188.3', master_user='repl', master_password='123456', master_log_file='test02.000001', master_log_pos=664343;     //這里log_file與pos值寫的必須是B上show master status;的值
start slave;                    //開啟同步
unlock tables;                //解鎖寫

測試主主

在A的test數據庫下建立t1表,B上查詢后有t1表,證明B能同步A修改的數據;
在B的test數據庫下建立t2表,A上查詢,有t2表,證明A能同步到B修改后的數據;

masterA設置

mysql -uroot -p123456                //登錄mysql
mysql> use test;                //切換到數據庫test
mysql> show tables;            //查看當前數據庫的所有表,這里沒有一個表
Empty set (0.00 sec)
mysql> create table t1(`id` int(4),`name` char(40));            //插入一個表t1
mysql> show tables;       //查看當前數據庫的表
+----------------+
| Tables_in_test |
+----------------+
| t1             |
+----------------+
1 row in set (0.00 sec)

masterB

mysql -uroot -p123456                //登錄mysql
use test;                //切換到數據庫test
mysql> show tables;            //查詢同步到數據庫test下出現了t1表,證明B能同步A的數據;
+----------------+
| Tables_in_test |
+----------------+
| t1             |
+----------------+
1 row in set (0.00 sec)
create table t2(`id` int(4),`name` char(40));            //新建一個t2表,如果A能同步到,就證明A能同步到B

切換masterA(省略了登錄與切換數據庫的命令)

mysql> show tables;    //查詢A的test數據庫下所有的表
+----------------+
| Tables_in_test |
+----------------+
| t1             |
| t2             |
+----------------+
2 rows in set (0.00 sec)
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女