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