目录
架构分析:
第一层:MHA集群
第二层:读写分离
第三层:高可用和负载均衡
MHA的搭建细节:
第一步:准备MHA运行环境
第二步:配置MHA
主数据库的配置【192.168.4.2】
主备数据库的配置【192.168.4.3】
主备数据库的配置【192.168.4.4】
配置从服务器【192.168.4.5】
配置从服务器【192.168.4.6】
2.2 配置管理主机 192.168.4.56
mycat 读写分离的详细配置
keeplived+haproxy服务器的详细配置
配置要求:
在该架构地MHA集群中主要是用一台数据库服务器单独用来管理者,这样可以方便检查集群的状态,为主库配置了两条主备数据库,这样使得主库都数据更加安全,使得这个集群更加的强壮,这样即使两台主数据库损坏,也还可以正常的运行。这是我在这里配置了一个浮动VIP,使得无论主数据库在是哪个IP地址,对客户端来说都是透明的。
集群解析:
MHA集群:6台数据库服务器,
MHA管理者---->192.168.4.1
主数据库mysql1----> 192.168.4.2
主备数据库mysql2----> 192.168.4.3
主备数据库mysql1----> 192.168.4.4
从数据库mysql1----> 192.168.4.5
从数据库mysql1----> 192.168.4.6
使用了中间件mycat,实现读写分离,使得读的业务都分摊给从数据库,写的业务在主数据上。为了防止mycat发生单点故障,所以这里使用里了两个mycat服务器。
读写分离解析:
mycat服务器两台
读写分离mycat1----> 192.168.4.11
读写分离mycat2----> 192.168.4.12
使用了haproxy实现两个mycat的负载均衡,防止haproxy发生单点故障,所以这里使用里了两个haproxy服务器。为了使得两台haproxy可以实现高可用的效果,所以使用了keeplived配置两VIP,使得每台haproxy都可以安装期望分摊业务。
高可用和负载均衡解析
keeplived+haproxy服务器1 -----> 192.168.4.13
keeplived+haproxy服务器4 -----> 192.168.4.14
部署mysql高可用集群(MHA软件+主从同步)
高可用集群 主备模式: 当主角色的主机宕机后,备用主机自动接替主角色的主机提供服务服务给客户端
192.168.4.1主机做管理监控服务 ,使用的vip地址:192.168.4.100/24
一主多从
安装依赖的软件包
ssh root用户无密码登陆
2.1配置数据主机(一主到从 安装依赖的软件包 彼此之间可以ssh root 无密码登陆) 192.168.4.100
2.1.1 一主多从
[root@mysql2 ~]# vim /etc/my.cnf
在[mysqld]下添加
validate_password_policy=0
validate_password_length=6
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1
server_id=2
log-bin=master2
binlog-format="mixed"
[root@mysql2 ~]# systemctl restart mysqld
[root@mysql2 ~]# ls /var/lib/mysql/master2.*
[root@mysql2 ~]# mysql -uroot -p123456
mysql> grant all on *.* to root@"%" identified by "123456";
mysql> grant replication slave on *.* to repluser@"%" identified by "123456"; ----授权给监控用户
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master2.000001 | 441 | | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
授权监控用户
mysql> grant all on *.* to root@"%" identified by "123456";
mysql> grant replication slave on *.* to repluser@"%" identified by "123456";
2.1.6 所有数据库服务器启不删除本机的中继日志文件
mysql> set global relay_log_purge=off;// 不自动删除本机的中继日志文件
[root@mysql2 ~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1
server_id=3
log-bin=master3
binlog-format="mixed"
[root@mysql2 ~]# systemctl restart mysqld
[root@mysql2 ~]# ls /var/lib/mysql/master3.*
/var/lib/mysql/master3.000001 /var/lib/mysql/master3.index
[root@mysql2 ~]# mysql -uroot -p123456
mysql> set global relay_log_purge=off; ------是否自动清空不再需要中继日志时。默认值为1(启用)
Query OK, 0 rows affected (0.13 sec)
mysql> change master to
-> master_host="192.168.4.3",
-> master_user="repluser",
-> master_password="123456",
-> master_log_file="master2.000001",
-> master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
[root@host52 ~]#mysql -uroot -p123456 -e "show slave status\G" | grep -i YES
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql3 ~]# vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1
server_id=4
log-bin=master4
binlog-format="mixed"
[root@mysql3 ~]# systemctl restart mysqld
[root@mysql3 ~]# ls /var/lib/mysql/master4.*
/var/lib/mysql/master4.000001 /var/lib/mysql/master4.index
[root@db108 ~]# mysql -uroot -p123456
mysql> set global relay_log_purge=off;
Query OK, 0 rows affected (0.13 sec)
mysql> change master to
-> master_host="192.168.4.4",
-> master_user="repluser",
-> master_password="123456",
-> master_log_file="master2.000001",
-> master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
[root@host52 ~]#mysql -uroot -p123456 -e "show slave status\G" | grep -i YES
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql4 ~]# vim /etc/my.cnf
[mysqld]
server_id=5
:wq
[root@mysql4 ~]# systemctl restart mysqld
[root@mysql4 ~]# mysql -uroot -p123456
mysql> change master to
-> master_host="192.168.4.5",
-> master_user="repluser",
-> master_password="123456",
-> master_log_file="master2.000001",
-> master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.09 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> quit;
Bye
[root@host54 ~]# mysql -uroot -p123456 -e "show slave status\G" | grep -i yes
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql5 ~]# vim /etc/my.cnf
[mysqld]
server_id=55
:wq
[root@mysql5 ~]# systemctl restart mysqld
[root@mysql5 ~]# mysql -uroot -p123456
mysql> change master to
-> master_host="192.168.4.5",
-> master_user="repluser",
-> master_password="123456",
-> master_log_file="master2.000001",
-> master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.09 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> quit;
Bye
[root@mysql5 ~]# mysql -uroot -p123456 -e "show slave status\G" | grep -i yes
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
2.1.2 安装依赖的perl软件包
2.1.3安装软件mha-node (每个mysql数据库上都需要配置该软件)
[root@mysql1 ~]# cd 数据库软件包/mha-soft-student/
[root@mysql1 ~]# mha-soft-student]# yum -y install perl-DBD-mysq
[root@mysql1 ~]# mha-soft-student]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
2.1.4 配此之间可以ssh root用户无密码登陆
2.2.1 安装依赖的perl软件包
2.2.2 安装软件mha-node
[root@mysqlmanage ~]# cd mha-soft-student/
[root@mysqlmanage mha-soft-student]# ls
master_ip_failover perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm
mha4mysql-manager-0.56 perl-Mail-Sender-0.8.23-1.el7.noarch.rpm
mha4mysql-manager-0.56.tar.gz perl-Mail-Sendmail-0.79-21.el7.art.noarch.rpm
mha4mysql-node-0.56-0.el6.noarch.rpm perl-MIME-Lite-3.030-1.el7.noarch.rpm
perl-Config-Tiny-2.14-7.el7.noarch.rpm perl-MIME-Types-1.38-2.el7.noarch.rpm
perl-Email-Date-Format-1.002-15.el7.noarch.rpm perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm
[root@mysqlmanage mha-soft-student]# yum -y install perl-DBD-mysql
[root@mysqlmanage mha-soft-student]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
源码安装mha4mysql-manager
[root@mysqlmanage mha-soft-student]# yum -y install perl-ExtUtils-* perl-CPAN-*
[root@mysqlmanage mha-soft-student]#tar -zxf mha4mysql-manager-0.56.tar.gz
[root@mysqlmanage mha-soft-student]#cd mha4mysql-manager-0.56
[root@mysqlmanage mha-soft-student]# perl Makefile.pl
[root@mysqlmanage mha-soft-student]# make
[root@mysqlmanage mha-soft-student]# make install
2.2.3cd 修改配置文件
[root@mysqlmanage ~] # mkdir /etc/mha_manager/
[root@mysqlmanage ~]# cd /root/mha-soft-student/mha4mysql-manager-0.56/samples/conf
[root@mysqlmanage mha4mysql-manager-0.56]# cp app1.cnf /etc/mha_manager/app1.cnf
[root@mysqlmanage mha4mysql-manager-0.56]# cd ~
[root@mysqlmanage ~] # vim /etc/mha_mannger/app1.cnf
[server default]
manager_log=/etc/mha_manager/manager.log
manager_workdir=/etc/mha_manager
master_ip_failover_script=/usr/local/bin/master_ip_failover
password=123456
repl_password=123456
repl_user=repluser
ssh_port=22
ssh_user=root
user=root
[server1]
candidate_master=1
hostname=192.168.4.2
port=3306
[server2]
candidate_master=1
hostname=192.168.4.3
port=3306
[server3]
candidate_master=1
hostname=192.168.4.4
port=3306
[server4]
hostname=192.168.4.5
no_master=1
port=3306
[server5]
hostname=192.168.4.6
no_master=1
port=3306
:wq
[root@mysqlmanage ~]# cd /root/mha-soft-student/mha4mysql-manager-0.56/samples/scripts/
[root@mysqlmanage scripts]# ls
master_ip_failover master_ip_online_change power_manager send_report
[root@mysqlmanage ~]#vim /etc/mha_manager/master_ip_failover
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2011 DeNA Co.,Ltd.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by:
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 ## Note: This is a sample script and is not complete. Modify the script based on your environment.
21
22 use strict;
23 use warnings FATAL => 'all';
24
25 use Getopt::Long;
26 use MHA::DBHelper;
27
28 my (
29 $command, $ssh_user, $orig_master_host,
30 $orig_master_ip, $orig_master_port, $new_master_host,
31 $new_master_ip, $new_master_port, $new_master_user,
32 $new_master_password
33 );
34
35 my $vip = '192.168.4.100/24'; # Virtual IP
36 my $key = "1";
37 my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
38 my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
39
40 GetOptions(
41 'command=s' => \$command,
42 'ssh_user=s' => \$ssh_user,
43 'orig_master_host=s' => \$orig_master_host,
44 'orig_master_ip=s' => \$orig_master_ip,
45 'orig_master_port=i' => \$orig_master_port,
46 'new_master_host=s' => \$new_master_host,
47 'new_master_ip=s' => \$new_master_ip,
48 'new_master_port=i' => \$new_master_port,
49 'new_master_user=s' => \$new_master_user,
50 'new_master_password=s' => \$new_master_password,
51 );
52
53 exit &main();
54
55 sub main {
56 if ( $command eq "stop" || $command eq "stopssh" ) {
57
58 # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
59 # If you manage master ip address at global catalog database,
60 # invalidate orig_master_ip here.
61 my $exit_code = 1;
62 eval {
63
64 # updating global catalog, etc
65 &stop_vip();
66 $exit_code = 0;
67 };
68 if ($@) {
69 warn "Got Error: $@\n";
70 exit $exit_code;
71 }
72 exit $exit_code;
73 }
74 elsif ( $command eq "start" ) {
75
76 # all arguments are passed.
77 # If you manage master ip address at global catalog database,
78 # activate new_master_ip here.
79 # You can also grant write access (create user, set read_only=0, etc) here.
80 my $exit_code = 10;
81 eval {
82 my $new_master_handler = new MHA::DBHelper();
83
84 # args: hostname, port, user, password, raise_error_or_not
85 $new_master_handler->connect( $new_master_ip, $new_master_port,
86 $new_master_user, $new_master_password, 1 );
87
88 ## Set read_only=0 on the new master
89 $new_master_handler->disable_log_bin_local();
90 print "Set read_only=0 on the new master.\n";
91 $new_master_handler->disable_read_only();
92
93 ## Creating an app user on the new master
94 print "Creating app user on the new master..\n";
95 $new_master_handler->enable_log_bin_local();
96 $new_master_handler->disconnect();
97
98 ## Update master ip on the catalog database, etc
99 &start_vip();
100 $exit_code = 0;
101 };
102 if ($@) {
103 warn $@;
104
105 # If you want to continue failover, exit 10.
106 exit $exit_code;
107 }
108 exit $exit_code;
109 }
110 elsif ( $command eq "status" ) {
111
112 # do nothing
113 exit 0;
114 }
115 else {
116 &usage();
117 exit 1;
118 }
119 }
120 sub start_vip() {
121 `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
122 }
123 sub stop_vip() {
124 return 0 unless ($ssh_user);
125 `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
126 }
127
128 sub usage {
129 print
130 "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
131 }
[root@mysqlmanage ~]# chmod +x /etc/mha_manager/master_ip_failover
[root@mysqlmanage ~]# vim /etc/mha_manager/master_ip_failover
my $vip = '192.168.4.100/24'; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
测试配置文件
[root@mysqlmanage ~]# vim /etc/mha_manager/app1.cnf
[server default]
#master_ip_failover_script=/etc/mha_manager/master_ip_failover
[root@mysqlmanage ~]# masterha_check_ssh --conf=/etc/mha_manager/app1.cnf #//测试ssh root用户22号是否可通过
[root@mysqlmanage ~]# masterha_check_repl --conf=/etc/mha_manager/app1.cnf #//测试 数据库sql repl用户是否可以连接
MySQL Replication Health is OK.
3、启动服务:
3.1 把vip 地址手动绑定在主库上
[root@mysql2 ~]# ifconfig eth0:1 192.168.4.100/24
[root@mysql2 ~]# ifconfig
3.2 启动服务
[root@mysql2 ~]# vim /etc/mha_manager/app1.cnf
[server default]
master_ip_failover_script=/etc/mha_manager/master_ip_failover
[root@mysql2 ~]# masterha_manager --conf=/etc/mha_manager/app1.cnf --remove_dead_master_conf --ignore_last_failover #//说明remove_dead_master_conf是说主库down后删除app1.cnf中的配置文件对应的信息,ignore_last_failover是指在8个小时内不能down多次
[root@mysql2 ~]# masterha_check_status --conf=/etc/mha_manager/app1.cnf #//查看mha运行状态
4、测试高可用集群配置
在数据库服务上添加访问数据连接用户 webuser 123456
[root@mysql2 ~]# mysql -h192.68.4.51 -uroot -p123456
MySQL > create database db13;
mysql> grant all on db13.* to webuser@"%" identified by "123456";
4.1 客户端连接VIP地址访问数据库
[root@mysql2 ~]# mysql -h192.168.4.100 -uwebuser -p123456 ----任意一台可以访问网段的并且有mysql命令的
4.2 测试高用集群
1.把主机192.168.4.2上的数据库服务停止
[root@mysql2 ~]# systemctl stop mysqld
2.在任意一台主数据库上查看现在的主数据库
[root@mysql5 ~]# mysql -uroot -p123456
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.4
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master4.000016
Read_Master_Log_Pos: 154
Relay_Log_File: mysql5-relay-bin.000002
Relay_Log_Pos: 318
Relay_Master_Log_File: master4.000016
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3.把宕机的数据库服务器192.168.4.2 在添加到当前集群里
如果主机宕机之后,mha对所有的机器上进行检查,当发现主数据库宕机后,30秒内进行相应的切换,在原来的主数据库修复之后添加到集群,需要两个条件,第一在修复好的数据库手动指定相应的现在在集群的主数据库,第二在相应的MHA管理器上的配置文件重新添加该主机,因为在发现原来的主机宕机后,原来的主机的相应的配置文件会被自动删除掉。
[root@mysql2 ~]#mysql -h192.68.4.51 -uroot -p123456
mysql> change master to
-> master_host="192.168.4.4", ----
-> master_user="repluser",
-> master_password="123456",
-> master_log_file="master4.000016",
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.09 sec)
mysql> start slave;
mha管理机192.168.4.1
[root@mysqlmanage ~]# vim /etc/mha_manager/app1.cnf
[server1]
candidate_master=1
hostname=192.168.4.2
:wq
[root@mysqlmanage ~]# masterha_check_repl --conf=/etc/mha_manager/app1.cnf
MySQL Replication Health is OK.
[root@mysqlmanage ~]#masterha_manager --conf=/etc/mha_manager/app1.cnf --remove_dead_master_conf --ignore_last_failover
在192.168.4.11和192.168.4.12两台主机上配置mycat。下面只是举例一台的配置。
1.在已经搭建好的MHA集群中的主数据库创建一个用于查询的用户
create user 'read'@'%' IDENTIFIED BY 'daer';
grant select on *.* to 'read'@'%';
2.安装软件
在机器上安装 java-1.8.0-openjdk-devel
拷贝 mycat 到 /usr/local/
[root@mycat2 ~]# rpm -qa | grep jdk
java-1.8.0-openjdk-devel-1.8.0.131-11.b12.el7.x86_64
java-1.8.0-openjdk-1.8.0.131-11.b12.el7.x86_64
java-1.8.0-openjdk-headless-1.8.0.131-11.b12.el7.x86_64
copy-jdk-configs-2.2-3.el7.noarch
[root@mycat2 ~]# tar -xaf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
[root@mycat2 ~]# mv mycat/ /usr/local/
3.修改配置文件server.xml和schema.xml
[root@mycat2 ~]# vim /usr/local/mycat/conf/server.xml
80:
97:
[root@mycat2 ~]# vim /usr/local/mycat/conf/schema.xml
4.启动 mycat ,验证测试
[root@mycat2~]# /usr/local/mycat/bin/mycat start
[root@mycat2 ~]# ss -untlp | grep 8066
tcp LISTEN 0 100 :::8066 :::* users:(("java",pid=2368,fd=78))
配置完成以后连接 mycat 查询 保证两台mycat都可以实现如下的效果,这里只展示一台 [root@mycat2 ~]# mysql -uroot -p123456 -h192.168.4.12 -P 8066 -e 'select @@hostname;' mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | mysql6 | +------------+ [root@mycat2 ~]# mysql -uroot -p123456 -h192.168.4.12 -P 8066 -e 'select @@hostname;' mysql: [Warning] Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | mysql5 | +------------+ 多查询几次,可以看到轮询效果
配置文件注意事项:
conf/server.xml 可以不修改,但要注意
虚拟库名称,要和后面对应
schemas是这个用户下的逻辑数据库可以有多个逻辑数据库可以用“,”逗号隔开 用户名和密码是连接 mycat 的用户名和密码,与 mysql 实例的用户名密码无关 mycat默认的普通连接端口是8066,管理连接端口是9066 schema:逻辑数据库 dataNode:节点
dataHost:节点对应的读库写库的地址和连接
balance指的负载均衡类型,目前的取值有4种:
balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。
balance="1",全部的readHost与stand by writeHost参与select语句的负载均衡
balance="2",所有读操作都随机的在writeHost、readhost上分发。
balance="3",所有读请求随机的分发到wiriterHost对应的readhost执行,writerHost不负担读压力
switchType指的是切换的模式,目前的取值也有4种:
switchType='-1' 表示不自动切换
switchType='1' 默认值,表示自动切换
switchType='2' 基于MySQL主从同步的状态决定是否切换,心跳语句为 show slavestatus
switchType='3' 基于MySQL galary cluster的切换机制(适合集群)(1.4.1),心跳语句为 show status like 'wsrep%'
WriteType参数设置:
writeType=“0”, 所有写操作都发送到可用的writeHost上。
writeType=“1”,所有写操作都随机的发送到readHost。
writeType=“2”,所有写操作都随机的在writeHost、readhost分上发。
配置完成以后连接 mycat 查询
mysql -uroot -p123456 -h192.168.4.20 -P 8066 -e 'select @@hostname;'
多查询几次,可以看到轮询效果
为防止 haproxy 单点故障,配置两台 haproxy 使用 keepalived 实现高可用.在192.168.4.13和192.168.4.14两台服务器上都要装keeplived+haproxy,keeplived的配置需要配置双VIP,具体细节如下:
1.yum 安装 haproxy
[root@pk1 ~]# yum -y install haproxy
2.修改 /etc/haproxy/haproxy.cfg
[root@pk1 ~]# vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#--------------------------------------------------------------------
listen mycat_3306 *:3306
mode tcp # mysql 得使用 tcp 协议
option tcpka # 使用长连接
balance leastconn # 最小连接调度算法
server mycat_01 192.168.4.11:8066 check inter 3000 rise 1 maxconn 1000 fall 3
server mycat_02 192.168.4.12:8066 check inter 3000 rise 1 maxconn 1000 fall 3
启动服务
[root@pk1 ~]# systemctl restart haproxy.service
[root@pk1 ~]# ss -untlp | grep haproxy
udp UNCONN 0 0 *:47724 *:* users:(("haproxy",pid=1637,fd=6),("haproxy",pid=1636,fd=6))
tcp LISTEN 0 128 *:3306 *:* users:(("haproxy",pid=1637,fd=5))
keepalived 配置
192.168.4.13上的keepalived 配置
1.yum 安装 keepalived
[root@pk1 ~]# yum -y install keepalived
2.修改配置文件 keepalived.conf
[root@pk1 ~]# vim /etc/keepalived/keepalived.conf
vrrp_script chk_haproxy {
script "killall -0 haproxy" # cheaper than pidof
interval 2 # check every 2 seconds
}
vrrp_instance Mycat {
state BACKUP
interface eth0
track_interface {
eth0
}
virtual_router_id 150
priority 150
! nopreempt
advert_int 2
authentication {
auth_type PASS
auth_pass test_mycat
}
virtual_ipaddress {
192.168.4.200/24 brd 192.168.4.255 dev eth0 label eth0:2
}
track_script {
chk_haproxy weight=0 # +2 if process is present
}
}
vrrp_instance Mycat1 {
state MASTER
interface eth0
track_interface {
eth0
}
virtual_router_id 200
priority 150
! nopreempt
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.150/24 brd 192.168.4.255 dev eth0 label eth0:1
}
track_script {
chk_haproxy weight=0 # +2 if process is present
}
}
192.168.4.14上的keepalived 配置
1.yum 安装 keepalived
[root@pk1 ~]# yum -y install keepalived
2.修改配置文件 keepalived.conf
[root@pk2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id mycat
}
vrrp_script chk_haproxy {
script "killall -0 haproxy" # cheaper than pidof
interval 2 # check every 2 seconds
}
vrrp_instance Mycat {
state MASTER
interface eth0
track_interface {
eth0
}
virtual_router_id 150
priority 200
! nopreempt
advert_int 2
authentication {
auth_type PASS
auth_pass test_mycat
}
virtual_ipaddress {
192.168.4.200/24 brd 192.168.4.255 dev eth0 label eth0:2
}
track_script {
chk_haproxy weight=0 # +2 if process is present
}
}
vrrp_instance Mycat1 {
state BACKUP
interface eth0
track_interface {
eth0
}
virtual_router_id 200
priority 100
! nopreempt
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.150/24 brd 192.168.4.255 dev eth0 label eth0:1
}
track_script {
chk_haproxy weight=0 # +2 if process is present
}
}
验证效果:
[192.168.4.13]
[root@pk1 ~]# ifconfig
eth0:1: flags=4163
inet 192.168.4.150 netmask 255.255.255.0 broadcast 192.168.4.255
ether 52:54:00:5e:90:67 txqueuelen 1000 (Ethernet)
[192.168.4.14]
[root@pk2 ~]# ifconfig
eth0:2: flags=4163
inet 192.168.4.150 netmask 255.255.255.0 broadcast 192.168.4.255
ether 52:54:00:25:67:10 txqueuelen 1000 (Ethernet)
[任意一台192.168.4.0/24]网段的主机做客户段访问
[root@pc01 ~]# mysql -uroot -p123456 -h192.168.4.150
MySQL [(none)]> select @@hostname
-> ;
+------------+
| @@hostname |
+------------+
| mysql6 |
+------------+
1 row in set (0.01 sec)
MySQL [(none)]> select @@hostname;
+------------+
| @@hostname |
+------------+
| mysql5 |
+------------+
[root@pc01 ~]# mysql -uroot -p123456 -h192.168.4.200
MySQL [(none)]> select @@hostname
-> ;
+------------+
| @@hostname |
+------------+
| mysql6 |
+------------+
1 row in set (0.01 sec)
MySQL [(none)]> select @@hostname;
+------------+
| @@hostname |
+------------+
| mysql5 |
+------------+