本篇文章給大家分享的是有關(guān)mysql數(shù)據(jù)庫(kù)中怎么實(shí)現(xiàn)異步備份,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
創(chuàng)新互聯(lián)于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元北湖做網(wǎng)站,已為上家服務(wù),為北湖各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:189808205751.服務(wù)器狀態(tài)
服務(wù)器A:192.168.1.1
服務(wù)器B:192.168.1.2
2.創(chuàng)建同步用戶
主機(jī)域A:192.168.1.2 用戶名A:sync_a 密碼A:aaa
主機(jī)域B:192.168.1.1 用戶名B:sync_b 密碼B:bbb
至少分配以下權(quán)限grant replication slave
3.執(zhí)行flush privileges
4.停止MySQL
5.配置my.cnf(my.ini)
服務(wù)器A 服務(wù)器B
user = mysql教程
log-bin = mysql-bin
server-id = 1
binlog-do-db = test
binlog-ignore-db = mysql
replicate-do-db = test
replicate-ignore-db = mysql
log-slave-updates
slave-skip-errors = all
sync_binlog = 1 user = mysql
log-bin = mysql-bin
server-id = 2
binlog-do-db = test
binlog-ignore-db = mysql
replicate-do-db = test
replicate-ignore-db = mysql
log-slave-updates
slave-skip-errors = all
sync_binlog = 1
server-id必須
binlog-do-db和replicate-do-db表示需要同步的數(shù)據(jù)庫(kù)教程
binlog-ignore-db和replicate-ignore-db表示不需要同步的數(shù)據(jù)庫(kù)
請(qǐng)不要加入以下命令,該命令并不會(huì)解決uid跳號(hào)的問題,恰恰相反以下兩行命令才是導(dǎo)致uid跳號(hào)的罪魁禍?zhǔn)?br/>auto_increment_increment = 2
auto_increment_offset = 1
6.重新啟動(dòng)MySQL
7.進(jìn)入MySQL控制臺(tái)
服務(wù)器A:
show master status G
flush tables with read lock;
服務(wù)器B:
show master status G
flush tables with read lock;
同時(shí)記錄下兩臺(tái)服務(wù)器的File和Position,此處假設(shè):
A: File: mysql-bin.000001
Position: 001
B: File: mysql-bin.000002
Position: 002
服務(wù)器A:
change master to
-> master_host='192.168.1.2',
-> master_user='sync_b',
-> master_password='bbb',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=002;
服務(wù)器B:
change master to
-> master_host='192.168.1.1',
-> master_user='sync_a',
-> master_password='aaa',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=001;
此處填寫的數(shù)據(jù)完全相反
8.執(zhí)行show processlist G查看是否同步成功
方法二
兩臺(tái)服務(wù)器
192.168.1.1(A)
192.168.1.2(B)
先保證這mysql的版本是一致的,參考,否則復(fù)制中的異常情況很折騰人。
1.在兩臺(tái)mysql上創(chuàng)建用戶,設(shè)置權(quán)限
A上添加:
#grant replication slave,replication client,reload,super on *.* to identified by '123456' with grant option;//用于B訪問
B上:
#grant replication slave,replication client,reload,super on *.* to identified by '123456' with grant option;//用于A訪問
執(zhí)行 #flush privileges; 更新數(shù)據(jù)庫(kù)使用戶生效。
2.在/etc/my.cnf上進(jìn)行相關(guān)配置
A B
server-id = 1
master-host =192.168.1.2
master-user =sync_user
master-pass =123456
master-port =3306
master-connect-retry=60
replicate-do-db =db1
replicate-do-db =db2
replicate-ignore-db=mysql server-id = 2
master-host =192.168.1.1
master-user =sync_user
master-pass =123456
master-port =3306
master-connect-retry=60
replicate-do-db =db1
replicate-do-db =db2
replicate-ignore-db=mysql
注意
1.server_id必須為.
2.如果想要同時(shí)同步多個(gè)庫(kù),添加多行replicate-do-db,每行指定一個(gè)數(shù)據(jù)庫(kù)。不能使用replicate-do-db=db1,db2的形式
3.replicate-ignore-db:指定不進(jìn)行同步的數(shù)據(jù)庫(kù)。
保存后,重啟mysql
#mysqladmin -u root -p shutdown
#mysqld_safe --user=mysql
3.把兩臺(tái)服務(wù)器上需要同步的數(shù)據(jù)庫(kù)進(jìn)行拷貝,保證這兩臺(tái)數(shù)據(jù)庫(kù)初始狀態(tài)一致。
4.進(jìn)行雙向同步
雙向同步就是把單向同步反過來在做一遍,但一定要注意操作的順序,這是成功的關(guān)鍵
step1.在A上mysql shell中執(zhí)行
#show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000054 | 35 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
記錄下 mysql-bin.000054,和35
step2.在B上執(zhí)行:
#stop slave;//停止同步
#
CHANGE MASTER TO MASTER_HOST='192.168.1.1', MASTER_PORT=3306, MASTER_USER='sync_user', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000054', MASTER_LOG_POS=35;
#start slave;//開始同步
step3,執(zhí)行show slave statusG;如顯示如下內(nèi)容,表示同步設(shè)置成功。
Slave_IO_State: Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
step4:上一步?jīng)]有問題。則在B上繼續(xù)執(zhí)行show master status;
#show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 6854 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
step5:在A上執(zhí)行
#stop slave;//停止同步
#
CHANGE MASTER TO MASTER_HOST='192.168.1.2', MASTER_PORT=3306, MASTER_USER='sync_user', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=6854;
#start slave;//開始同步
step6:執(zhí)行show slave statusG;如顯示如下內(nèi)容,表示同步設(shè)置成功。
Slave_IO_State: Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如上述沒有啥問題。到此雙向同步設(shè)置完成。
方法三
一、準(zhǔn)備服務(wù)器
由于MySQL不同版本之間的(二進(jìn)制日志)binlog格式可能會(huì)不一樣,因此好的搭配組合是Master的MySQL版本和Slave的版本相同或者更低,Master的版本肯定不能高于Slave版本。
more.. | less.. | 本文中,我們假設(shè)主服務(wù)器(以下簡(jiǎn)稱Master)和從服務(wù)器(以下簡(jiǎn)稱Slave)的版本都是5.0.27,操作系統(tǒng)是RedHat 9。
假設(shè)同步Master的主機(jī)名為:master(IP:192.168.1.123),Slave主機(jī)名為:slave(IP:192.168.1.124),2個(gè)MySQL的basedir目錄都是/usr/local/mysql,datadir都是:/var/lib/mysql。
二、設(shè)置同步服務(wù)器
1、設(shè)置同步Master
修改 my.cnf 文件,在
# Replication Master Server (default)
# binary logging is required for replication
添加如下內(nèi)容:
#log-bin=/var/log/mysql/updatelog
server-id = 1
binlog-do-db=discuz
binlog-ignore-db=mysql
重啟MySQL,創(chuàng)建一個(gè)MySQL帳號(hào)為同步專用
# /usr/local/mysql/bin/mysql -u root -p
mysql> GRANT REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
如果想要在Slave上有權(quán)限執(zhí)行 "LOAD TABLE FROM MASTER" 或 "LOAD DATA FROM MASTER" 語句的話,必須授予全局的 FILE 和 SELECT 權(quán)限:
mysql>GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
mysql> FLUSH PRIVILEGES ;
2、設(shè)置同步Slave
修改my.cnf文件,添加
server-id = 2
master-host = 192.168.1.123
master-user = back
master-password = back
master-port = 3306
replicate-ignore-db=mysql
replicate-do-db=discuz
重啟MySQL
3、啟動(dòng)同步
在主服務(wù)器master MySQL命令符下:
# /usr/local/mysql/bin/mysql -u root -p
mysql> show master status;
顯示(當(dāng)然這個(gè)是我機(jī)器的情況,你的不可能跟我一樣哈,只是個(gè)例子):
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| mysql-bin.000009 | 98 | discuz | mysql |
+------------------+----------+-------------------+------------------+
在從服務(wù)器master MySQL命令符下:
# /usr/local/mysql/bin/mysql -u root -p
mysql> slave stop;
mysql> change master to master_host='192.168.1.123', master_user='back', master_password='back', master_log_file='mysql-bin.000009', master_log_pos=98;
mysql> slave start;
用show slave statusG;看一下從服務(wù)器的同步情況
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果都是yes,那代表已經(jīng)在同步
往表里面寫點(diǎn)數(shù)據(jù)測(cè)試一下看是否同步成功,如果不成功,絕對(duì)不是你的RP問題,再檢查一下操作步驟!
4、設(shè)置雙向同步
修改slave服務(wù)器的my.cnf,添加
log-bin=/var/log/mysql/updatelog
binlog-do-db=discuz
binlog-ignore-db=mysql
重啟MySQL,創(chuàng)建一個(gè)MySQL帳號(hào)為同步專用
mysql> GRANT REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
mysql> GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
mysql> FLUSH PRIVILEGES ;
修改master服務(wù)器的my.cnf,添加
master-host = 192.168.1.124
master-user = back
master-password = back
master-port = 3306
replicate-ignore-db=mysql
replicate-do-db=discuz
重啟MySQL
在主服務(wù)器slave MySQL命令符下:
show master status;
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| mysql-bin.000013 | 98 | discuz | mysql |
+------------------+----------+-------------------+------------------+
在服務(wù)器A MySQL命令符下:
mysql> slave stop;
mysql> change master to master_host='192.168.1.124', master_user='back', master_password='back', master_log_file='mysql-bin.000013', master_log_pos=98;
mysql> slave start;
其實(shí)也就是A->B單向同步的反向操作!雙向同步,就這么簡(jiǎn)單啦!
提示:如果修改了主服務(wù)器的配置,記得刪除從服務(wù)器上的master.info文件。否則從服務(wù)器使用的還是老配置,可能會(huì)導(dǎo)致錯(cuò)誤。
-----------------------------------------------------------------------------------
注意:關(guān)于要復(fù)制多個(gè)時(shí),binlog-do-db和replicate-do-db選項(xiàng)的設(shè)置,網(wǎng)上很多人說是用半角逗號(hào)分隔,經(jīng)過測(cè)試,這樣的說法是錯(cuò)誤的,MySQL官方文檔也明確指出,如果要備份多個(gè)數(shù)據(jù)庫(kù),只要重復(fù)設(shè)置相應(yīng)選項(xiàng)就可以了。
比如:
binlog-do-db=a
binlog-do-db=b
replicate-do-db=a
replicate-do-db=b
以上就是mysql數(shù)據(jù)庫(kù)中怎么實(shí)現(xiàn)異步備份,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。