DevOps开发运维
成长之路

MySQL读写分离架构Atlas

其他扩展:ProxySQL(重要)/Maxscale/Mycat/DBLE

Atlas介绍

Atlas是由奇虎360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目。
它是在mysql-proxy 0.8.2版本的基础上,对其进行了优化,增加了一些新的功能特性。适合读多写少。
360内部使用Atlas运行的mysql业务,每天承载的读写请求数达几十亿条。
下载地址
https://github.com/Qihoo360/Atlas/releases
注意:
1、Atlas只能安装运行在64位的系统上
2、Centos 5.X安装 Atlas-XX.el5.x86_64.rpm,Centos 6.X安装Atlas-XX.el6.x86_64.rpm。
3、后端mysql版本应大于5.1,建议使用Mysql 5.6以上

安装配置

[root@db03 ~]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
[root@db03 ~]# cd /usr/local/mysql-proxy/conf
[root@db03 conf]# vi test.cnf
[mysql-proxy]
admin-username = user
admin-password = pwd
proxy-backend-addresses = 10.0.0.205:3306
proxy-read-only-backend-addresses = 10.0.0.201:3306,10.0.0.203: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@db03 conf]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started
[root@db03 conf]#ps -ef |grep proxy
[root@db03 conf]# netstat -lnp|grep 33060
tcp 0 0 0.0.0.0:33060 0.0.0.0:* LISTEN 17842/mysql-proxy 
[root@db03 conf]# netstat -lnp|grep 2345
tcp 0 0 0.0.0.0:2345 0.0.0.0:* LISTEN 17842/mysql-proxy

Atlas功能测试

[root@db03 conf]# mysql -umha -pmha -h 10.0.0.203 -P 33060
测试读操作:
mysql -umha -pmha -h 10.0.0.203 -P 33060 
db03 [(none)]>select @@server_id;
db03 [(none)]>select @@server_id;
+-------------+
| @@server_id |
+-------------+
|         201 |
+-------------+
1 row in set (0.00 sec)
db03 [(none)]>select @@server_id;
+-------------+
| @@server_id |
+-------------+
|         203 |
+-------------+
10.0.0.201和10.0.0.203轮询显示
测试写操作:
mysql> begin;select @@server_id;commit;

生产用户要求

开发人员申请一个应用用户 app( select update insert) 密码123456,要通过10网段登录
1. 在主库中,创建用户
db01 [(none)]>grant select ,update,insert on *.* to app@'10.0.0.%' identified by '123456';
2. 在atlas中添加生产用户
[root@db03 conf]# /usr/local/mysql-proxy/bin/encrypt 123456
/iZxz+0GRoA=
---->制作加密密码
[root@db03 conf]# vim test.cnf
[mysql-proxy]
repl:3yb5jEku5h4=,mha:O2jBXONX098=,app:/iZxz+0GRoA=
[root@db01 ~]# mysql -uapp -p123456 -h 10.0.0.203 -P 33060

Atlas基本管理

连接管理接口
db03 [(none)]>mysql -uuser -ppwd -h127.0.0.1 -P2345
获取Atlas帮助
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 |
+----------------------------+---------------------------------------------------------+
查询后端所有节点信息
db03 [(none)]> SELECT * FROM backends;
+-------------+-----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+-----------------+-------+------+
| 1 | 10.0.0.205:3306 | up | rw |
| 2 | 10.0.0.201:3306 | up | ro |
| 3 | 10.0.0.203:3306 | up | ro |
+-------------+-----------------+-------+------+
动态添加删除节点
REMOVE BACKEND 3;
动态添加节点
ADD SLAVE 10.0.0.53:3306;
保存配置到配置文件
SAVE CONFIG;

自动分表

使用Atlas的分表功能时,首先需要在配置文件test.cnf设置tables参数。
tables参数设置格式:数据库名.表名.分表字段.子表数量,
比如:
你的数据库名叫school,表名叫stu,分表字段叫id,总共分为2张表,那么就写为school.stu.id.2,如果还有其他的分表,以逗号分隔即可。

关于读写分离建议

MySQL-Router ---> MySQL官方
ProxySQL --->Percona
Maxscale ---> MariaDB
赞(1)

评论 抢沙发

评论前必须登录!

 

LNMP社群 不仅仅是技术

关于我们网站地图