1. 配置MySQL雙主模式
1.修改my.cnf配置檔案
預設情況下,MySQL的配置檔案是/etc/my.cnf,在配置檔案的[mysqld]段新增如下內容:
server-id=1
log-bin=mysql-bin
relay-log=mysql-relay-bin
- server-id:是節點標識,主從節點不能相同必須全域性唯一
- log-bin:表示開啟MySQL日誌功能,"mysql-bin"表示日誌檔案的命名格式
- relay-log:用來定義relay-log日誌檔案的命名格式
2.登入MySQL,授權伺服器replication許可權及配置從伺服器,注意因為是雙主模式所以所有操作都為雙向操作。
MySQL master授權replication
grant replication slave on *.* to 'Federico'@'192.168.1.2' identified by '123456';
show master status;
MySQL slave指定master伺服器,並檢視當前服務是否主從複製成功
stop slave;
change master to master_host='192.168.1.1',master_user='Federico',master_password='123456',master_port=3306,master_log_file='mysql-bin.000001',master_log_pos=336;
start slave;
show slave status\G
檢視以下兩項引數為yes,證明主從複製設定成功。
Slave_IO_Running: Yes Slave_SQL_Running: Yes
注意:以上操作為雙向操作,及互為主從,達到雙主的效果。
3. 配置MySQL資料庫可以遠端登入,即我們的其他服務可以通過vip進行登入
grant all on *.* to'Federico'@'%' identified by '123456';
2.配置keepalived實現MySQL雙主高可用
修改keepalived配置檔案
#keepalived master配置檔案
! Configuration File for keepalived
global_defs {
router_id HA_MySQL
}
vrrp_script check_mysql {
script "pkill -0 -x mysqld"
interval 2
fall 2
raise 2
}
vrrp_instance VI_1 {
state MASTER
nopreempt
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
track_script {
check_mysql
}
}
#keepalived backup配置檔案
! Configuration File for keepalived
global_defs {
router_id HA_MySQL
}
vrrp_script check_mysql {
script "pkill -0 -x mysqld"
interval 2
fall 2
raise 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
track_script {
check_mysql
}
}
3.在MySQL客戶端通過vip登入,檢視是否登入成功
mysql -u Federico -h 192.168.1.100 -p