MySQL主從同步、讀寫分離配置步驟
2024-07-24 12:49:17
供稿:網友
現在使用的兩臺服務器已經安裝了MySQL,全是rpm包裝的,能正常使用。
為了避免不必要的麻煩,主從服務器MySQL版本盡量保持一致;
環(huán)境:192.168.0.1 (Master)
192.168.0.2 (Slave)
MySQL Version:Ver 14.14 Distrib 5.1.48, for pc-linux-gnu (i686) using readline 5.1
1、登錄Master服務器,修改my.cnf,添加如下內容;
server-id = 1 //數據庫ID號, 為1時表示為Master,其中master_id必須為1到232–1之間的一個正整數值;
log-bin=mysql-bin //啟用二進制日志;
binlog-do-db=data //需要同步的二進制數據庫名;
binlog-ignore-db=mysql //不同步的二進制數據庫名;這個同步后聽說很麻煩,我沒有同步;
log-bin=/var/log/mysql/updatelog //設定生成的log文件名;
log-slave-updates //把更新的記錄寫到二進制文件中;
slave-skip-errors //跳過錯誤,繼續(xù)執(zhí)行復制;
2、建立復制所要使用的用戶;
mysql>grant replication slave on *.* to test@192.168.0.2 identified by '********'
3、重啟mysql;
/usr/bin/mysqladmin -uroot shutdown;
/usr/bin/mysql_safe &
4、現在備份Master上的數據;
鎖定后我直接tar.gz data這個庫文件;
mysql>FLUSH TABLES WITH READ LOCK;
cd /var/lib/mysql
tar data.tar.gz data
接著直接執(zhí)行了遠程scp;
scp ./data.tar.gz root@192.168.0.2:/var/lib/mysql
5、登錄Slave數據庫服務器,修改my.cnf;
server-id = 3 //2已經被用在另一個服務器上了,如果以后要再加Slave號接著往后數就OK了;
log-bin=mysql-bin
master-host = 192.168.0.1
master-user = test
master-password = ******
master-port = 3306
master-connect-retry=60 //如果發(fā)現主服務器斷線,重新連接的時間差;
replicate-ignore-db=mysql //不需要備份的數據庫;
replicate-do-db=data //需要備份的數據庫
log-slave-update
slave-skip-errors
6、解壓剛才從Master scp過來的文件,此處不用改權限、屬主,默認沒有改變,可以根據實際情況進行修改;
7、上述完成后,可以啟動slave了;查看slave狀態(tài);
mysql>slave start;
mysql>show slave status/G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.1
Master_User: test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: updatelog.000001
Read_Master_Log_Pos: 106
Relay_Log_File: onlinevc-relay-bin.000013
Relay_Log_Pos: 1069
Relay_Master_Log_File: updatelog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: data
Replicate_Ignore_DB: mysql
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: 106
Relay_Log_Space: 1681
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No