轉載位址:http://mopishv0.blog.163.com/blog/static/544559322011101432746529/?suggestedreading&wumii
1. 主從關系的同步
master端 192.168.5.10
slave端 192.168.5.4
修改master端的/etc/my.cnf檔案
[mysqld]:
log-bin
server-id = 1
進入mysql
建立一個要同步的資料庫abc
create database abc;
建立一個用來同步的使用者,指定隻能在192.168.5.4登入
grant file on *.* to [email protected]'192.168.5.4 identified by '123456';
grant all privileges on backup.* to [email protected]’192.168.5.4’ identified by ‘123456’;
flush privileges;
重新開機mysql
修改slave端的/etc/my.cnf檔案
[mysqld]:
server_id = 2
log-bin = mysql-bin
master-host = 192.168.5.10
master-user = bak
master-password = '123456' (如果為空也要打上兩個單引号)
master-port = 3306 (根據具體設定的端口号填)
master-connect-retry = 10 連接配接重試次數
replicate-do-db = abc 要接收的資料庫名,如果有多個資料庫,每個資料庫一行
replicate-ignore-db= mysql 設定不要接收的資料庫,每個資料庫一行 (一般這條可以不寫)
重新開機mysql
slave start;
配置成功後會在mysql目錄下生成master.info,如果要更改slave設定,要先将master.info删除,才會起作用。
然後進入mysql用show slave status 或show master status 檢視同步情況
3、 雙向同步
master端的設定
[mysqld]:
log-bin
server-id = 1
sql-bin-update-same 同步模式 ,在mysql5以上的版本都不需要這句,否則會啟動不了
mysql服務
binlog-do-db= abc 設定同步資料庫,如果有多個資料庫,每個資料庫一行
binlog-ignore-db = mysql 設定不要同步的資料庫,如有多個資料庫,每個資料庫一行
master-host=192.168.5.4
master-user=bak
master-password='123456'
master-port=3306
master-connect-retry=10
replicate-do-db=abc 設定要接收的資料庫,如有多個資料庫,每個資料庫一行
replicate-ignore-db= mysql 設定不要接收的資料庫,每個資料庫一行 (一般這條可以不寫)
log-slave-updates 在mysql5以上的版本都不需要這句
進入mysql
建立一個資料庫abc
create database abc;
建立一個用來同步的使用者,指定隻能在192.168.5.4登入
grant file on *.* to [email protected]'192.168.5.4 identified by '123456';
重新開機mysql服務
slave start;
slave設定
log-bin
server-id = 2
binlog-do-db= abc 設定同步資料庫,如果有多個資料庫,每個資料庫一行
binlog-ignore-db = mysql 設定不要同步的資料庫,如有多個資料庫,每個資料庫一行
sql-bin-update-same 同步模式 ,在mysql5以上的版本都不需要這句,否則會啟動不了mysql服務
master-host=192.168.5.10
master-user= bak
master-password='123456'
master-port=3306
master-connect-retry=10
replicate-do-db=abc 設定要接收的資料庫,如有多個資料庫,每個資料庫一行
replicate-ignore-db= mysql 設定不要接收的資料庫,每個資料庫一行 (一般這條可以不寫)
log-slave-updates 在mysql5以上的版本都不需要這句
進入mysql
建立一個資料庫abc
create database abc;
建立一個用來同步的使用者,指定隻能在192.168.5.10登入
grant file on *.* to [email protected]'192.168.5.10 identified by '123456';
重新開機mysql服務
slave start;
注意,如果有一端修改了資料後,另一端接收不到,手工執行指令load data from master;就可以了
如果因為誤删了二進制日志檔案導緻無法同步,則執行
reset slave;
stop slave;
start slave;
版本5.5以上:
不支援通過my.cnf設定master
MySQL> change master to master_host='192.168.72.128',master_user='replication',master_password='replication',master_log_file='MySQL-bin.000003',master_log_pos=374;
用此語句設定master