準(zhǔn)備工作:
master機(jī)和slave機(jī)的相關(guān)配置
1、修改master機(jī)器中mysql配置文件my.cnf,該文件在/etc目錄下
在[mysqld]配置段添加如下字段
server-id=1
log-bin=log
binlog-do-db=repl //需要同步的數(shù)據(jù)庫,如果沒有本行,即表示同步所有的數(shù)據(jù)庫 此參數(shù)可以重復(fù)設(shè)置,此參數(shù) 可能會(huì)出現(xiàn)問題,盡量不用,過濾數(shù)據(jù) 設(shè)置到從庫
binlog-ignore-db=mysql //被忽略的數(shù)據(jù)庫 同上 最好不用 過濾設(shè)置到從庫
修改slave機(jī)中mysql配置文件
同樣在[mysqld]字段下添加如下內(nèi)容
server-id=2
master-host=192.168.1.222
master-user=repl
master-password=123456
master-port=3306
master-connect-retry=60
# replicate-ignore-db=mysql 忽略的數(shù)據(jù)庫 同上
# master-connect-retry=60 如果從服務(wù)器發(fā)現(xiàn)主服務(wù)器斷掉,重新連接的時(shí)間差(秒)
#replicate-do-db=repl //同步的數(shù)據(jù)庫(需要備份的數(shù)據(jù)庫名),不寫本行 表示 同步所有數(shù)據(jù)庫
#建議用下面兩個(gè)參數(shù)
replicate-wild-do-table=db_name.%
replicate-wild-ignore-table=mysql.%
然后重啟slave機(jī)的mysql
————————————————————————————————-
1、登錄mysql 首先 查看 數(shù)據(jù)庫的版本
>select version();
2、檢查主庫 和 從庫的 server_id
>show global variables like ‘server_id’;
3、查看主庫的二進(jìn)制文件是否開啟
>show global variables like ‘log_bin’;
若沒有開啟,那么需要重啟數(shù)據(jù)庫的:
(1)在my.cnf參數(shù)文件中[mysqld]中添加
log-bin = mysql-bin
(2)重啟MySQL數(shù)據(jù)庫。
4、檢查到從庫的網(wǎng)絡(luò)及端口是否可達(dá)
#netstat -tnlp|grep mysqld
#telnet 192.168.230.71 3306
5、在主庫創(chuàng)建同步帳號并授權(quán) replication
>create user ‘repl’@’host’ identified by ‘password’;
>grant replication slave,replication client on *.* to ‘repl’@’host’;
6、再從庫中測試是否可以使用repl用戶登錄
#mysql -urepl -ppassword -hhost -P3306
7、記錄主庫master上當(dāng)前二進(jìn)制日志名和偏移量
主要是用于slave中指定開始恢復(fù)的位置。
>show master statusG
8、備份數(shù)據(jù)庫
啟動(dòng)主從復(fù)制功能
slave start;
查看主從信息
show slave statusG;
如果打印的信息中slave_IO_Running:yes和Slave_SQL_Runnning:yes 則表示配置成功