Mysql访问中间件--Atlas初探

Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目。它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bug,添加了很多功能特性,目前该项目在360公司内部得到了广泛应用。

Atlas主要有以下功能

1.读写分离

2.从库负载均衡

3.IP过滤

4.自动分表(目前只支持在同一个库中进行分表)

5.DBA可平滑上下线DB

6.自动摘除宕机的DB

Atlas相对于官方MySQL-Proxy有以下优势

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

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

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

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

下面就一步一步带大家去安装和使用Atlas数据库中间件

安装Atlas-sharding_1.0.1-el6.x86_64.rpm包

大家可以从https://github.com/Qihoo360/Atlas/releases网站下载到最新的rpm包,推荐使用rpm包安装

加密应用访问mysql数据库访问密码

安装好Atlas的rpm包之后,进入到/usr/local/mysql-proxy/bin目录,使用下面命令对密码进行加密

./encrypt tony

ANDKNNypf4k=   <--这个就是加密后的密码

配置Atlas配置文件(/usr/local/mysql-proxy/conf/opentest.cnf)

[mysql-proxy]



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



#管理接口的用户名

admin-username = user



#管理接口的密码

admin-password = pwd12345



#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔

proxy-backend-addresses = 10.10.57.206:3306



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

proxy-read-only-backend-addresses = 10.10.57.207:3306@1,10.10.57.208:3306@1



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

pwds = tony:ANDKNNypf4k=



#设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。

daemon = true



#设置Atlas的运行方式,设为true时Atlas会启动两个进程,一个为monitor,一个为worker,monitor在worker意外退出后会自动将其重启,设为false时只有worker,没有monitor,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。

keepalive = true



#工作线程数,对Atlas的性能有很大影响,推荐设置成系统的CPU核数的2至4倍

event-threads = 2



#日志级别,分为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 = REALTIME



#实例名称,用于同一台机器上多个Atlas实例间的区分

instance = opentest



#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

启动Atlas服务

/usr/local/mysql-proxy/bin/mysql-proxyd opentest start

检查Atlas服务状态

扫描二维码关注公众号,回复: 13716839 查看本文章
ps -ef||grep -i mysql

/usr/local/mysql-proxy/bin/mysql-proxyd opentest status

连接Atlas管理

mysql -h10.10.57.205 -P2345 -uuser -ppwd12345

连接好之后,可以使用select * from help;查看可以查看Atlas的管理命令
在这里插入图片描述
例如:查看mysql库的读写分离信息

mysql> SELECT * FROM backends;

+----------+-------------------+-------+------+-------------+

| group_id | address           | state | type | backend_ndx |

+----------+-------------------+-------+------+-------------+

|       -1 | 10.10.57.206:3306 | up    | rw   |           1 |

|       -1 | 10.10.57.207:3306 | up    | ro   |           2 |

|       -1 | 10.10.57.208:3306 | up    | ro   |           3 |

+----------+-------------------+-------+------+-------------+

3 rows in set (0.00 sec)

测试应用连接Atlas服务

mysql -h10.10.57.205 -P1234 -utony -ptony

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 11

Server version: 5.0.81-log MySQL Community Server (GPL)



Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.



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> 

mysql> select * from test.t_test;

+----+-------+

| id | name  |

+----+-------+

| 1  | test1 |

| 2  | test2 |

+----+-------+

2 rows in set (0.00 sec)

通过查看sql运行日志,可以看到应用已经实现了读写分离和负载均衡

[08/30/2018 16:48:18] C:10.10.57.208:56858 S:10.10.57.206:3306 OK 13.602 "insert into t_test values('1','test1')"

[08/30/2018 16:48:35] C:10.10.57.208:56858 S:10.10.57.206:3306 OK 12.519 "insert into t_test values('2','test2')"

[08/30/2018 16:48:47] C:10.10.57.208:56858 S:10.10.57.208:3306 OK 0.414 "select * from t_test"

[08/30/2018 16:48:47] C:10.10.57.208:56858 S:10.10.57.207:3306 OK 0.456 "select * from t_test"

[08/30/2018 16:48:48] C:10.10.57.208:56858 S:10.10.57.208:3306 OK 0.413 "select * from t_test"

参考连接 :

Mysql访问中间件–Atlas初探

https://mp.weixin.qq.com/s/PYs93l0x9jzPk28VfvMAFg

猜你喜欢

转载自blog.csdn.net/qq_40907977/article/details/119915298