mysql5.7+keepalived实现主主高可用、配置半同步
mysql5.7安装流程:https://www.jianshu.com/p/bf1717d9fc93
我们使用中小企业最常用也是较简单的方案,用keepalived提供一个vip(虚拟IP)来实现切换和负载均衡,在这个ip下跟随2个mysql实例,通过keepalived自带的负载均衡算法和模式来实现负载均衡,通过杀死keepalived进程来实现自动切换。数据库之间的数据同步,依赖于mysql自身的半同步复制。
方案:主主+半同步复制+keepalived
环境:10.0.0.9 mysql+keepalived
10.0.0.10 mysql+keepalived
10.0.0.111 VIP(虚拟ip,这个是通过keepalived来提供的,不需要提前准备)
实现方法
一、配置mysql的主主半同步复制
1、修改mysql配置文件,两台都需要设置
[root@ecs-e50ota-0008 ~]# vim /etc/my.cnf
server_id=1
## server_id=2 (第二台mysql,设为2)
log_bin = /usr/local/mysql/data/mysql-bin
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1
binlog_format=row
master_info_repository = "TABLE"
relay_log_info_repository = "TABLE"
relay_log_recovery = 1
log_slave_updates=1
auto-increment-increment=2
auto-increment-offset=1
## auto-increment-offset=2 (第二台mysql设为2)
2、两台都重启mysql服务
systemctl restart mysqld
3、两台都用root账号登陆mysql,创建用于同步的用户,用这个用户登陆master机进行同步的。
[root@ecs-e50ota-0008 ~]# mysql -uroot -p
mysql>grant replication slave on . to 'slave01'@'%' identified by 'slave01';
(用户名为 slave01,密码也为salve01)
mysql> flush privileges; (刷新权限表)
4、 两台都查看主机状态,记录下 File 和Position,如下面的:mysql-bin.000001 , 970
mysql>show master status;
----------------------------------------------------------------------------------------------------------
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 970 | | | |
+------------------+----------+--------------+------------------+---------------
5、分别在两台mysql上操作,设定主库的信息,第一台的主库就设为第二台,第二台的主库就设为第一台,两台mysql,互为主备。
mysql>change master to master_host='10.0.0.9(或者10.0.0.10)',master_port=3306,
master_user='slave01',master_password='slave01',master_log_file='mysql-bin.000001'
master_log_pos=970;
## 两台mysql分别启动从库同步
mysql> start slave;
将log_file和log_Pos设为另一台机的master status参数。用于去另一台机同步时查找bin_log日志文件和状态,两台分别操作时要分别改为另一台mysql的参数。
6、 两台mysql分别查看是否正常启动,都出现如下信息为成功启动主主同步
mysql> show slave status \G;
----------------------------------------------------------------------------------------------------
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
----------------------------------------------------------------------------------------------------
7、在主主同步的基础上开启半同步
## 分别在两台mysql 上都进行如下操作。安装半同步的master插件和slave插件,并开启插件。
$ mysql -u root -p
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
mysql> set global rpl_semi_sync_master_enabled = 1;
mysql> set global rpl_semi_sync_slave_enabled = 1;
## 重启slave_io线程
mysql> stop slave io_thread;
mysql> start slave io_thread;
## 查看插件是否安装成功,是否启动成功
mysql> show plugins; (出现下面的信息则为安装成功)
----------------------------------------------------------------------------------------------------
rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL |
| rpl_semi_sync_slave | ACTIVE | REPLICATION | semisync_slave.so | GPL |
----------------------------------------------------------------------------------------------------
mysql> show status like 'Rpl%'; (出现下面的信息则为开启半同步成功)
----------------------------------------------------------------------------------------------------
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_slave_status | ON |
----------------------------------------------------------------------------------------------------
8、## 最后,将半同步的启动写入配置文件,每次启动mysql则会启动半同步的master和slave插件。
$ vim /etc/my.cnf
----------------------------------------------------------------------------------------------------
plugin-load=rpl_semi_sync_master=semisync_master.so
rpl_semi_sync_master_enabled=1
plugin-load=rpl_semi_sync_slave=semisync_slave.so
rpl_semi_sync_slave_enabled=1
----------------------------------------------------------------------------------------------------
OK,到此,数据库的主主半同步则安装成功,接下来就是安装在每台mysql服务器上安装keepalived并配置。
二、安装配置keepalived
1、 使用yum安装keepalived
$ yum install keepalived
2、修改配置文件
$ vim /etc/keepalived/keepalived.conf
----------------------------------------------------------------------------------------------------
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
script_user root
enable_script_security
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_mysql_port {
script "/usr/local/keepalived/mysql.sh" #配置脚本位置
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP #两台都设置为BACKUP
interface eth0 #设置你主机的网卡名
virtual_router_id 51
priority 100 #优先级,两台不要设为相同,设为比100小的即可
advert_int 1
nopreempt #非抢占模式,第二台不用设置
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_mysql_port
}
virtual_ipaddress {
10.0.0.111 #虚拟IP
}
}
编写宕机时执行的脚本,可在改脚本中写入所要做的任意事情,比如发邮件给管理员,告诉他时哪台机宕机
$ vim /usr/local/keepalived/mysql.sh
----------------------------------------------------------------------------------------------------
#!/bin/bash
A=`netstat -lnt|grep 3306|wc -l` #检测3306端口是否存在
if [ $A -eq 0 ];then
systemctl start mysqld #3306如果没有存在就重启mysql
sleep 2
if [ `netstat -lnt|grep 3306|wc -l` -eq 0 ];then
systemctl stop keepalived #如果3306不存在了,就结束掉keepalived进程
fi
fi
~
3、分别启动两台mysql服务器上的keepalived,并设为开机启动
$ systemctl enable keepalived
$ systemctl start keepalived
4、启动不报错则OK,到此keepalived,安装配置完成
用ip addr 可以看到自己的网卡下面多出了一个虚拟IP,那个IP则为连接mysql的对外连接ip,端口为3306.外来访问直接访问这个ip,则会被keepalived通过负载均衡的算法,分配到不同的mysql服务器上。有任一mysql服务器宕机后则会通过执行脚本,杀死该机的keepalived进程,使得访问分配到别的服务器上,实现自动切换。
[root@ecs-e50ota-0008 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether fa:16:3e:64:3c:6e brd ff:ff:ff:ff:ff:ff
inet 10.0.0.9/24 brd 10.0.0.255 scope global noprefixroute dynamic eth0
valid_lft 74070sec preferred_lft 74070sec
inet 10.0.0.111/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:fe64:3c6e/64 scope link noprefixroute
valid_lft forever preferred_lft forever
OK,到此,整个方案的搭建完成,实现高可用的负载均衡。
mysql+主主复制+半同步+keepalived