1. 程式人生 > >mysql主從復制實戰——詳細

mysql主從復制實戰——詳細

mysql

mysql主從原理:

1)至少需要2臺數據庫服務器,一主一從,master開啟bin-log功能。(bin-log功能用戶記錄主控增 加、刪除、修改、更新SQL的語句。)

2)異步復制的過程,有延遲,毫秒級別(延遲和你的網絡和性能、數據庫的量級有關),開啟3個線程。分別是master開啟io線程,slave開啟io線程、sql線程。

3)從庫啟動 salve start,通過io線程、用戶名和密碼去連接master,master接收請求後,master io線程負責將bin-log內容position位置點數據發給salve端。

4)slave io線程收到數據之後,會將內容追加到本地relay-log中繼日誌,同時會產生master.info文件(這次從哪臺機器同步,用戶名、密碼、bin-log文件名、position位置).

5)slave SQL線程實時監測relay-log,如果這個日誌內容有更新,解析文件中的SQL語句,在本地去執行。

實戰:

1)master配置文件中開啟bin-log,設置server-id

2)授權同步用戶和密碼

3)slave執行change master綁定主庫

192.168.1.155(master)

192.168.1.156(salve)

註:關掉服務器的防火墻和setenforce 0

主庫配置:

1.在mysql配置文件中加入

log-bin=mysql-bin(開啟bin-log)

server-id=1(區分主從)

2.重啟數據庫

/etc/init.d/mysql restart

3.進入數據庫,創建一個新的用戶,並授權允許同步

create user [email protected] identified by ‘123456‘;(創建用戶)

grant replication slave on *.* to [email protected] identified by ‘123456‘;(授權給從庫)

4.查看主庫狀態

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 | 990 | | | |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.01 sec)

從庫配置:

5.修改從庫配置文件

server-id = 2

6.重啟數據庫

/etc/init.d/mysql restart

7.在從服務器上指定master IP和同步的pos點

進入數據庫:

change master to master_host=‘192.168.1.155‘ ,master_user=‘tongbu‘ ,master_password=‘123456‘ ,master_log_file=‘mysql-bin.000001‘ ,master_log_pos=990;

註:如果報錯

ERROR 3021 (HY000): This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL ‘‘ first

說明slave正在運行,stop slave;(關閉slave)

7.啟動slave

start slave;

8.測試,在主庫創建一個數據庫,查看從庫是否同步過來。

技術分享(本實驗是成功的)


本文出自 “帥小欣” 博客,請務必保留此出處http://jiaxinwang.blog.51cto.com/12273793/1946054

mysql主從復制實戰——詳細