读写分离软件
1 2 3 4 5 6
| Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项 目。它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bug,添加了很多功能特 性。目前该项目在360公司内部得到了广泛应用,很多MySQL业务已经接入了Atlas平台,每天承载的读 写请求数达几十亿条。同时,有超过50家公司在生产环境中部署了Atlas,超过800人已加入了我们的开 发者交流群,并且这些数字还在不断增加。
mysql-proxy Atlas:阿里 Atlas:360 mycat
|
Atlas功能
1 2 3 4 5 6
| 1.读写分离 2.从库负载均衡 3.IP过滤 4.自动分表 5.DBA可平滑上下线DB 6.自动摘除宕机的DB
|
Atlas相对于官方MySQL-Proxy的优势
1.将主流程中所有 Lua 代码用C重写,Lua仅用于管理接口
2.重写网络模型、线程模型
3.实现了真正意义上的连接池
4.优化了锁机制,性能提高数十倍
安装配置Atlas
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| [root@mha-manager ~]# wget http://test.driverzeng.com/MySQL_plugins/Atlas-2.2.1.el6.x86_64.rpm
[root@mha-manager ~]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm [root@mha-manager ~]# ll /usr/local/mysql-proxy/ drwxr-xr-x 2 root root 75 Aug 28 15:54 bin drwxr-xr-x 2 root root 22 Aug 28 15:54 conf drwxr-xr-x 3 root root 331 Aug 28 15:54 lib drwxr-xr-x 2 root root 6 Dec 17 2014 log
[root@db01 ~]# cat /usr/local/mysql-proxy/conf/test.cnf [mysql-proxy]
admin-username = user
admin-password = pwd
proxy-backend-addresses = 172.16.1.51:3306
proxy-read-only-backend-addresses = 172.16.1.52:3306@1,172.16.1.53:3306@1,172.16.1.54:3306@1
pwds = root:3yb5jEku5h4=, mha:3yb5jEku5h4=
daemon = true
keepalive = true
event-threads = 8
log-level = error
log-path = /usr/local/mysql-proxy/log
sql-log = ON
proxy-address = 0.0.0.0:1234
admin-address = 0.0.0.0:2345
charset = utf8
client-ips = 127.0.0.1, 172.16.1
[root@mha ~]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
|
Atlas管理接口使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| [root@mha-manager ~]# mysql -uuser -ppwd -h127.0.0.1 -P2345
mysql> select * from help; db01 [(none)]>select * from help; +----------------------------+---------------------------------------------------------+ | command | description | +----------------------------+---------------------------------------------------------+ | SELECT * FROM help | 查看帮助 | SELECT * FROM backends | 查看后端数据库 | SET OFFLINE $backend_id | 下线数据库 set offline 2; | SET ONLINE $backend_id | 上线数据库 set online 2; | ADD MASTER $backend | 添加一个主库 add master 172.16.1.88:3306; | ADD SLAVE $backend | 添加一个从库 add slave 172.16.1.77:3306; | REMOVE BACKEND $backend_id | 删除后端数据库 remove backend 2; | SELECT * FROM clients | 查看可连接的客户端 select * from clients | ADD CLIENT $client | 添加一个可连接atlas的客户端 add client 10.0.0; | REMOVE CLIENT $client | 删除客户端 remove client 10.0.0 | SELECT * FROM pwds | 查看所有可连接atlas的用户和密码 select * from pwds; | ADD PWD $pwd | 添加一个明文的密码用户 add pwd mha:mha; | ADD ENPWD $pwd | 添加一个加密的密码用户 add enpwd tls:O2jBXONX098=; | REMOVE PWD $pwd | 删除用户 remove pwd zls; | SAVE CONFIG | 保存配置文件 | SELECT VERSION | 查看atlas版本号 +----------------------------+---------------------------------------------------------+
db01 [(none)]>select * from backends; +-------------+------------------+-------+------+ | backend_ndx | address | state | type | +-------------+------------------+-------+------+ | 1 | 172.16.1.51:3306 | up | rw | | 2 | 172.16.1.52:3306 | up | ro | | 3 | 172.16.1.53:3306 | up | ro | | 4 | 172.16.1.54:3306 | up | ro | +-------------+------------------+-------+------+
mysql> set offline 2;
mysql> set online 2;
|
Atlas结合MHA脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #!/bin/bash mha_log="/etc/mha/logs/manager.log" down_master=$(sed -nr 's#(.*)\(.*\) \(current master\)$#\1#gp' ${mha_log}|tail-1) new_master=$(sed -nr 's#(.*)\(.*\) \(new master\)$#\1#gp' /etc/mha/logs/manager.log|tail -1)
atlas_manager_user='user' atlas_manager_password='pwd' atlas_manager_port='2345' atlas_conn="mysql -u${atlas_manager_user} -p${atlas_manager_password} -P${atlas_manager_port}"
for atlas_ip in 51 52 53 54;do down_master_backend_id=$(${atlas_conn} -h172.16.1.${atlas_ip} -e 'select * from backends'|grep "${new_master}"|awk '{print $1}') ${atlas_conn} -h172.16.1.${atlas_ip} -e "remove backend ${down_master_backend_id}" ${atlas_conn} -h172.16.1.${atlas_ip} -e "add slave ${down_master}:3306;save config" done
[root@mha-manager ~]# vim /etc/mha/app1/master_ip_failover sub start_vip() { `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`; `/bin/sh -x \"/root/atlas_mha.sh\"`; }
|
1 2
| mysqladmin -umha -pmha -h172.16.1.55 ping
|