mysql教程兩服務器數據庫教程實現同步數據備份方法
1.服務器狀態
服務器A:192.168.1.1
服務器B:192.168.1.2
2.創建同步用戶
主機域A:192.168.1.2 用戶名A:sync_a 密碼A:aaa
主機域B:192.168.1.1 用戶名B:sync_b 密碼B:bbb
至少分配以下權限grant replication slave
3.執行flush privileges
4.停止MySQL
5.配置my.cnf(my.ini)
服務器A 服務器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表示需要同步的數據庫
binlog-ignore-db和replicate-ignore-db表示不需要同步的數據庫
請不要加入以下命令,該命令並不會解決uid跳號的問題,恰恰相反以下兩行命令才是導致uid跳號的罪魁禍首
auto_increment_increment = 2
auto_increment_offset = 1
6.重新啟動MySQL
7.進入MySQL控制台
服務器A:
show master status G
flush tables with read lock;
服務器B:
show master status G
flush tables with read lock;
同時記錄下兩台服務器的File和Position,此處假設:
A: File: mysql-bin.000001
Position: 001
B: File: mysql-bin.000002
Position: 002
服務器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;
服務器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;
此處填寫的數據完全相反
8.執行show processlist G查看是否同步成功
方法二
兩台服務器
192.168.1.1(A)
192.168.1.2(B)
先保證這mysql的版本是一致的,參考http://dev.mysql.com/doc/refman/5.1/zh/replication.html#replication-implementation-details,否則復制中的異常情況很折騰人。
1.在兩台mysql上創建用戶,設置權限
A上添加:
#grant replication slave,replication client,reload,super on *.* to 'sync_user'@'192.168.1.2' identified by '123456' with grant option;//用於B訪問
B上:
#grant replication slave,replication client,reload,super on *.* to 'sync_user'@'192.168.1.1' identified by '123456' with grant option;//用於A訪問
執行 #flush privileges; 更新數據庫使用戶生效。
2.在/etc/my.cnf上進行相關配置
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.如果想要同時同步多個庫,添加多行replicate-do-db,每行指定一個數據庫。不能使用replicate-do-db=db1,db2的形式
3.replicate-ignore-db:指定不進行同步的數據庫。
保存後,重啟mysql
#mysqladmin -u root -p shutdown
#mysqld_safe --user=mysql
3.把兩台服務器上需要同步的數據庫進行拷貝,保證這兩台數據庫初始狀態一致。
4.進行雙向同步
雙向同步就是把單向同步反過來在做一遍,但一定要注意操作的順序,這是成功的關鍵
step1.在A上mysql shell中執行
#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上執行:
#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,執行show slave statusG;如顯示如下內容,表示同步設置成功。
Slave_IO_State: Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
step4:上一步沒有問題。則在B上繼續執行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上執行
#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:執行show slave statusG;如顯示如下內容,表示同步設置成功。
Slave_IO_State: Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如上述沒有啥問題。到此雙向同步設置完成。
方法三
一、准備服務器
由於MySQL不同版本之間的(二進制日志)binlog格式可能會不一樣,因此最好的搭配組合是Master的MySQL版本和Slave的版本相同或者更低,Master的版本肯定不能高於Slave版本。
more.. | less.. | 本文中,我們假設主服務器(以下簡稱Master)和從服務器(以下簡稱Slave)的版本都是5.0.27,操作系統是RedHat Linux 9。
假設同步Master的主機名為:master(IP:192.168.1.123),Slave主機名為:slave(IP:192.168.1.124),2個MySQL的basedir目錄都是/usr/local/mysql,datadir都是: /var/lib/mysql。
二、設置同步服務器
1、設置同步Master
修改 my.cnf 文件,在
# Replication Master Server (default)
# binary logging is required for replication
添加如下內容:
#log-bin=/var/log/mysql/updatelog
server-id = 1
binlog-do-db=discuz
binlog-ignore-db=mysql
重啟MySQL,創建一個MySQL帳號為同步專用
# /usr/local/mysql/bin/mysql -u root -p
mysql> GRANT REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
如果想要在Slave上有權限執行 "LOAD TABLE FROM MASTER" 或 "LOAD DATA FROM MASTER" 語句的話,必須授予全局的 FILE 和 SELECT 權限:
mysql>GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
mysql> FLUSH PRIVILEGES ;
2、設置同步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、啟動同步
在主服務器master MySQL命令符下:
# /usr/local/mysql/bin/mysql -u root -p
mysql> show master status;
顯示(當然這個是我機器的情況,你的不可能跟我一樣哈,只是個例子):
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| mysql-bin.000009 | 98 | discuz | mysql |
+------------------+----------+-------------------+------------------+
在從服務器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;看一下從服務器的同步情況
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果都是yes,那代表已經在同步
往表裡面寫點數據測試一下看是否同步成功,如果不成功,絕對不是你的RP問題,再檢查一下操作步驟!
4、設置雙向同步
修改slave服務器的my.cnf,添加
log-bin=/var/log/mysql/updatelog
binlog-do-db=discuz
binlog-ignore-db=mysql
重啟MySQL,創建一個MySQL帳號為同步專用
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服務器的my.cnf,添加
master-host = www.3ppt.com
master-user = back
master-password = back
master-port = 3306
replicate-ignore-db=mysql
replicate-do-db=discuz
重啟MySQL
在主服務器slave MySQL命令符下:
show master status;
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| mysql-bin.000013 | 98 | discuz | mysql |
+------------------+----------+-------------------+------------------+
在服務器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;
其實也就是A->B單向同步的反向操作!雙向同步,就這麼簡單啦!
提示:如果修改了主服務器的配置,記得刪除從服務器上的master.info文件。否則從服務器使用的還是老配置,可能會導致錯誤。
-----------------------------------------------------------------------------------
注意:關於要復制多個數據庫時,binlog-do-db和replicate-do-db選項的設置,網上很多人說是用半角逗號分隔,經過測試,這樣的說法是錯誤的,MySQL官方文檔也明確指出,如果要備份多個數據庫,只要重復設置相應選項就可以了。
比如:
binlog-do-db=a
binlog-do-db=b
replicate-do-db=a
replicate-do-db=b