mysq的主從複製、備份、還原
基於mysql數據庫文件的主從同步與AB複製及數據備份
搭建實驗環境說明:
在 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>界面
一、
##########################在master mysql操作:##########################
1、授權從服務器的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
2、在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= // 不需要備份的數據庫名,可寫多行
二、
##########################在slave mysql操作:###########################
1、編輯/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 //如果從服務器發現主服務器斷掉,重新連接的時間差(秒)
三、
##########################在master mysql操作:##########################
1、創建測試數據庫
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 |
+----------+------+------------+-----------+
2、在master使用mysqldump命令將數據庫導出:
#mysqldump -uroot mytest > mysql.sql
3、使用scp命令將mysql.sql拷貝到slave
#scp mysq.sql root#slaveIP:/root //將#換成@
四、
##########################在slave mysql操作:###########################
1、創建mytest數據庫,
mysql> create database mytest;
2、將mysql.sql導入其中
#mysql -uroot -p mytest < mysql.sql
3、檢查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 |
+----------+------+------------+-----------+
4、驗證配置的正確性
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的備份與還原基本完成
五、
##########################在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服務
1、master
#rm -rf /var/lib/mysql/*.*
2、slave
#rm -rf /usr/local/mysql/var/*.*
3、service mysqld restart
提供技術支持:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.