標籤:mysql 雙向同步
Centos 下 Mysql 雙向同步配置
一,環境
Master:Centos
Ip:172.16.100.5
Slave:Centos
Ip:172.16.100.6
根據安裝方法的不同,資料庫相關路徑也不太一致,
YUM安裝資料庫存放路徑/var/lib/mysql,
資料庫設定檔路徑/usr/share/mysql,
資料庫相關命令/usr/bin.
1,查看主程式庫伺服器/usr/share/mysql目錄下的cnf檔案
/usr/share/mysql/*.cnf
.cnf配置參考:
My-small.cnf 記憶體少於或等於64M,只提供很少資料庫服務
My-medium.cnf 記憶體在32M-64M之間而且和其他服務一起使用,如web
My-large.cnf 記憶體在512M主要提供資料庫服務
My-huge.cnf 記憶體在1G-2G,主要提供資料庫服務
My-innodb-heavy-4G.cnf 記憶體有4G,主要提供較大負載資料庫服務(一般伺服器使用這個)
2,複製檔案到/etc下並更名為my.cnf
# cp /usr/share/mysql/my-innodb-heavy-4G.cnf /etc/my.cnf
二;
Master端
1,進入mysql,建立一個資料庫testdb;
Mysql>create database testdb;
2,建立一個用來同步的使用者,指定只能在172.26.100.6登入;
Mysql>grant replication slave on *.* to ‘testdb‘@‘172.26.100.6‘ identified by ‘123456‘;
3,開啟my.cnf,並添加如下欄位
# vi /etc/my.cnf
Server-id = 1
Log-bin = log
Binlog-do-db = testdb //需要同步的資料庫,如果沒有本行,即表示同步所有資料庫
Master-host = 172.16.100.6
Master-user = testdb
Master-password = 123456
Master-port = 3306
Master-connect-retry = 10
Replicate-do-db = testdb //需要接收的資料庫,如有多個資料庫,每個資料庫一行
4,重啟master機的mysql服務
Service mysqld restart
5,進入mysql,執行
Mysql>slave start;
三,
Slave端
1,進入mysql,建立一個資料庫testdb
Mysql>create database testdb;
2,建立一個用來同步的使用者,指定只能在172.26.100.5登入
Mysql>grant replication slave on *.* to ‘testdb‘@‘172.26.100.5‘ identified by ‘123456‘;
3,開啟my.cnf,並添加如下欄位
# vi /etc/my.cnf
Server-id = 2
Log-bin = log
Binlog-do-db = testdb
Master-host = 172.26.100.5
Master-user = testdb
Master-password = 123456
Master-port = 3306
Master-connect-retry = 10
Replcate-do-db = testdb //需要接收的資料庫,如有多個資料庫,每個資料庫一行
4,然後重啟slave機的mysql
Service mysqld restart
5,在slaves機中進入mysql
Mysql>start slave;
四;
資料同步;
1,雙向同步就是把單向同步反過來走一遍。
在master伺服器上進入mysql
mysql> show master status;
+------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------+----------+--------------+------------------+
| log.000011 | 4514237 | testdb | mysql |
+------------+----------+--------------+------------------+
1 row in set (0.00 sec)
記錄下log.000011 和4514237
2,在slave伺服器上執行
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
3,輸入如下命令:
mysql> change master to
-> master_host = ‘172.26.100.5‘,
-> master_user = ‘testdb‘,
-> master_password = ‘123456‘,
-> master_log_file = ‘log.000011‘,
-> master_log_pos = 4514237;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave; //開始同步
Query OK, 0 rows affected (0.00 sec)
4、在slave伺服器上進入mysql,查看資料。
mysql> show master status;
+------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------+----------+--------------+------------------+
| log.000001 | 4502348 | testdb | |
+------------+----------+--------------+------------------+
1 row in set (0.00 sec)
記錄下log.000001 和4502348
5、在master伺服器上執行
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
6,輸入如下命令:
mysql> change master to
-> master_host = ‘172.26.100.6‘,
-> master_user = ‘testdb‘,
-> master_password = ‘123456‘,
-> master_log_file = ‘log.000001‘,
-> master_log_pos = 4502348;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave; //開始同步
Query OK, 0 rows affected (0.00 sec)
五;測試。
mysql> slave processlist \G;
Slave_IO_Running 和 Slave_SQL_Running 都為 yes,表示配置成功。
本文出自 “logs” 部落格,請務必保留此出處http://51log.blog.51cto.com/6076767/1852738
MySQL 雙向同步