MySQL中间件Atlas

MySQL中间件Atlas

Mysql 的 proxy 中间件有比较多的工具,例如,mysql-proxy(官方提供), atlas , cobar, mycat, tddl, tinnydbrouter等等。而Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目。它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bug,添加了很多功能特性。目前该项目在360公司内部得到了广泛应用,很多MySQL业务已经接入了Atlas平台,每天承载的读写请求数达几十亿条。

同时,有超过50家公司在生产环境中部署了Atlas,超过800人已加入了我们的开发者交流群,并且这些数字还在不断增加。而且安装方便。配置的注释写的蛮详细的,都是中文。

Atlas官方链接: https://github.com/Qihoo360/Atlas/blob/master/README_ZH.md

Atlas下载链接: https://github.com/Qihoo360/Atlas/releases

主要功能

Atlas主要功能(代理)

​ 1.读写分离

​ 2.从库负载均衡

​ 3.IP过滤

​ 4.自动分表

​ 5.DBA可平滑上下线DB(不影响用户的体验,把你的数据库下线)

​ 6.自动摘除宕机的DB

Atlas相对于官方MySQL-Proxy的优势

​ 1.将主流程中所有Lua代码用C重写,Lua仅用于管理接口

​ 2.重写网络模型、线程模型

​ 3.实现了真正意义上的连接池

​ 4.优化了锁机制,性能提高数十倍

使用场景

Atlas是一个位于前端应用与后端MySQL数据库之间的中间件,在后端DB看来,Atlas相当于连接它的客户端,在前端应用看来,Atlas相当于一个DB。Atlas作为服务端与应用程序通讯,它实现了MySQL的客户端和服务端协议,同时作为客户端与MySQL通讯。它对应用程序屏蔽了DB的细节,同时为了降低MySQL负担,它还维护了连接池。

Atlas使得应用程序员无需再关心读写分离、分表等与MySQL相关的细节,可以专注于编写业务逻辑,同时使得DBA的运维工作对前端应用透明,上下线DB前端应用无感知。

企业读写分离及分库分表其他方案了解

  • Mysql-proxy(oracle)

  • Mysql-router(oracle)

  • Atlas (Qihoo 360)

  • Atlas-sharding (Qihoo 360)

  • Cobar(是阿里巴巴(B2B)部门开发)

  • Mycat(基于阿里开源的Cobar产品而研发)

  • TDDL Smart Client的方式(淘宝)

  • Oceanus(58同城数据库中间件)

  • OneProxy(原支付宝首席架构师楼方鑫开发 )

  • vitess(谷歌开发的数据库中间件)

  • Heisenberg(百度)

  • TSharding(蘑菇街白辉)

  • Xx-dbproxy(金山的Kingshard、当当网的sharding-jdbc )

  • amoeba

安装Atlas

# 虽然包时el6的,但是centos7也能用
wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm
 
rpm -ivh Atlas-2.2.1.el6.x86_64.rpm

mysql库创建账号

grant all on *.* to 'root'@'%' identified by '123456';
flush privileges;

grant all on *.* to 'shanhe'@'%' identified by '123456';
flush privileges;

配置

[root@manager ~]# cd /usr/local/mysql-proxy/conf
[root@manager mysql-proxy]# vim test.cnf
[mysql-proxy]

#带#号的为非必需的配置项目

#管理接口的用户名
admin-username = shanhe

#管理接口的密码
admin-password = 123456

#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 192.168.0.10:3306 

#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
proxy-read-only-backend-addresses = 192.168.0.14:3306,192.168.0.6:3306

#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
pwds = root:/iZxz+0GRoA=, shanhe:/iZxz+0GRoA=

#设置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

启动服务

#1、启动,配置文件名为test.conf对应此处的test
[root@VM-0-11-centos conf]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started
 
#2、验证启动(没起来他也显示OK)
[root@VM-0-11-centos conf]# ps -ef|grep [m]ysql-proxy
[root@VM-0-11-centos conf]# netstat -lntup|grep [m]ysql-proxy
 
#3、查看日志定位问题
tail -f /usr/local/mysql-proxy/log/test.log

Atlas使用

[root@VM-0-10-centos ~]# mysql -ushanhe -p123456 -h192.168.0.11 -P2345
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 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)

mysql> SELECT * FROM backends;
+-------------+-------------------+-------+------+
| backend_ndx | address           | state | type |
+-------------+-------------------+-------+------+
|           1 | 192.168.0.10:3306 | up    | rw   |
|           2 | 192.168.0.14:3306 | up    | ro   |
|           3 | 192.168.0.6:3306  | up    | ro   |
+-------------+-------------------+-------+------+
3 rows in set (0.00 sec)
posted @ 2021-10-24 13:04  小丶凡  阅读(197)  评论(0编辑  收藏  举报
1