Consul, ProxySQL and MySQL MHA(mysql 读写分离,高可用)

背景:
mysql 官方目前没有提供比较成熟,稳定的高可用的方案,使得Mysql的高可用方案相对较少,一般都只有简单的主从,今天和大家分享一个Consul, ProxySQL and MySQL MHA架构的高可用方案:

架构特性:
1,高可用
2,读写分离,查询路由
3,sql过滤等功能

Installation of Consul:
Firstly, we’ll need to install the required packages, download the Consul archive and perform the initial configuration. We’ll need to perform the same installation on each of the nodes (i.e., appserver, mysql1 and mysql2).
Install pre-requisite packages:
sudo yum -y install wget unzip bind-utils dnsmasq
Install Consul:
sudo useradd consul
sudo mkdir -p /opt/consul /etc/consul.d
sudo touch /var/log/consul.log /etc/consul.d/proxysql.json
cd /opt/consul
sudo wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip
sudo unzip consul_0.6.4_linux_amd64.zip
sudo ln -s /opt/consul/consul /usr/bin/consul
sudo chown consul:consul -R /etc/consul /opt/consul /var/log/consul.log

Configuration of Consul on Application Server (used as ‘bootstrap’ node):
Now, that we’re done with the installation on each of the hosts, let’s continue with the configuration. In this example we’ll bootstrap the Consul cluster using “appserver”:

Edit configuration files
$ sudo vi /etc/consul.conf
{
"datacenter": "dc1",
"data_dir": "/opt/consul/",
"log_level": "INFO",
"node_name": "agent1",
"server": true,
"ui": true,
"bootstrap": true,
"client_addr": "0.0.0.0",
"advertise_addr": "192.168.1.119" ## Add server IP here
}
######
$ sudo vi /etc/consul.d/proxysql.json
{"services": [
{
"id": "proxy1",
"name": "proxysql",
"address": "192.168.1.120",
"tags": ["mysql"],
"port": 6033,
"check": {
"script": "mysqladmin ping --host=192.168.1.120 --port=6033 --user=root --password=123",
"interval": "3s"}
},
{
"id": "proxy2",
"name": "proxysql",
"address": "192.168.1.121",
"tags": ["mysql"],
"port": 6033,
"check": {
"script": "mysqladmin ping --host=192.168.1.121 --port=6033 --user=root --password=123",
"interval": "3s"}
}
]
}

Start Consul agent
$ sudo su - consul -c ‘consul agent -config-file=/etc/consul.conf -config-dir=/etc/consul.d > /var/log/consul.log &‘

Setup DNSMASQ (as root)
vim /etc/dnsmasq.conf
resolv-file=/etc/resolv.conf
server=/consul/127.0.0.1#8600
service dnsmasq restart

