有server1=192.168.0.17 和 server2=192.168.0.18倆臺mysql server
其中;server1主機上配置server-id=1 server2上配置:server-id=2
17 Server1上mysql的配置:
linux 內核:
#uname -r
2.6.32-220.el6.x86_64
2.6.32-220.el6.x86_64
mysql版本:
#mysql -u root -p
Enter password:輸入mysql密碼
Enter password:輸入mysql密碼
mysql> status
--------------
...............................
...............................
mysql Ver 14.14 Distrib 5.5.22, for Linux (x86_64) using EditLine wrapper //數據版本號
................................
................................
--------------
//創建mysql同步用戶,只允許bak這個用戶通過192.168.0.18這臺主機上連接。
mysql>grant replication slave on *.* to [email protected] identified by 'wdmysql';
編輯mysq1主配置文件,在/etc/my.cnf中添加以下內容
server-id = 1 #最好是找到這一行,省的手工添加避免衝突,
character_set_server=utf8
character_set_server=utf8
log-bin = mysql-bin
binlog-ignore-db = mysql
binlog-do-db = bbsdb
binlog-do-db = phpmyadmin
replicate-do-db = phpmyadmin
binlog-ignore-db = test
# master-host = 192.168.0.18
#master-user = bak
#master-password = wdmysql
#master-port = 3306
#master-connect-retry = 60
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-do-db = bbsdb
binlog-ignore-db = mysql
binlog-do-db = bbsdb
binlog-do-db = phpmyadmin
replicate-do-db = phpmyadmin
binlog-ignore-db = test
# master-host = 192.168.0.18
#master-user = bak
#master-password = wdmysql
#master-port = 3306
#master-connect-retry = 60
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-do-db = bbsdb
//重新啓動mysql server
#server mysql restart
18 Server2上mysql的配置:
linux 內核:
#uname -r
2.6.32-220.el6.x86_64
2.6.32-220.el6.x86_64
mysql版本:
#mysql -u root -p
Enter password:輸入mysql密碼
Enter password:輸入mysql密碼
mysql> status
--------------
...............................
...............................
mysql Ver 14.14 Distrib 5.5.22, for Linux (x86_64) using EditLine wrapper //數據版本號
................................
................................
--------------
創建mysql同步用戶,只允許bak這個用戶通過192.168.0.17這臺主機上連接。
grant replication slave on *.* to [email protected] identified by 'wdmysql';
編輯mysq2主配置文件,在/etc/my.cnf中添加以下內容
[mysqld] #找到這一行,在這個下面添加以下內容;
server-id = 2 #//最好是找到這一行,省的手工添加衝突,
character_set_server=utf8 #這個對本次試驗沒有時間用處,在網上看見有人改mysql字符集報錯,隨便我在這給加上了
#master-host = 192.168.0.17 # 指定主服務器IP地址
#master-user = bak # 指定在主服務器上可以進行同步的用戶名
#master-password = wdmysql # 密碼
#master-port = 3306 # 同步所用端口
#master-connect-retry = 60 # 斷點從新連接時間
replicate-ignore-db = mysql # 屏蔽對mysql庫的同步
replicate-ignore-db = test # 屏蔽對test庫的同步
binlog-ignore-db =mysql
#master-user = bak # 指定在主服務器上可以進行同步的用戶名
#master-password = wdmysql # 密碼
#master-port = 3306 # 同步所用端口
#master-connect-retry = 60 # 斷點從新連接時間
replicate-ignore-db = mysql # 屏蔽對mysql庫的同步
replicate-ignore-db = test # 屏蔽對test庫的同步
binlog-ignore-db =mysql
replicate-do-db = bbsdb # 同步的數據庫的名稱
binlog-do-db = bbsdb # 二進制需要同步的數據庫名
replicate-do-db = phpmyadmin # 同步的數據庫的名稱
binlog-do-db = phpmyadmin # 二進制需要同步的數據庫名
binlog-ignore-db = test
binlog-do-db = bbsdb # 二進制需要同步的數據庫名
replicate-do-db = phpmyadmin # 同步的數據庫的名稱
binlog-do-db = phpmyadmin # 二進制需要同步的數據庫名
binlog-ignore-db = test
//上面帶有 "master_" 都被我註釋掉了,因爲我的mysql不支持這些參數;
然後重新啓動mysql服務,
#server mysql restart
//下面的步驟都需要在倆臺mysql上操作一遍;
#mysql -u root -p
Enter password:輸入mysql密碼
Enter password:輸入mysql密碼
..................
mysql> stop slave;
mysql> start slave;
mysql> show slave status\G
//主要看這兩行,是否都爲“yes”,如果不是,請查看日誌文件,一般都有提示;
.................................
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Slave_SQL_Running: Yes
....................................
//一般情況Slave_IO_Running:會出現NO,Slave_SQL_Running一般沒事,如果出現以下情況請執行以下操作;
先登錄mysql。執行以下命令。
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.0.18
Master_User: bak
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 107
Relay_Log_File: server7-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: No
Slave_SQL_Running: Yes
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.0.18
Master_User: bak
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 107
Relay_Log_File: server7-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB: phpmyadmin,bbsdb
Replicate_Ignore_DB: mysql,test
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 107
Relay_Log_Space: 411
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
1 row in set (0.00 sec)
Replicate_Ignore_DB: mysql,test
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 107
Relay_Log_Space: 411
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
1 row in set (0.00 sec)
然後登錄server1主數據庫,允許從數據庫同步相關數據;
mysql> grant replication slave on *.* to bak@192.168.0.18 identified by 'wdmysql';
mysql> grant replication slave on *.* to bak@192.168.0.18 identified by 'wdmysql';
再次重啓從數據庫,登錄進去查看狀態時,如果還是不行的話再繼續往下操作。
mysql> stop slave;
//先到另一臺mysql上查看
mysql> show master status;
+------------------+----------+------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+------------------+------------------+
| mysql-bin.000003 | 107 | bbsdb,phpmyadmin | mysql,test |
+------------------+----------+------------------+------------------+
1 row in set (0.00 sec)
+------------------+----------+------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+------------------+------------------+
| mysql-bin.000003 | 107 | bbsdb,phpmyadmin | mysql,test |
+------------------+----------+------------------+------------------+
1 row in set (0.00 sec)
//在server1上操作
mysql> change master to master_host='192.168.0.18',master_user='bak',master_password='wdmysql',master_log_file='mysql-bin.000003',master_log_pos=107;
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.18
Master_User: bak
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 107
Relay_Log_File: server7-relay-bin.000003
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: phpmyadmin,bbsdb
Replicate_Ignore_DB: mysql,test
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 107
Relay_Log_Space: 557
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
1 row in set (0.00 sec)
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.18
Master_User: bak
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 107
Relay_Log_File: server7-relay-bin.000003
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: phpmyadmin,bbsdb
Replicate_Ignore_DB: mysql,test
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 107
Relay_Log_Space: 557
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
1 row in set (0.00 sec)
然後在server2上面再操作一遍,這樣就可以實現mysql的實時同步了。
可以在bbs數據庫創建表,或者創建phpmyadmin數據庫上創建表。
然後到另一臺mysql上查看是否多了一個新表。如果有實驗成功~