標籤:mysql 主主 雙主
環境介紹:
CentOS-6.7 64位
Master1:Blog-MySQL-1 192.168.133.128
Master2:Blog-MySQL-2 192.168.133.132
MySQL 版本 5.5.33 二進位包安裝
檢查兩台機器的實驗環境是否符合要求。並保證可以正常登陸 mysql
以其中一台為例:
hostname
ifconfig eth0 | grep "inet addr" | awk ‘{print $2}‘ | cut -d: –f2
mysql –V
cat /etc/redhat-release
uname -a
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/8D/01/wKiom1iBeeOzvwm9AABG_v425iM233.png" height="170" />
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M02/8D/01/wKiom1iBeeTyhvxMAABC1TqzMuc631.png" height="233" />
以上算是雙主環境搭建的準備工作吧,接下來編輯 Master 1 的 my.cnf 檔案
為了方便查閱還是跟之前一樣,先過濾一下 # , $ 開頭的行吧
cat /etc/my.cnf | grep -v ^# | grep -v ^$ > tmp.log
cat tmp.log > /etc/my.cnf
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M02/8C/FE/wKioL1iBeeTQ3PLTAAAR_u35yRc048.png" height="40" />
Master 1 的 my.cnf 檔案中的 [mysqld] 中添加以下欄位
vim /etc/my.cnf
server-id = 1
log-bin = mysql-bin
auto_increment_increment = 2
auto_increment_offset = 1
log-slave-updates
expire_logs_days = 7
skip-name-resolve
lower_case_table_names=1
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/8D/01/wKiom1iBeeXTcvcDAAAWT_-1U0o779.png" height="142" />
以上參數
auto_increment_increment = 2 #自增ID的間隔,如 1 3 5 間隔為 2
auto_increment_offset = 1 #ID的初始位置
修改設定檔需要重啟 Master 1 的資料庫
/etc/init.d/mysqld restart
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/8D/01/wKiom1iBeebQvg60AAAPt65wvYc655.png" height="56" />
登入 Master 1 的 mysql
mysql -uroot -p123456
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/8D/01/wKiom1iBeebgzdBUAABDCYoWubc389.png" height="232" />
在 Master 1 上建立同步到 Master 2 上的帳號
grant replication slave on *.* to [email protected]‘%‘ identified by ‘123456‘;
flush privileges;
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M02/8D/01/wKiom1iBeejCHEbbAAAbT698LUA011.png" height="85" />
檢查剛才剛才的操作
select user,host from mysql.user;
show grants for [email protected]‘%‘;
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/8C/FE/wKioL1iBeemAwD63AABEQmI3yqo543.png" height="338" />
繼續操作 Master1 鎖表,查看 binlog 檔案及 pos 位置節點
flush table with read lock;
show master status;
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/8D/01/wKiom1iBeeqS6jkRAAAlEaSEoPw986.png" height="174" />
新開啟一個視窗,將 Master 1 的資料庫檔案匯出,通過 scp 命令傳遞給 Master 2 上
mysqldump -uroot -p123456 --events -A -B > /root/test.sql
scp test.sql [email protected]:/root
yes
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/8D/01/wKiom1iBeeuiHmT3AABKlVw-iuM797.png" height="169" />
解表 Master 1
unlock tables;
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/8D/01/wKiom1iBeezSWRpKAAALKBU8Plg922.png" height="48" />
登陸到 Master 2 上,現將剛才的資料庫 test.sql 匯入
mysql -uroot -p123456 < test.sql
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/8C/FE/wKioL1iBee2j7TiYAAAKrc8XD4o409.png" height="28" />
編輯 Master 2 的 my.cnf 檔案,跟上面的操作一樣,只需要注意的是 server-id 和 offset
cat /etc/my.cnf | grep -v ^# | grep -v ^$ > tmp.log
cat tmp.log > /etc/my.cnf
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/8C/FE/wKioL1iBee3BsIHyAAASPsN5uvE957.png" height="38" />
server-id = 2
log-bin = mysql-bin
auto_increment_increment = 2
auto_increment_offset = 2
log-slave-updates
expire_logs_days = 7
skip-name-resolve
lower_case_table_names=1
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M02/8C/FE/wKioL1iBee7hi-pzAABIhNfDhkM392.png" height="441" />
修改設定檔後別忘重啟 mysql
/etc/init.d/mysqld restart
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M02/8D/01/wKiom1iBee-jhu68AAAQSYsUFxU064.png" height="57" />
登陸 Master 2 的 mysql 資料庫
mysql -uroot –p123456
CHANGE MASTER TO
MASTER_HOST=‘192.168.133.128‘,
MASTER_PORT=3306,
MASTER_USER=‘yuci‘,
MASTER_PASSWORD=‘123456‘,
MASTER_LOG_FILE=‘mysql-bin.000001‘,
MASTER_LOG_POS=332;
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M02/8C/FE/wKioL1iBefCgpItPAABkcfua3aI642.png" height="344" />
查看兩個IO SQL連個線程是否串連成功
start slave;
show slave status\G
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/8D/01/wKiom1iBefHD2GbAAABIY4Xl2Mw044.png" height="280" />
在 Master 2 上建立一個用於同步到 Master 1 的帳號,重新整理並檢查
其實就是兩台 mysql 分別建立兩個帳號,帳號不能重複
grant replication slave on *.* to [email protected]‘%‘ identified by ‘123456‘;
flush privileges;
select user,host from mysql.user;
show grants for [email protected]‘%‘;
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/8C/FE/wKioL1iBefHxnC6NAABkm2arogo689.png" height="462" />
查看 Master 2 的 binlog 檔案和 pos 號
show master status;
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/8D/01/wKiom1iBefLhcVokAAAZKMXf-YU918.png" height="117" />
返回 Master 1 上,設定帳號同步參數
輸入時注意每個參數,別把它和之前的帳號,使用者名稱,密碼搞反了
CHANGE MASTER TO
MASTER_HOST=‘192.168.133.132‘,
MASTER_PORT=3306,
MASTER_USER=‘yuci1‘,
MASTER_PASSWORD=‘123456‘,
MASTER_LOG_FILE=‘mysql-bin.000002‘,
MASTER_LOG_POS=333;
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/8C/FE/wKioL1iBefbyXmjpAAAqfOGjtA4963.png" height="246" />
在 Master 1 上啟動 slave,並查看
start slave;
show slave status\G
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M02/8D/01/wKiom1iBefeBp94xAABMBFGRDFE203.png" height="295" />
到此為止,雙主的環境就算是搭建完成了,接下來測試一下。
先在 Master 1 上建立資料庫 yucitest1
create database yucitest1;
show databases;
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/8D/01/wKiom1iBeffje9MJAAAlj8O24TE345.png" height="235" />
在 Master 2 上查看所有資料庫,可以看到剛才建立的 yucitest1 ,將其刪除,建立 yucitest2 資料庫
show databases;
drop database yucitest1;
create database yucitest2;
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/8D/01/wKiom1iBefijpenIAAAt2pqbcDw383.png" height="283" />
返回 Master 1 看到 yucitest2 ,環境搭建成功
show databases;
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/8C/FE/wKioL1iBefjDEEd7AAAVq06xfA8653.png" height="193" />
本文出自 “LULU” 部落格,請務必保留此出處http://aby028.blog.51cto.com/5371905/1893336
MySQL主主同步