目录
Atlas介绍
Atlas是由 Qihoo 360 Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目。它是在mysql-proxy 0.8.2版本的基础上,对其进行了优化,增加了一些新的功能特性。
Atlas主要功能如下:
- 读写分离
- 从库负载均衡
- IP过滤
- 自动分表
- 自动清除宕机DB
Atlas部署
-
下载安装Atlas软件
[root@slave02 ~]# wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm [root@slave02 ~]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
-
查看Atlas文件
[root@slave02 ~]# ll /usr/local/mysql-proxy/ total 0 drwxr-xr-x 2 root root 75 Oct 5 15:53 bin # 可执行文件 drwxr-xr-x 2 root root 22 Oct 5 15:53 conf # 配置文件 drwxr-xr-x 3 root root 331 Oct 5 15:53 lib # 库文件 drwxr-xr-x 2 root root 6 Dec 17 2014 log # 日志文件
-
查看配置文件
[root@slave02 conf]# cat test.cnf [mysql-proxy] #带#号的为非必需的配置项目 #管理接口的用户名 admin-username = user #管理接口的密码 admin-password = pwd #Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔 proxy-backend-addresses = 127.0.0.1:3306 #Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔 #proxy-read-only-backend-addresses = 127.0.0.1:3305@1 #用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码! pwds = user1:+jKsgB3YAG8=, user2:GS+tr4TPgqc= #设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。 daemon = true #设置Atlas的运行方式,设为true时Atlas会启动两个进程,一个为monitor,一个为worker,monitor在worker意外退出后会自动将其重启,设为false时只有worker,没有monitor,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。 keepalive = true #工作线程数,对Atlas的性能有很大影响,可根据情况适当设置 event-threads = 8 #日志级别,分为message、warning、critical、error、debug五个级别 log-level = message #日志存放的路径 log-path = /usr/local/mysql-proxy/log #SQL日志的开关,可设置为OFF、ON、REALTIME,OFF代表不记录SQL日志,ON代表记录SQL日志,REALTIME代表记录SQL日志且实时写入磁盘,默认为OFF #sql-log = OFF #慢日志输出设置。当设置了该参数时,则日志只输出执行时间超过sql-log-slow(单位:ms)的日志记录。不设置该参数则输出全部日志。 #sql-log-slow = 10 #实例名称,用于同一台机器上多个Atlas实例间的区分 #instance = test #Atlas监听的工作接口IP和端口 proxy-address = 0.0.0.0:1234 #Atlas监听的管理接口IP和端口 admin-address = 0.0.0.0:2345 #分表设置,此例中person为库名,mt为表名,id为分表字段,3为子表数量,可设置多项,以逗号分隔,若不分表则不需要设置该项 #tables = person.mt.id.3 #默认字符集,设置该项后客户端不再需要执行SET NAMES语句 #charset = utf8 #允许连接Atlas的客户端的IP,可以是精确IP,也可以是IP段,以逗号分隔,若不设置该项则允许所有IP连接,否则只允许列表中的IP连接 #client-ips = 127.0.0.1, 192.168.1 #Atlas前面挂接的LVS的物理网卡的IP(注意不是虚IP),若有LVS且设置了client-ips则此项必须设置,否则可以不设置 #lvs-ips = 192.168.1.1
-
编辑配置文件
[mysql-proxy] admin-username = user admin-password = pwd proxy-backend-addresses = 192.168.1.10:3306 # 写节点(配合MHA,此处可以是VIP地址) proxy-read-only-backend-addresses = 192.168.1.6:3306,192.168.1.7:3306 # 读节点 pwds = repl:3yb5jEku5h4=,mha:O2jBXONX098= # 后端数据库用户(密码是加密后的密文) daemon = true keepalive = true event-threads = 8 log-level = message log-path = /usr/local/mysql-proxy/log sql-log=ON proxy-address = 0.0.0.0:33060 admin-address = 0.0.0.0:2345 charset=utf8
-
启动Atlas
[root@slave02 ~]# /usr/local/mysql-proxy/bin/mysql-proxyd test start OK: MySQL-Proxy of test is started [root@slave02 ~]# netstat -lntup | grep 33060 # 对外提供服务的端口 tcp 0 0 0.0.0.0:33060 0.0.0.0:* LISTEN 13200/mysql-proxy [root@slave02 ~]# netstat -lntup | grep 2345 # 管理端口 tcp 0 0 0.0.0.0:2345 0.0.0.0:* LISTEN 13200/mysql-proxy
-
Atlas功能测试
测试读操作:在两个从库上轮询[root@slave02 ~]# mysql -umha -pmha -h 192.168.1.7 -P 33060 db03 [(none)]>select @@server_id; +-------------+ | @@server_id | +-------------+ | 7 | +-------------+ 1 row in set (0.51 sec) db03 [(none)]>select @@server_id; +-------------+ | @@server_id | +-------------+ | 6 | +-------------+ 1 row in set (0.00 sec) db03 [(none)]>select @@server_id; +-------------+ | @@server_id | +-------------+ | 7 | +-------------+ 1 row in set (0.00 sec)
测试写操作:只会在主库进行
db03 [(none)]>begin; Query OK, 0 rows affected (0.15 sec) db03 [(none)]>select @@server_id; +-------------+ | @@server_id | +-------------+ | 5 | +-------------+ 1 row in set (0.12 sec) db03 [(none)]>commit; Query OK, 0 rows affected (0.03 sec)
需求:在数据库中创建root管理员用户,host为10.0.0%进行管理
-
主节点创建用户
> grant all on *.* to root@'192.168.1.%' identified by '123';
-
在Atlas配置文件添加用户
[root@slave02 ~]# /usr/local/mysql-proxy/bin/encrypt 123 # 加密密文 [root@slave02 ~]#vim test.cnf pwds = repl:3yb5jEku5h4=,mha:O2jBXONX098=,root:3yb5jEku5h4= [root@slave02 ~]#/usr/local/mysql-proxy/bin/mysql-proxyd test restart [root@slave02 ~]# mysql -uroot -p123 -h 192.168.1.7 -P 33060
Atlas基本管理
-
连接管理接口
[root@slave02 ~]# mysql -uuser -ppwd -h127.0.0.1 -P2345
-
查看所有管理帮助
db03 [(none)]>select * from help; +----------------------------+---------------------------------------------------------+ | command | description | +----------------------------+---------------------------------------------------------+ | SELECT * FROM help | shows this help | | SELECT * FROM backends | lists the backends and their state | | SET OFFLINE $backend_id | offline backend server, $backend_id is backend_ndx's id | | SET ONLINE $backend_id | online backend server, ... | | ADD MASTER $backend | example: "add master 127.0.0.1:3306", ... | | ADD SLAVE $backend | example: "add slave 127.0.0.1:3306", ... | | REMOVE BACKEND $backend_id | example: "remove backend 1", ... | | SELECT * FROM clients | lists the clients | | ADD CLIENT $client | example: "add client 192.168.1.2", ... | | REMOVE CLIENT $client | example: "remove client 192.168.1.2", ... | | SELECT * FROM pwds | lists the pwds | | ADD PWD $pwd | example: "add pwd user:raw_password", ... | | ADD ENPWD $pwd | example: "add enpwd user:encrypted_password", ... | | REMOVE PWD $pwd | example: "remove pwd user", ... | | SAVE CONFIG | save the backends to config file | | SELECT VERSION | display the version of Atlas | +----------------------------+---------------------------------------------------------+ 16 rows in set (0.00 sec)
-
查看后端所有节点信息
db03 [(none)]>select * from backends; +-------------+-------------------+-------+------+ | backend_ndx | address | state | type | +-------------+-------------------+-------+------+ | 1 | 192.168.1.10:3306 | up | rw | | 2 | 192.168.1.6:3306 | up | ro | | 3 | 192.168.1.7:3306 | up | ro | +-------------+-------------------+-------+------+ 3 rows in set (0.00 sec)
-
动态添加节点
db03 [(none)]>ADD SLAVE 192.168.1.8:3306; Empty set (0.00 sec) db03 [(none)]>select * from backends; +-------------+-------------------+-------+------+ | backend_ndx | address | state | type | +-------------+-------------------+-------+------+ | 1 | 192.168.1.10:3306 | up | rw | | 2 | 192.168.1.6:3306 | up | ro | | 3 | 192.168.1.7:3306 | up | ro | | 4 | 192.168.1.8:3306 | down | ro | +-------------+-------------------+-------+------+ 4 rows in set (0.00 sec)
-
动态删除节点
db03 [(none)]>select * from backends; +-------------+-------------------+-------+------+ | backend_ndx | address | state | type | +-------------+-------------------+-------+------+ | 1 | 192.168.1.10:3306 | up | rw | | 2 | 192.168.1.6:3306 | up | ro | | 3 | 192.168.1.7:3306 | up | ro | +-------------+-------------------+-------+------+ 3 rows in set (0.00 sec)
-
保存到配置文件
db03 [(none)]>save config; Empty set (0.00 sec)
Atlas结合MHA故障恢复
-
switch_Atlas.sh
# 脚本执行条件 1.主从复制中,配置MHA服务(binlog server / VIP),Atlas服务 2.主库down掉 3.主从复制集群中,所有节点配置Atlas 4.主从复制集群中,所有节点配置该脚本 5.看主从复制现有的主从关系,修改配置文件中主从的指定(Atlas的配置文件在所有主机相同) # 执行结果 1.使down的主库以从库的身份加入主从复制 2.manager所在主机MHA恢复运行 3.拉取down掉主机的binlog到manager指定目录 4.Atlas恢复读写分离 [root@db03 ~]# vim switch_Atlas.sh #/bin/bash # 1.获取新的主库IP(该ip被VIP占用) new_master=`ssh 192.168.1.7 "grep 'as a new master' /var/log/mha/manager" | tail -1 | awk -F '[ ,(]' '{print $2}'` # 2.获取新的主库在Atlas中的ID new_master_id=`mysql -uuser -ppwd -h127.0.0.1 -P 2345 -e "SELECT * FROM backends" | grep $new_master | awk '{print $1}'` # 3.移除提升为主库的从库 mysql -uuser -ppwd -h127.0.0.1 -P 2345 -e "REMOVE BACKEND $new_master_id" &> /dev/null # 4.恢复主从复制 MHA sh /root/start_mha.sh # 6.获取down掉的主机的IP:端口(该ip将以从库的身份加入主从) down_server_port=`ssh 192.168.1.7 "grep 'Master .* is down' /var/log/mha/manager" | tail -1 | awk -F '[ ,()]' '{print $3}'` # 7.添加down的主库为新的从库到Atlas mysql -uuser -ppwd -h127.0.0.1 -P 2345 -e "add slave $down_server_port" &> /dev/null # 8.保存配置 mysql -uuser -ppwd -h127.0.0.1 -P 2345 -e "save config" &> /dev/null
-
start_mha.sh
# 脚本执行条件 1.主从复制中,配置MHA服务(binlog server / VIP) 2.主库down掉 # 执行结果(哪台主机down掉在哪执行) 1.使down的主库以从库的身份加入主从复制 2.MHA恢复运行 3.拉取down掉主机的binlog到manager指定目录 [root@db04 ~]# cat start_mha.sh # 判断mysqld服务是否挂掉 mysql_pid=`ps -ef | grep [m]ysqld | wc -l` # 如果挂掉则重启,如果没挂则杀掉重启 if [ $mysql_pid -eq 0 ]; then systemctl start mysqld else pkill mysqld systemctl start mysqld fi sleep 3 # 获取change master to语句 change=`ssh 192.168.1.7 "grep 'CHANGE MASTER TO' /var/log/mha/manager | tail -1 | sed 's#xxx#123#g'" | awk -F: '{print $4}'` # 重启数据库后,执行change master to mysql -uroot -p123 -e "${change};start slave" &>/dev/null # 修复MHA配置文件 ssh 192.168.1.7 "\cp /etc/mha/mysql.cnf.bak /etc/mha/mysql.cnf" # 过滤出down主机的ip down_ip=`grep 'is down!' /var/log/mha/manager|tail -1|awk -F'[ ,(]' '{print $2}'` # 启动保存binlog ssh 192.168.1.7 'cd /binlog/ && nohup mysqlbinlog -R --host="$down_ip" --user=mha --password=mha --raw --stop-never mysql-bin.000001 &>/dev/null &' # 启动MHA ssh 192.168.1.7 'nohup masterha_manager --conf=/service/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /service/mha/manager.log 2>&1 &'
读写分离建议
- MySQL-Router → MySQL官方
- ProxySQL → Percona
- Maxscale → MariaDB