Remember to add the localhost as a DNS server (this step can vary
depending on how your DNS servers are managed... here I‘m just
adding the following line to resolve.conf:
sudo vi /etc/resolve.conf
#... snippet ...#
nameserver 127.0.0.1
#... snippet ...#
Restart dnsmasq
sudo service dnsmasq restart

====================================================================

Configuration of Consul on Proxy Servers:
The next item is to configure each of the proxy Consul agents. Note that the “agent name” and the “IP address” need to be updated for each host (values for both must be unique):

Edit configuration files
$ sudo vi /etc/consul.conf
{
"datacenter": "dc1",
"data_dir": "/opt/consul/",
"log_level": "INFO",
"node_name": "agent2", ### Agent node name must be unique
"server": true,
"ui": true,
"bootstrap": false, ### Disable bootstrap on joiner nodes
"client_addr": "0.0.0.0",
"advertise_addr": "192.168.1.xxx", ### Set to local instance IP
"dns_config": {
"only_passing": true
}
}
######
$ sudo vi /etc/consul.d/proxysql.json
{"services": [
{
"id": "proxy1",
"name": "proxysql",
"address": "192.168.1.120",
"tags": ["mysql"],
"port": 6033,
"check": {
"script": "sh /etc/consul.d/test.sh",
"interval": "3s"}
},
{
"id": "proxy2",
"name": "proxysql",
"address": "192.168.1.121",
"tags": ["mysql"],
"port": 6033,
"check": {
"script": "sh /etc/consul.d/test.sh",
"interval": "3s"}
}
]
}

=============================================================

[[email protected] consul.d]# more test.sh
mysql -u haproxy -h 127.0.0.1 -P 3306 -e "select 1" >/dev/null 2>&1

if [ "$?" -ne 0 ]
then echo "not available"
exit 8
fi

==============================================================

Start Consul agent:
$ sudo su - consul -c ‘consul agent -config-file=/etc/consul.conf -config-dir=/etc/consul.d > /var/log/consul.log &‘
Join Consul cluster specifying 1st node IP e.g.
$ consul join 192.168.1.119
Verify logs and look out for the following messages:
$ cat /var/log/consul.log
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
Node name: ‘agent2‘
Datacenter: ‘dc1‘
Server: true (bootstrap: false)
Client Addr: 0.0.0.0 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
Cluster Addr: 192.168.1.120 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
Atlas:
==> Log data will now stream in as it occurs:
... snippet ...
2016/09/05 19:48:04 [INFO] agent: Synced service ‘consul‘
2016/09/05 19:48:04 [INFO] agent: Synced check ‘service:proxysql1‘
2016/09/05 19:48:04 [INFO] agent: Synced check ‘service:proxysql2‘
... snippet ...

=============================================================================

Install ProxySQL packages and initialise ProxySQL DB
sudo yum -y install https://github.com/sysown/proxysql/releases/proxysql-1.4.3-1-centos67.x86_64.rpm
sudo service proxysql initial
sudo service proxysql stop
Edit the ProxySQL configuration file to update username / password
vi /etc/proxysql.cnf

admin_variables=
{
admin_credentials="admin:admin"
mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock"
}

Start ProxySQL
sudo service proxysql start
Connect to ProxySQL and configure
mysql -P6032 -h127.0.0.1 -uadmin -padmin
First we create a replication hostgroup:
mysql> INSERT INTO mysql_replication_hostgroups VALUES (10,11,‘Standard Replication Groups‘);
Add both nodes to the hostgroup 11 (ProxySQL will automatically put the writer node in hostgroup 10)
mysql> INSERT INTO mysql_servers (hostname,hostgroup_id,port,weight) VALUES (‘192.168.1.120‘,10,3306,1000);
mysql> INSERT INTO mysql_servers (hostname,hostgroup_id,port,weight) VALUES (‘192.168.1.121‘,11,3306,1000);
Save server configuration
mysql> LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK;
Add query rules for RW split
mysql> INSERT INTO mysql_query_rules (active, match_pattern, destination_hostgroup, cache_ttl, apply) VALUES (1, ‘^SELECT . FOR UPDATE‘, 10, NULL, 1);
mysql> INSERT INTO mysql_query_rules (active, match_pattern, destination_hostgroup, cache_ttl, apply) VALUES (1, ‘^SELECT .
‘, 11, NULL, 1);
mysql> LOAD MYSQL QUERY RULES TO RUNTIME; SAVE MYSQL QUERY RULES TO DISK;
Finally configure ProxySQL user and save configuration
mysql> INSERT INTO mysql_users (username,password,active,default_hostgroup,default_schema) VALUES (‘root‘,‘123‘,1,10,‘test‘);
mysql> LOAD MYSQL USERS TO RUNTIME; SAVE MYSQL USERS TO DISK;
mysql> EXIT;

总结,坑点:
1,用户访问的数据库,主从,读写都是通过hostgroup 要定义和标识,所以这个参数一定对应

2,consul 健康检查
如果健康script 脚本返回error code 为1 ,code 1 只是warning ,还是会被解析,所以修改检查脚本内容,如果不匹配,手动返回error code 2或者其他值,之后这个异常服务才不解析。

原文地址:http://blog.51cto.com/538858/2327665

时间: 2024-10-12 01:15:08

Consul, ProxySQL and MySQL MHA(mysql 读写分离,高可用)的相关文章

MHA+ProxySQL实现读写分离高可用

最近在研究ProxySQL,觉得还挺不错的,所以就简单的折腾了一下,ProxySQL目前也是Percona在推荐的一个读写分离的中间件.关于详细的介绍可以参考官方文档.https://github.com/sysown/proxysql/wiki 本文主要介绍的是MHA+ProxySQL读写分离以及高可用,ProxySQL的细节请参考文档,目前已经有人写发非常详细了,文章最后会给出链接.当然和Group Replication,PXC搭配那是更完美的.关于MHA的配置参考我前面的文章,本文就不再

mysql mha 主从自动切换 高可用

mha(Master High Availability)目前在MySQL多服务器(超过二台),高可用方面是一个相对成熟的解决方案. 一,什么是mha,有什么特性 1. 主服务器的自动监控和故障转移 MHA监控复制架构的主服务器,一旦检测到主服务器故障,就会自动进行故障转移.即使有些从服务器没有收到最新的relay log,MHA自动从最新的从服务器上识别差异的relay log并把这些日志应用到其他从服务器上,因此所有的从服务器保持一致性了.MHA通常在几秒内完成故障转移,9-12秒可以检测出

Atlas读写分离[高可用]

Atlas下载地址: https://github.com/Qihoo360/Atlas/releases Atlas是出于360的, 比mysql-proxy更稳定, 部署起来更方便. 环境: proxy:192.168.99.60 master:192.168.99.61 slave:192.168.99.62 1.安装 由于我使用的是rpm包, 直接安装 rpm -ivh Atlas-2.2.el6.x86_64.rpm 就是这么简单, 安装完成. 目录默认在: /usr/local/my

MySQL主从复制、读写分离、高可用集群搭建

MySQL主从复制.读写分离.高可用集群搭建  一.服务介绍   1.1 Keepalived     Keepalived,见名知意,即保持存活,其目的是解决单点故障,当一台服务器宕机或者故障时自动切换到其他的服务器中.Keepalived是基于VRRP协议实现的.VRRP协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成虚拟设备,可以对外提供虚拟路由器IP(一个或多个),即漂移IP(VIP). 1.2 ProxySQL ProxySQL是一个高性能,高可用性的MySQL

mysql主从复制与读写分离

MySQL主从复制与读写分离 MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 Mysql作为目前世界上使用最广泛的免费数据库,相信所有从事系统运维的工程师都一定接触过.但在实际的生产环境中,由单台Mysql作为独立的数据库是完全不能满足实际需求的,无论是在安全性,高可用性以及高并发等各个方面. 因此,一般来说都是通过 主从复制(Master-Slave)的方式来同步数据,再通过读写分离(MySQL-Proxy)来提升数据库的并发负载能力 这样的方案来进行部

Amoeba+Mysql实现数据库读写分离

一.Amoeba 是什么 Amoeba(变形虫)项目,专注 分布式数据库 proxy 开发.座落与Client.DB Server(s)之间.对客户端透明.具有负载均衡.高可用性.sql过滤.读写分离.可路由相关的query到目标数据库.可并发请求多台数据库合并结果. 主要解决: ? 降低 数据切分带来的复杂多数据库结构 ? 提供切分规则并降低 数据切分规则 给应用带来的影响 ? 降低db 与客户端的连接数 ? 读写分离 二.为什么要用Amoeba 目前要实现mysql的主从读写分离,主要有以下

Mysql主从配置+读写分离(转)

   MySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具.因此,我们首先要在系统中源码编译安装cmake工具. 注:安装前须查看是否已经安装了如下依赖包,如果没有请安装. apt-get -y install gcc g++ libncurses5-dev ncurses-devel openssl 一.主库安装及配置 1.源码安装cmake # tar xf cmake-3.0.0.tar.gz # cd cmake-3.0.0 # .

Mysql 主从复制,读写分离

Mysql 主从复制及读写分离 特别推荐看:amoeba.xml 的配置部分,在百度上看了很多配置都不完整,是我测试时的痛点 实验的目的: 有两部分:第一实现Mysql主从复制,第二实现读写分离. 下面是实验的环境: 192.168.58.11     安装了amoeba 的节点 192.168.58.16      master    系统:rhel 5.4 192.168.58.12      slave     系统: rhel 5.4 192.168.58.11      代理     

Mysql的主从复制读写分离--简单篇

Mysql基础拓扑图: Mysql环境准备: 一台mysql主服务器(安装mysql) 两台mysql从服务器(安装mysql) 一台mysql代理(安装amoeba和java) 一台mysql客户端(mysql客户端) 部署前先关闭所有的iptables,selinux Mysql的主从复制读写分离所需安装包: cmake-2.8.6.tar.gz mysql-5.5.22.tar.gz amoeba-mysql-binary-2.2.0.tar.gz jdk-7u65-linux-x64.t

docker-compose.yml样例(mysql主从+mycat读写分离)

Docker-compose.yml文件示例 1.mysql主从复制的docker-compose.yml文件 # cat docker-compose.yml version: '2' # 这个version是指dockerfile解析时用的版本,不是给我们自己定义版本号用的. services: m1: # master build: ./master # ./master文件下需要有Dockerfile文件,并且build属性和image属性不能一起使用 container_name: m