本文主要給大家介紹Mysql5.5版本的一主多從配置講義,希望可以給大家補(bǔ)充和更新些知識(shí),如有其它問(wèn)題需要了解的可以持續(xù)在創(chuàng)新互聯(lián)行業(yè)資訊里面關(guān)注我的更新文章的。
1)實(shí)驗(yàn)拓?fù)湫畔?/p>
mysql版本:5.5.32(基于源代碼安裝)
系統(tǒng)版本:CentOS6.7
10.10.10.129: mysql的主云服務(wù)器
10.10.10.130: mysql的從服務(wù)器
目的:
本地主從:
10.10.10.129 3306 --> 10.10.10.10.129 3307
異地主從:
10.10.10.129 3306 --> 10.10.10.10.130 3306
2)主庫(kù)上的配置
a、準(zhǔn)備mysql的測(cè)試環(huán)境
先準(zhǔn)備好mysql多實(shí)例環(huán)境,之前我已經(jīng)寫(xiě)過(guò)博客,這里就不重復(fù)了,可以參考http://molewan.blog.51cto.com/287340/1860198
b、在主庫(kù)(10.10.10.129)上開(kāi)啟binlog記錄功能
# grep server-id /data/3306/my.cnf server-id = 1 # grep server-id /data/3307/my.cnf server-id = 3 # grep log-bin /data/3306/my.cnf log-bin = /data/3306/mysql-bin登陸數(shù)據(jù)庫(kù)進(jìn)行查看:
# mysql -u root -S /data/3306/mysql.sock -predhat12345 mysql> show variables like 'log_bin'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ 1 row in set (0.01 sec)c、建立用于主從復(fù)制的賬號(hào)
# mysql -u root -S /data/3306/mysql.sock -predhat12345 mysql> grant replication slave on *.* to 'rep'@'10.10.10.%' identified by 'redhat12345'; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from mysql.user; +------+------------+ | user | host | +------+------------+ | wan | % | | rep | 10.10.10.% | | wan | 10.10.10.% | | root | 127.0.0.1 | | root | ::1 | | | C67-X64-A8 | | root | C67-X64-A8 | | | localhost | | root | localhost | +------+------------+ 9 rows in set (0.00 sec) mysql> select user,host from mysql.user where user='rep'; +------+------------+ | user | host | +------+------------+ | rep | 10.10.10.% | +------+------------+ 1 row in set (0.00 sec)d、實(shí)現(xiàn)對(duì)主數(shù)據(jù)庫(kù)鎖表只讀(當(dāng)前窗口不要關(guān)閉)
mysql> flush table with read lock; Query OK, 0 rows affected (0.00 sec) 重新打開(kāi)一個(gè)窗口,鎖表后查看主庫(kù)狀態(tài): # mysql -uroot -S /data/3306/mysql.sock -predhat12345 mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 337 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)e、使用mysqldump進(jìn)行數(shù)據(jù)庫(kù)備份:
# mysqldump -uroot -p'redhat12345' -S /data/3306/mysql.sock --events -A -B |gzip >/mysqlback/mysql_bak.$(date +%F).sql.gz # echo $? 0 # ls -l /mysqlback/ 總用量 144 -rw-r--r-- 1 root root 144402 10月 9 02:54 mysql_bak.2016-10-09.sql.gz # du -sh /mysqlback/mysql_bak.2016-10-09.sql.gz 144K/mysqlback/mysql_bak.2016-10-09.sql.gz 為了確保導(dǎo)出數(shù)據(jù)期間,數(shù)據(jù)庫(kù)沒(méi)有數(shù)據(jù)插入,導(dǎo)庫(kù)完畢可以再次檢查主庫(kù)狀態(tài)信息 # mysql -uroot -S /data/3306/mysql.sock -predhat12345 -e "show master status" +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 337 | | | +------------------+----------+--------------+------------------+f、導(dǎo)出數(shù)據(jù)完畢后,解鎖主庫(kù),恢復(fù)可寫(xiě)
mysql> unlock tables; Query OK, 0 rows affected (0.01 sec)3)從庫(kù)上的配置
a、10.10.10.129 3307數(shù)據(jù)庫(kù)上的配置
# cd /mysqlback/ # ls mysql_bak.2016-10-09.sql.gz # gzip -d mysql_bak.2016-10-09.sql.gz # ls mysql_bak.2016-10-09.sql # mysql -uroot -p'redhat12345' -S /data/3307/mysql.sock start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.10.10.129 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 337 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: 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: 337 Relay_Log_Space: 403 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: 1 1 row in set (0.00 sec) ERROR: No query specified主從復(fù)制是否成功,最關(guān)鍵的為下面的3項(xiàng)狀態(tài)參數(shù):
# mysql -uroot -predhat12345 -S /data/3307/mysql.sock -e "show slave status\G"|egrep "IO_Running|SQL_Running|_Behind_Master" Slave_IO_Running: Yes Slave_SQL_Running: Yes Seconds_Behind_Master: 0b、10.10.10.130 3306數(shù)據(jù)庫(kù)上的配置
# mkdir -p /mysqlback/ # scp /mysqlbackup/mysql_bak.2016-10-09.sql.gz 10.10.10.130:/mysqlback # gzip -d mysql_bak.2016-10-09.sql.gz # ls mysql_bak.2016-10-09.sql # mysql -uroot -p'redhat12345' -S /data/3306/mysql.sock start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.10.10.129 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 337 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: 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: 337 Relay_Log_Space: 403 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: 1 1 row in set (0.00 sec) ERROR: No query specified在10.10.10.130服務(wù)器上檢查
主從復(fù)制是否成功,最關(guān)鍵的為下面的3項(xiàng)狀態(tài)參數(shù):
# mysql -uroot -predhat12345 -S /data/3306/mysql.sock -e "show slave status\G"|egrep "IO_Running|SQL_Running|_Behind_Master" Slave_IO_Running: Yes Slave_SQL_Running: Yes Seconds_Behind_Master: 04)mysql主從同步測(cè)試
在10.10.10.129上查看數(shù)據(jù)庫(kù):
[root@mysql-master ~]# mysql -uroot -predhat12345 -S /data/3306/mysql.sock -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | dawnpro | | eip | | ems | | hangzhou_dawnpro | | mysql | | performance_schema | | wh610 | +--------------------+ [root@mysql-master ~]# mysql -uroot -predhat12345 -S /data/3307/mysql.sock -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | dawnpro | | eip | | ems | | hangzhou_dawnpro | | mysql | | performance_schema | | wh610 | +--------------------+在10.10.10.130上查看數(shù)據(jù)庫(kù):
[root@mysql-slave mysqlbackup]# mysql -uroot -predhat12345 -S /data/3306/mysql.sock -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | dawnpro | | eip | | ems | | hangzhou_dawnpro | | mysql | | performance_schema | | wh610 | +--------------------+通過(guò)對(duì)比,可以發(fā)現(xiàn),數(shù)據(jù)庫(kù)完全一致,然后我們?cè)谥鲙?kù)上新建一個(gè)blog的數(shù)據(jù)庫(kù)
[root@mysql-master mysqlback]# mysql -uroot -predhat12345 -S /data/3307/mysql.sock -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | blog | | dawnpro | | eip | | ems | | hangzhou_dawnpro | | mysql | | performance_schema | | wh610 | +--------------------+ [root@mysql-slave mysqlbackup]# mysql -uroot -predhat12345 -S /data/3306/mysql.sock -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | blog | | dawnpro | | eip | | ems | | hangzhou_dawnpro | | mysql | | performance_schema | | wh610 | +--------------------+ 主從同步到此完成5)注意事項(xiàng)
1)master與slave中的server-id要保持唯一 2)備份導(dǎo)出前要使用“flush table with read lock;”讓數(shù)據(jù)庫(kù)鎖表只讀,并且這個(gè)窗口不要關(guān)閉 3)slave上要start slave;接收信息 4)master與slave時(shí)間要保持一致看了以上關(guān)于Mysql5.5版本的一主多從配置講義,希望能給大家在實(shí)際運(yùn)用中帶來(lái)一定的幫助。本文由于篇幅有限,難免會(huì)有不足和需要補(bǔ)充的地方,如有需要更加專業(yè)的解答,可在官網(wǎng)聯(lián)系我們的24小時(shí)售前售后,隨時(shí)幫您解答問(wèn)題的。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。