標籤:mysql 雙向主從 keepalived ha高可用
最近在做MySQL資料庫的雙向主從,瞭解到keepalived能夠自動判斷並切換到可用資料庫,自己試了一下,整理出文檔來。
先聲明一下環境
iptables開啟3306連接埠或者關掉,關閉selinux
MySQL-01:192.168.204.138
MySQL-02:192.168.204.139
VIP:192.168.204.200 #web伺服器串連的ip,自己可以使用工具串連試一下。
MySQL的安裝過程就略過了,根據個人情況自己安裝即可。
1.修改資料庫的設定檔/etc/my.cnf:
1.1修改MySQL-01的資料庫檔案,在[mysql]下添加如下內容
server_id = 1log_bin = mysql-bin
1.2修改MySQL-02的資料庫檔案,在[mysql]下添加如下內容
server_id = 2log_bin = mysql-bin
2.搭建單向主從
2.1在MySQL-01上
2.1.1操作授權
mysql -u root -p #輸入密碼mysql> grant replication slave on *.* to ‘slave‘@‘192.168.204.139‘ identified by ‘123456‘;mysql> flush privileges;
2.1.2資料傳遞給MySQL-02
mysql -u root -p #輸入密碼flush tables with read lock; #鎖表操作mysql> show master status;+------------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000012 | 120 | | | |+------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)mysqldump -u root -p --all-databases > mysqldump.sqlmysql -u root -pmysql> unlock tables;mysql> quit scp myqsldump.sql 192.168.204.139:/root/
2.2在MySQL-02上操作
2.2.1恢複資料庫資料
mysql -u root -p < mysqldump.sql
2.2.2建立主從同步
mysql -u root -pmysql> change master to master_host=‘192.168.204.138‘,master_user=‘slave‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000012‘,master_log_pos=120,master_port=3306;start slave;
檢查是否成功
show slave status\G;
Slave_IO_Running: YesSlave_SQL_Running: Yes
都為Yes表示同步成功。
註:master_port=3306,預設時不需要加,但是修改過連接埠的則需要該選項
3.搭建互為主從
3.1在MySQl-01上操作
3.1.1使用者授權
mysql -u root -p123456mysql> grant replication slave on *.* to ‘slave‘@‘192.168.204.138‘ identified by ‘123456‘; //建立許可權mysql> flush privileges;
註:因為做的是所有庫的主從,所以在MySQL-01上的操作會同步到MySQL-02上
3.2在MySQL-02上的操作
mysql -u root -pmysql> show master status\G;+------------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000009 | 120 | | | |+------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)
3.1.2在MySQL-01上操作授權
mysql> change master to master_host=‘192.168.204.138‘,master_user=‘slave‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000009‘,master_log_pos=120;mysql> start slave;
檢查是否成功
show slave status\G;
Slave_IO_Running: YesSlave_SQL_Running: Yes
表示成功。
此,mysql的雙向主從同步已完成
4.keepalived的搭建
4.1在兩台伺服器依次做以下操作
a.安裝keepalived
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gztar zxvf keepalived-1.2.15.tar.gzcd keepalived-1.2.15./configure --prefix=/usr/local/keepalivedmake && make install
假如編譯過程出現錯誤,請安裝 gcc,openssl,openssl-devel
b.複製相關檔案
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/mkdir /etc/keepalived/cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
4.2修改設定檔
請先將/etc/keepalived/keepalived.conf檔案清空
4.2.1在MySQL-01上操作
將一下內容複寫進去
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MySQL-ha }vrrp_instance VI_1 { state master interface eth0 virtual_router_id 51 priority 100 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.204.222 }}virtual_server 192.168.204.222 3306 { delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.204.138 3306 { weight 3 notify_down /var/lib/mysql/killkeepalived.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } }}
4.2.2在MySQL-02上操作
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MySQL-ha }vrrp_instance VI_1 { state backup interface eth0 virtual_router_id 51 priority 100 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.204.222 }}virtual_server 192.168.204.222 3306 { delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.204.139 3306 { weight 3 notify_down /var/lib/mysql/killkeepalived.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } }}
4.3在兩台伺服器做以下操作
vim /var/lib/mysql/killkeepalived.sh #!/bin/sh pkill keepalived chmod +x /var/lib/mysql/killkeepalived.sh
4.4啟動keepalived服務
service keepalived restart
5.測試是否可用
建立一個新的使用者
mysql> grant all privileges on *.* to ‘test‘@‘%‘ identified by ‘123456‘;mysql> flush privileges;
使用工具測試一下,選擇navacat ,ip處填寫VIP地址,串連可用
至此,配置完成
Mysql之雙向主從加keepalived高可用