Mysql主從半同步及雙機熱備
Mysql主從同步配置(mysql版本5.5以上、keepalived軟體實作雙機熱備):
基礎:兩台機子 主伺服器:192.168.1.130 從伺服器:192.168.1.131
1、修改mysql配置檔案my.cnf(以下配置項,檔案中有則根據下邊要求修改,沒有則自行添加):
主:
server-id = 1(主資料庫一般都是id為1)
log-bin=mysql-bin (必須的)
binlog_format=mixed (必須的,推薦類型為mixd)
expire_logs_days=5 (為避免日志檔案過大,設定過期時間為5天)
binlog-ignore-db = mysql (忽略同步的資料庫,也不記入二進制日志,可列多行)
replicate-do-db = test (需要同步的資料庫,記入二進制日志,可列多行)
從:
server-id = 2
log-bin=mysql-bin
binlog_format=mixed
expire_logs_days=5
注1:MySQL進行主從複制是通過二進制的日志檔案來進行,是以我們必須開啟MySQL的日志功能,即我們上面的log-bin,同時每一台資料庫伺服器都需要指定一個唯一的server-id,通常主資料庫伺服器我們指定為1。
注2:Mysql5.5版本以上不再支援master的用法,比如5.1中可用的(master-user = repl;master-password = repl;master-port = 3307)這樣的文法已經失效了,如果你使用了,還會報錯。(我在這裡載了跟頭,檢查mysql日志/usr/local/db/mysql/ 127.0.0.1.err——您的路徑也許會稍有不同——才發現錯誤)如果你不小心配置了這些參數,MySQL伺服器将無法正常啟動
錯誤提示為:[ERROR] /usr/local/mysql/bin/mysqld: unknownvariable ‘master-host=192.168.1.130’,正确的辦法如上文所寫。
配置完成之後,将主從兩機的mysql服務重新啟動一下。
2、Mysql主(1.130),執行:GRANT all privileges on *.* to 'test' @'192.168.1.131' IDENTIFIED BY '123456'(授予slave賬号對所有資料庫享有slave權限,密碼為slave777);
3、檢視Mysql主 的狀态:執行:show master status;這時會看到master資料庫所處的位置,記錄下來:
show master status;
| File | Position |
| mysql-bin.000011 | 383 |
4、Mysql從:
執行:slave stop;
執行:change master tomaster_host='192.168.1.130', master_port=3306, master_user=’test’,master_password=’123456’,master_log_file=’mysql-bin.000011′, master_log_pos=383;
執行:slave start;
5、查詢slave的狀态:mysql> show slavestatus \G;看下slave的狀态:
Master_Log_File: mysql-bin.000011 (和主mysql一緻)
Read_Master_Log_Pos: 383 (和主mysql一緻)Slave_IO_Running: Yes(讀寫)
Slave_SQL_Running: Yes (資料庫狀态)
還要注意狀态中是否有error,如果沒有的話,就差不多了。
6、測試資料庫是否能夠同步:略;
7、如果Slave複制失敗,你可以根據錯誤資訊進行修正,然後執行
mysql> slave stop;
mysql> slave start;就可以把原來應該複制過來的資料都複制過來
8、幾個常用的指令:
Slave start; --啟動複制線程
Slave stop; --停止複制線程
Reset slave; --重置複制線程
Show slave status; --顯示複制線程的狀态
Show slave status\G; --顯示複制線程的狀态(分行顯示)
Show master status\G; --顯示主資料庫的狀态(分行顯示)
Show master logs --顯示主資料庫日志,需在主資料庫上運作
Change master to; --動态改變到主資料庫的配置
Show processlist --顯示有哪些線程在運作
由于Mysql的複制都是基于異步進行的,在特殊情況下不能保證資料的成功複制,是以在mysql 5.5之後使用了來自google更新檔,可以将Mysql的複制實作半同步模式。是以需要為主伺服器加載對應的插件。在Mysql的安裝目錄下的lib/plugin/目錄中具有對應的插件semisync_master.so,semisync_slave.so
在Master和Slave的mysql指令行運作如下指令:
Master:
mysql> install pluginrpl_semi_sync_master soname 'semisync_master.so';
mysql> set globalrpl_semi_sync_master_enabled = 1;
mysql> set globalrpl_semi_sync_master_timeout = 1000;
mysql> show variables like '%semi%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 1000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave |ON |
+------------------------------------+-------+
Slave:
mysql> install pluginrpl_semi_sync_slave soname 'semisync_slave.so';
mysql> set globalrpl_semi_sync_slave_enabled = 1;
mysql> stop slave;
mysql> start slave;
mysql> show variables like '%semi%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
檢查半同步是否生效:
Master:
mysql> show global status like'rpl_semi%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
|Rpl_semi_sync_master_net_avg_wait_time | 0 |
| Rpl_semi_sync_master_net_wait_time | 0 |
| Rpl_semi_sync_master_net_waits | 0 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | ON |
|Rpl_semi_sync_master_timefunc_failures | 0 |
|Rpl_semi_sync_master_tx_avg_wait_time | 0 |
| Rpl_semi_sync_master_tx_wait_time | 0 |
| Rpl_semi_sync_master_tx_waits | 0 |
|Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 0 |
+--------------------------------------------+-------+
說明半同步成功。
讓半同步功能在MySQL每次啟動都自動生效,在Master和Slave的my.cnf中編輯:
Master:
[mysqld]
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000 #1秒
Slave:
[mysqld]
rpl_semi_sync_slave_enabled=1
也可通過設定全局變量的方式來設定是否啟動半同步插件:
Master:
mysql> set globalrpl_semi_sync_master_enabled=1
取消加載插件
mysql> uninstall pluginrpl_semi_sync_master;
Slave:
mysql> set globalrpl_semi_sync_slave_enabled = 1;
mysql> uninstall pluginrpl_semi_sync_slave;
安裝keepalived軟體實作雙機熱備
Keepalived是Linux下面實作VRRP備份路由的高可靠性運作件。基于Keepalived設計的服務模式能夠真正做到主伺服器和備份伺服器故障時IP瞬間無縫交接。
主伺服器
#tar zxvf keepalived-1.2.2.tar.gz
#cd keepalived-1.2.2
#mkdir /usr/local/keepalived
#./configure --prefix=/usr/local/keepalived
#make
#make install
#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
修改keepalived配置檔案
vim /usr/local/keepalived/etc/keepalived/keepalived.conf(删除多餘内容,僅留以下部分)
! Configuration File for keepalived
global_defs {
notification_email {
修改/etc/sysconfig/keepalived的内容:
KEEPALIVED_OPTIONS="-D -f/usr/local/keepalived/etc/keepalived/keepalived.conf"
#service keepalived start
從伺服器
#tar zxvf keepalived-1.2.2.tar.gz
#mkdir /usr/local/keepalived
#cd keepalived-1.2.2
#./configure --prefix=/usr/local/keepalived
#make
#make install
#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
#vim /usr/local/keepalived/etc/keepalived/keepalived.conf(删除多餘内容,僅留以下部分)! Configuration File for keepalived
global_defs {
notification_email {
修改/etc/sysconfig/keepalived的内容:
KEEPALIVED_OPTIONS="-D -f/usr/local/keepalived/etc/keepalived/keepalived.conf"
#service keepalived start
設定定時任務腳本,定時檢測mysql狀态:
Vicheck_mysql_helth.sh
#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=127.0.0.1
MYSQL_USER=root
MYSQL_PASSWORD=aibei1010
CHECK_TIME=3
#mysql is working MYSQL_OK is 1 , mysql downMYSQL_OK is 0
MYSQL_OK=1
functioncheck_mysql_helth (){
$MYSQL-u$MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" >/dev/null2>&1
if [$? = 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while[ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [$MYSQL_OK = 1 ] ; then
CHECK_TIME=0
exit 0
fi
if [$MYSQL_OK -eq 0 ] && [$CHECK_TIME -eq 0 ]
then
killall keepalived
exit1
fi
done