基於mysql數據庫文件的主從同步與AB複製及數據備份
2011-09-13更新
搭建實驗環境說明:
在 RH5.4 yum安裝 mysql部署master 數據庫A,Centos 5.5 編譯安裝mysql 部署slave數據庫B;
mysql 版本信息:
master mysql-5.0.77 slave :mysql-5.0.87 //測試環境並無考慮版本問題
提示:(關閉selinux)
---------------------------------------------------------------------------------------------
通常狀況:mysqladmin -uroot password '123456'設置root用戶訪問mysql的密碼;
mysql -uroot -p 鍵如密碼便可登陸mysql>界面
1、
##########################在master mysql操做:##########################
一、受權從服務器的root用戶來主服務器同步資源的權限
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT,RELOAD,SUPER ON *.* TO root#slave IP IDENTIFIED BY '123456'; //將#換成@
#若是須要的話添加管理用戶,經過mysql的客戶端來測試同步的狀況
mysql>flush privileges; // 刷新權限,使設置生效
#在node2上使用一下命令測試受權是否成功
#mysql -h 192.168.0.42 -uroot -p
二、在master mysql 配置
#vim /etc/my.cnf
# 確保有以下行
server-id = 1
log-bin=mysql-bin
binlog-do-db=mytest
binlog-ignore-db=mysql
#binlog-do-db= // 須要備份的數據庫名,可寫多行
#binlog-ignore-db= // 不須要備份的數據庫名,可寫多行
2、
##########################在slave mysql操做:###########################
一、編輯/etc/my.cnf
server-id=2
log-bin=mysql-bin
master-host=192.168.0.123
master-user=root
master-password=123456
master-port=3306
replicate-do-db=mytest
可選項以下:
# replicate-do-db=test // 須要備份的數據庫名
# replicate-ignore-db=mysql // 忽略的數據庫
# master-connect-retry=60 //若是從服務器發現主服務器斷掉,從新鏈接的時間差(秒)
3、
##########################在master mysql操做:##########################
一、建立測試數據庫
mysql> create database mytest; //建立mytest庫
mysql>show databases;
mysql> use mytest;
mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| mytable |
+------------------+
1 row in set (0.01 sec)
mysql>ceate table mytable (name VARCHAR(20), sex CHAR(1), birth DATE, birthaddr VARCHAR(20));
mysql>describe mytable;
mysql>insert into mytable values ('hanfeng','b','1987-08-08','china');
mysql>insert into mytable values ('feiniao','g','1988-08-08','china');
mysql>select * from mytable;
+----------+------+------------+-----------+
| name | sex | birth | birthaddr |
+----------+------+------------+-----------+
| hanfeng | s | 1985-08-08 | china |
| feiniao | g | 1988-08-08 | china |
+----------+------+------------+-----------+
二、在master使用mysqldump命令將數據庫導出:
#mysqldump -uroot mytest > mysql.sql
三、使用scp命令將mysql.sql拷貝到slave
#scp mysq.sql root#slaveIP:/root //將#換成@
4、
##########################在slave mysql操做:###########################
一、建立mytest數據庫,
mysql> create database mytest;
二、將mysql.sql導入其中
#mysql -uroot -p mytest < mysql.sql
三、檢查mysql.sql導入是否成功
mysql> create database mytest;
mysql>show databases;
mysql> use mytest;
mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| mytable |
+------------------+
mysql>select * from mytable;
+----------+------+------------+-----------+
| name | sex | birth | birthaddr |
+----------+------+------------+-----------+
| hanfeng | s | 1985-08-08 | china |
| feiniao | g | 1988-08-08 | china |
+----------+------+------------+-----------+
四、驗證配置的正確性
mysql>start slave; // 若是啓動失敗,參看如下錯誤處理②
mysql> show slave status\G;
*************************** 1. row *******************
Slave_IO_State: Waiting for master to send event 必須的
Master_Host: 192.168.0.123
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 287
Relay_Log_File: mysqld-relay-bin.000009
Relay_Log_Pos: 424
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes # 爲YES狀態 (說明:若是出現NO請參看下面錯誤解答!)
Slave_SQL_Running: Yes # 爲YES狀態
Replicate_Do_DB: mytest
Replicate_Ignore_DB:
#####################################################################
此時使用mysqldump的備份與還原基本完成
5、
##########################在master mysql操做:##########################
mysql> use mytest;
mysql>insert into mytable values ('shanghai','s','2011-09-13','china');
mysql>select * from mytable;
向mytest數據庫的mytable表中插入一新記錄;
在slave服務器上
mysql> use mytest;
mysql>select * from mytable;
獲得:
mysql> select * from mytable;
+-----------+------+------------+---------------+
| name | sex | birth | birthaddr |
+-----------+------+------------+----------------+
| hanfeng | b | 1987-08-08 | china |
| feiniao | g | 1988-08-08 | china |
| shanghai | s | 2011-09-13 | china |
+-----------+------+------------+---------------+
3 rows in set (0.01 sec)
基於mysql數據庫文件的主從同步與AB複製及數據備份到此完成!
總結:
############################## 錯誤處理 ① ##########################
對於 Slave_IO_Running: No
Slave_SQL_Running: Yes
錯誤解答
Master slave 複製錯誤
Description:
Slave_IO_Running:NO
Slave_SQL_Running:Yes
Seconds_Behind_Master: NULL
本人遇到的Slave_IO_Running:NO的狀況有下面兩種:
1. 在配置slave同步時由於slave訪問master沒有權限致使;
2. master上的mysql-bin.xxxxxx文件全被我誤刪除了;
對於第一種狀況,仔細檢查數據庫訪問權限便可解決;
對於第二種狀況:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.0.123
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000016
Read_Master_Log_Pos: 173
Relay_Log_File: mysqld-relay-bin.000008
Relay_Log_Pos: 98
Relay_Master_Log_File: mysql-bin.000016
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table: br>
Last_Error: 0
Skip_Counter: 0
Exec_Master_Log_Pos: 173
Relay_Log_Space: 98
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: NULL
1 row in set (0.00 sec)
解決步驟:
重啓master庫:service mysqld restart
mysql> show master status;
+------------------+--------------------+----------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-------------------+-----------------+------------------+
| mysql-bin.000001 | 98 | | |
+------------------+--------------------+-----------------+------------------+
mysql> slave stop;
mysql> change master to Master_Log_File='mysql-bin.000001',Master_Log_Pos=98;
mysql> slave start;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.123
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 98
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
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: 235
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
1 row in set (0.00 sec)
############################## 錯誤處理 ② ##########################
問題:若是你的mysql數據庫已經有數據存儲使用,將致使master與slave日誌錯誤!
mysql> start slave;
ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log
mysql> show slave status;
Empty set (0.00 sec)
處理:刪除mysql-bin.000001 日誌文件,並重啓mysql服務
一、master
#rm -rf /var/lib/mysql/*.*
二、slave
#rm -rf /usr/local/mysql/var/*.*
三、service mysqld restart
提供技術支持:
對於本文章, 若有好的建議或問題可:Send Email to linuxhzg#qq.com //將#換成@