详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群

详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群


主要思路:

简单说,实现mysql主备复制-->利用mycat实现负载均衡。

比较了常用的读写分离方式,推荐mycat,社区活跃,性能稳定。

以下基于centos7操作系统进行演示。


架构图

以下是这次搭建的架构图。

详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群

 

主机1与主机2上的keepalived会为该服务器抢占vip,抢占到vip后,对该主机的访问可以通过原来的ip访问,也可以直接通过vip访问,当其中一台宕机时,vip会漂移到另一台机器上,当我们访问VIP时haproxy会根据配置将请求分发到两台Mycat上实现负载均衡,并且haproxy会检测mycat是否存活。

下面先介绍下后端数据库的搭建部分。


一、安装部署mysql

1、安装依赖

yum install tcl gcc perl perl-JSON.noarch perl-Time-HiRes.x86_64 -y
详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群

 

2、创建基础目录并修改文件夹的属组

mkdir -p /data/datafile
mkdir -p /data/log
chown -R mysql:mysql /data

3、安装mysql数据库,采用rpm安装方式

3.1卸载自带包

sudo yum remove -y mariadb*
详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群

 

3.2、安装rpm

rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-embedded-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-embedded-devel-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-test-5.7.26-1.el7.x86_64.rpm
详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群

 

4、mysql数据库配置

4.1、修改数据库my.cnf参数(注意server-id参数)

# vi /etc/my.cnf
[mysqld]
datadir=/data/datafile
socket=/var/lib/mysql/mysql.sock
log-error=/data/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character_set_server=utf8
bind-address = 0.0.0.0
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
symbolic-links=0
max_connections=900
max_allowed_packet=500M
#二进制配置
server-id = 1
log-bin = /data/log/mysql-bin.log
log-bin-index =/data/log/binlog.index
log_bin_trust_function_creators=1
expire_logs_days=7
详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群

 

4.2、重启数据库初始化数据

service mysqld restart

4.3、修改数据库密码

4.3.1、查看数据库默认密码

#cat /data/log/mysqld.log|grep password
详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群

 

4.3.2、更改密码

#mysql -u root -p
set global validate_password_policy=0;
SET PASSWORD = PASSWORD("xxxxx");
详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群

 

4.3.3、创建数据库和用户

create schema tms_prod default character set utf8;
set global validate_password_policy=0;
GRANT ALL PRIVILEGES ON *.* TO tms_prod@"%" identified by "xxx";

4.3.4、重启mysql数据库

service mysqld restart
详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群

 


二、配置mysql主从

原理:

详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群

 

1、主库授权同步帐号

在主服务器上,设置一个从数据库的账户,使用REPLICATION SLAVE赋予权限,如:

set global validate_password_policy=0;
GRANT REPLICATION SLAVE ON *.* TO "repl"@"%" IDENTIFIED BY "fxxx";

2、修改主数据库的配置文件my.cnf

开启BINLOG,并设置server-id的值,修改之后必须重启Mysql服务。

#vi /etc/my.cnf
-----------------------------------------配置文件-----------------------------------------
#server-id表示在整个Mysql集群中保证唯一
[mysqld]
datadir=/data/datafile
socket=/var/lib/mysql/mysql.sock
log-error=/data/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character_set_server=utf8
bind-address = 0.0.0.0
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
symbolic-links=0
max_connections=900
max_allowed_packet=500M
#二进制配置
server-id = 1
log-bin = /data/log/mysql-bin.log
log-bin-index =/data/log/binlog.index
log_bin_trust_function_creators=1
expire_logs_days=7
-----------------------------------------配置文件-----------------------------------------
详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群

 

3、查看主服务器状态

可以查看主服务器当前二进制日志名和偏移量,这个操作的目的是为了在从数据库启动后,从这个点开始进行数据的恢复

mysql> show master status G;
详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群

 

4、从服务器mysql配置

修改配置后需重启数据库服务器

#vi /etc/my.cnf
---------------------------------------配置文件-------------------------------------------
[mysqld]
datadir=/data/datafile
socket=/var/lib/mysql/mysql.sock
log-error=/data/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character_set_server=utf8
bind-address = 0.0.0.0
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
symbolic-links=0
max_connections=900
max_allowed_packet=500M
#从数据库二进制配置
server-id = 2
log-bin = /data/log/mysql-bin.log
log-bin-index =/data/log/binlog.index
log_bin_trust_function_creators=1
expire_logs_days=7
----------------------------------------配置文件------------------------------------------
详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群

 

5、从库开启同步

5.1 从库配置同步

配置mysql主从复制时,在从机上需要进行CHANGE MASTER TO操作,以确定需要同步的主机IP,用户名,密码,binlog文件,binlog位置等信息。

change master to配置和改变slave服务器用于连接master服务器的参数,以便slave服务器读取master服务器的binlog及slave服务器的relay log。同时也更新master info及relay log info信息库。执行该语句前如果从机上slave io及sql线程已经启动,需要先停止(执行stop slave)。

change master to
MASTER_HOST="xx.xx.xx",
MASTER_USER="repl",
MASTER_PASSWORD="xxxx",
MASTER_LOG_FILE="mysql-bin.000003",
MASTER_LOG_POS=437;

5.2、开启主从同步

开启后注意查看从库状态,

当Slave_IO_Running和Slave_SQL_Running都为YES的时候表示主从同步设置成功。

mysql> start slave;
mysql> show slave status G
详解mycat+haproxy+keepalived搭建高可用负载均衡mysql集群