1.简介
Atlas是一个位于应用程序与MySQL之间中间件。在后端DB看来,Atlas相当于连接它的客户端,在前端应用看来,Atlas相当于一个DB。Atlas作为服务端与应用程序通讯,它 实现了MySQL的客户端和服务端协议,同时作为客户端与MySQL通讯。它对应用程序 屏蔽了DB的细节,同时为了降低MySQL负担,它还维护了连接池
Atlas相对于官方MySQL-Proxy的优势
(1).将主流程中所有Lua代码用C重写,Lua仅用于管理接口
(2).重写网络模型、线程模型
(3).实现了真正意义上的连接池
(4).优化了锁机制,性能提高数十倍
这货做读写分离的中间件还是不错的。
(1).读写分离
(2).从库负载均衡
(3).IP过滤
(4).自动分表 #可以分表,功能有点鸡肋
(5).DBA可平滑上下线DB
(6).自动摘除宕机的DB
如果又想读写分离,又想分表,那还是用Mycat吧。
如果环境不大,就想搞一套主从高可用那就用MHA吧。
目前Atlas还不能支持MySQL8.0。如果你的后端MySQL是8.0版本,那么在Atlas上面需要5.6以上的MySQL,不用实际安装上,只要提供环境变量就可以了。相当于用mysql5.6的客户端连接后端mysql8.0的MySQL server
2.安装
(1)主机规划
CentOS7 192.168.91.128 Atlas
CentOS7 192.168.91.129 MySQL-Master
CentOS7 192.168.91.130 MySQL-Slave
(2)MySQL主从复制
完成MySQL的主从复制功能,这里就不细说了,可参考前面的文章
(3)在Atlas上配置MySQL5.7(我这里是8.0环境)
tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql
echo "PATH=\$PATH:\$HOME/bin:/usr/local/mysql/bin">> /etc/profile
source /etc/profile
mysql -V
mysql Ver 14.14 Distrib 5.7.17, for linux-glibc2.5 (x86_64) using EditLine wrapper
(4)安装Atlas
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
(5)在Master上创建一个远程用户用来连接MySQL-proxy
mysql> create user 'atlas'@'192.168.91.%' identified by 'dddddd';
Query OK, 0 rows affected (0.06 sec)
mysql> grant all on *.* to 'atlas'@'192.168.91.%';
Query OK, 0 rows affected (0.02 sec)
3.配置
Atlas运行需要依赖一个配置文件(test.cnf)。在运行Atlas之前,需要对该文件进行配置。Atlas的安装目录是/usr/local/mysql-proxy,进入安装目录下的conf目录,可以看 到已经有一个名为test.cnf的默认配置文件,我们只需要修改里面的某些配置项
cd /usr/local/mysql-proxy/conf
mv test.conf test.conf.ora
cp test.conf.ora test.conf
cat /usr/local/mysql-proxy/conf/test.conf
[mysql-proxy]
#带#号的为非必需的配置项目
#管理接口的用户名
admin-username = atlas
#管理接口的密码
admin-password = dddddd
#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 192.168.91.129:3306
#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
proxy-read-only-backend-addresses = 192.168.91.130:3306
#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!例bin/encrypt zhang870731
pwds = atlas:ED8BNIrVVq+9dJZrK56Khw==
#设置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 = OFF
#慢日志输出设置。当设置了该参数时,则日志只输出执行时间超过sql-log-slow(单位:ms)的日志记录。不设置该参数则输出全部日志。
#sql-log-slow = 10
#实例名称,用于同一台机器上多个Atlas实例间的区分
#instance = test
#Atlas监听的工作接口IP和端口
proxy-address = 0.0.0.0:3306
#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
4.启动Atlas
(1)配置环境变量
vim /etc/profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql-proxy/bin
(2)启动
cd /usr/local/mysql-proxy/bin
mysql-proxyd test start
OK: MySQL-Proxy of test is started
(3)测试连接管理接口
[root@proxy bin]# mysql -h127.0.0.1 -u atlas -P 2345 -p
Enter password:
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, 2016, 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> 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.91.129:3306 | up | rw |
| 2 | 192.168.91.130:3306 | up | ro |
+-------------+---------------------+-------+------+
2 rows in set (0.00 sec)
可以看出这里管理的内容和test.cnf配置文件内使用的内容相关 。这里和直接修改配置文件的区别是,在这里执行语句的修改可以立即生效,无需重再重启atlas服务 。
(4)连接工作接口
[root@proxy bin]# mysql -h127.0.0.1 -u atlas -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.81-log MySQL Community Server - GPL
Copyright (c) 2000, 2016, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.15 |
+-----------+
1 row in set (0.00 sec)
(5)在atlas上测试sql操作
mysql> create database t;
Query OK, 1 row affected (0.02 sec)
mysql> use t;
Database changed
mysql> create table test (id int,name varchar(10));
Query OK, 0 rows affected (0.12 sec)
mysql> insert into t values (1,'xy');
ERROR 1146 (42S02): Table 't.t' doesn't exist
mysql> insert into test values (1,'xy');
Query OK, 1 row affected (0.12 sec)
mysql> insert into test values (2,'momo');
Query OK, 1 row affected (0.01 sec)
mysql> insert into test values (3,'yueyue');
Query OK, 1 row affected (0.01 sec)
5.总结
(1)读写分离
如果读的操作比较多,也可以把写的节点写到proxy-read-only-backend-addresses,用逗号分隔
#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
proxy-read-only-backend-addresses = 192.168.91.130:3306,192.168.91.129:3306
(2)负载均衡
#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡, 若省略则默认为1,可设置多项,用逗号分隔
proxy-read-only-backend-addresses = 192.168.91.130:3306@1,192.168.91.129:3306@1