本篇內(nèi)容主要講解“Mysql搭建主從復(fù)制的方法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Mysql搭建主從復(fù)制的方法”吧!
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供許昌網(wǎng)站建設(shè)、許昌做網(wǎng)站、許昌網(wǎng)站設(shè)計(jì)、許昌網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、許昌企業(yè)網(wǎng)站模板建站服務(wù),10多年許昌做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。主庫(kù):192.168.1.1
從庫(kù):192.168.1.2
數(shù)據(jù)文件存放位置:/data/datafile
待同步數(shù)據(jù)庫(kù):repdb
一.設(shè)置MASTER
1.修改my.cnf
# vi /etc/my.cnf
server-id = 1
log-bin
binlog-ignore-db=mysql
2.賦予SLAVE權(quán)限帳號(hào),允許用戶在SLAVE上LOAD TABLE和LOAD DATA
mysql>GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO user_rep@192.168.1.2 IDENTIFIED BY 'rep123';
3.鎖主庫(kù)表
mysql>FLUSH TABLES WITH READ LOCK;
4.顯示主庫(kù)信息
記錄File和Position,從庫(kù)設(shè)置將會(huì)用到
mysql>SHOW MASTER STATUS G;
*************************** 1. row ***************************
File: mysql-bin.000030
Position: 391156558
Binlog_Do_DB:
Binlog_Ignore_DB:
5.另開(kāi)一個(gè)終端,打包主庫(kù)
/data/datafile/ # tar cvf repdb.tar repdb
二.設(shè)置SLAVE
1.傳輸拿到主庫(kù)包,解包
/data/datafile/ # scp 192.168.1.1:/data/datafile/repdb.tar .
/data/datafile # tar xvf repdb.tar
2.解鎖主庫(kù)表
mysql>UNLOCK TABLES;
3.查看修改repdb文件夾權(quán)限
/data/datafile # chown mysql:mysql repdb -R
4.修改my.cnf
# vi /etc/my.cnf
#slave
server-id=2
master-host=192.168.1.1
master-user=user_rep
master-password=rep123
master-port=3306
replicate-do-db=repdb
5.驗(yàn)證連接MASTER
# mysql -h292.168.1.1 -uuser_rep -prep123
mysql>show grants;
+------------------------------------------------------------------------------+
| Grants for user_rep@192.168.1.2 |
+------------------------------------------------------------------------------+
| GRANT SELECT, FILE, REPLICATION SLAVE ON *.* TO user_rep@192.168.1.2 IDENTIFIED BY
PASSWORD '*9FF2C222F44C7BBA5CC7E3BE8573AA4E1776278C' |
+------------------------------------------------------------------------------+
6.本地登錄mysql
設(shè)置連接MASTER MASTER_LOG_FILE為主庫(kù)的File
MASTER_LOG_POS為主庫(kù)的Position
mysql>CHANGE MASTER TO
MASTER_HOST='192.168.1.1',
MASTER_USER='user_rep',
MASTER_PASSWORD='rep123',
MASTER_LOG_FILE='mysql-bin.000030',
MASTER_LOG_POS=391156558;
7.啟動(dòng)SLAVE服務(wù)
mysql>slave start;
8.本地登錄mysql,查看SLAVE狀態(tài)
mysql>SHOW SLAVE STATUS G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.1
Master_User: user_rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000001
Read_Master_Log_Pos: 98
Relay_Log_File: relay.000003
Relay_Log_Pos: 232
Relay_Master_Log_File: binlog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 98
Relay_Log_Space: 232
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
Slave_IO_Running 和Slave_SQL_Running 兩列的值都為 "Yes",表明 Slave 的 I/O 和 SQL 線程都在正常運(yùn)行.
到此主從庫(kù)搭建成功. [@more@]
從庫(kù)SLAVE啟動(dòng)問(wèn)題
由于一些錯(cuò)誤操作導(dǎo)致CHANGE MASTER和SLAVE服務(wù)無(wú)法啟動(dòng),系統(tǒng)報(bào)錯(cuò)如下:
Could not initialize master info structure; more error messages can be found in the MySQL error log.
無(wú)法初始化master info結(jié)構(gòu);MySQL錯(cuò)誤日志記錄了更詳細(xì)的錯(cuò)誤信息.
解決方法:
1.查看MySQL錯(cuò)誤日志,查看原因.
如:同步的上一個(gè)Position是多少.
很多情況下無(wú)法啟動(dòng)服務(wù)是由于mysql識(shí)別的同步始終停留在上一個(gè)Position上.
2.查看master.info和relay-log.info
master.info 記錄MASTER相關(guān)信息
14
mysql-bin.000030
391156558
192.168.1.1
user_rep
rep123
3306
60
0
relay-log.info 記錄當(dāng)前同步日志信息
235
mysql-bin.000030
391156558
3.停止myslq服務(wù),刪除master.info和relay-log.info
# service mysql stop
/data/datafile/ # rm master.info
/data/datafile/ # rm relay-log.info
4.啟動(dòng)mysql服務(wù)
# service mysql start
5.重新CHANGE MASTER,重新啟動(dòng)SLAVE服務(wù).
問(wèn)題應(yīng)該就可以解決了.
到此,相信大家對(duì)“Mysql搭建主從復(fù)制的方法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!