mysql的主從備份和雙機互備實現
雙機互備和熱備的區別:
互備所指的是backup,保證資料的完整性而做的備份。主要目的是防止資料丟失。
熱備是為了保證應用的連續性。利用故障轉移的方法實現。
Linux下實現的雙機互備:
主從備份:
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命令符下
view plaincopy to clipboardprint?# /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命令符下
view plaincopy to clipboardprint?# /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 status/G;看一下從伺服器的同步情況
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帳號為同步專用
view plaincopy to clipboardprint?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 = 192.168.1.124
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命令符下:
view plaincopy to clipboardprint?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;
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;
mysql做互備時要注意mysql 5.5以上版本不能把
master-host = 192.168.1.124
master-user = back
master-password = back
master-port = 3306
寫道my.ini檔案中。要在執行完 slave stop;後 用 change master to master_host='主的ip',master_user='使用者名稱',mater_password='密碼',master_log_file='日誌',master_log_pos='同步點';
然後slave status;