1. 程式人生 > 資料庫 >mysql主主+keepalived高可用

mysql主主+keepalived高可用

192.168.81.109 vip
192.168.81.100 centos7 客戶端
192.168.81.101 centos7 mysql主+keepalived主
192.168.81.102 centos7 mysql主+keepalived備

192.168.81.100
部署資料庫
yum -y install mariadb mariadb-server
修改配置檔案
vim /etc/my.cnf
server-id=1
log-bin=mysqlbin
relay-log=relays-log
啟動mariadb
systemctl start mariadb
部署主伺服器
mysql -e "grant all on *.* to mysql@'%' identified by 'mysql';"
mysql -e "show master status;"
mysql -e "change master to master_host='192.168.81.102',master_user='mysql',master_passowrd='mysql',master_log_file='mysqlbin.000001',master_log_pos=123456"
mysql -e "slave start;"
mysql -e "show slave status \G;"
部署keepalived
yum -y install keepalived ipvsadm
修改keepalived配置檔案
vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.81.109
    }
}
virtual_server 192.168.81.109 3306 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.81.100 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}
啟動keepalived
systemctl start keepalived
開機啟動
chkconfig keepalived on
檢視ipvsadm
ipvsadm
192.168.81.100
部署資料庫
yum -y install mariadb mariadb-server
修改配置檔案
vim /etc/my.cnf
server-id=1
log-bin=mysqlbin
relay-log=relays-log
啟動mariadb
systemctl start mariadb
部署主伺服器
mysql -e "grant all on *.* to mysql@'%' identified by 'mysql';"
mysql -e "show master status;"
mysql -e "change master to master_host='192.168.81.101',master_user='mysql',master_passowrd='mysql',master_log_file='mysqlbin.000001',master_log_pos=123456"
mysql -e "slave start;"
mysql -e "show slave status \G;"
部署keepalived
yum -y install keepalived ipvsadm
修改keepalived配置檔案
vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state SLAVE
    interface eth0
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.81.109
    }
}
virtual_server 192.168.81.109 3306 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.81.101 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}
啟動keepalived
systemctl start keepalived
開機啟動
chkconfig keepalived on
檢視ipvsadm
ipvsadm
192.168.81.100
部署mariadb
yum -y install mariadb mariadb-server
systemctl start mariadb
遠端登陸mysql主主架構
mysql -umysql -pmysql -h192.168.81.109 -P3306
mysql -e "show variables like 'server_id';"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |
+---------------+-------+
1 row in set (0.00 sec)
關閉keepalived主的keepalived和mysql
systemctl stop keepalived
systemctl stop mariadb
再遠端登陸mysql
mysql -umysql -pmysql -h192.168.81.109 -P3306
mysql -e "show variables like 'server_id';"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 2     |
+---------------+-------+
1 row in set (0.00 sec)
自動切換為keepalived備用mysql資料庫