MySQL Atlas介绍
一、MySQL Atlas介绍
1.1.1 MySQL Atlas介绍
- MySQL Atlas介绍
Atlas是由 Qihoo 360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目。
它是在mysql-proxy 0.8.2版本的基础上,对其进行了优化,增加了一些新的功能特性。
360内部使用Atlas运行的mysql业务,每天承载的读写请求数达几十亿条。
软件下载地址:https://github.com/Qihoo360/Atlas/releases
常用读写分离软件
1、Atlas 下载地址:https://github.com/Qihoo360/Atlas/releases
2、proxySQL 官方地址:https://www.proxysql.com/
3、maxscale 官方地址:https://mariadb.com/kb/en/maxscale-22-getting-started/
4、MySQL-Router MySQL官方
- Atlas 部署
MHA+Atlas 高可用读写分离(部署前提有MHA高可用环境)
MHA高可用部署:https://www.cnblogs.com/woaiyunwei/p/13210749.html
[root@db03 /server/tools]# ll Atlas-2.2.1.el6.x86_64.rpm
-rw-r--r-- 1 root root 4963681 Jul 2 14:41 Atlas-2.2.1.el6.x86_64.rpm
[root@db03 /server/tools]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:Atlas-2.2.1-1 ################################# [100%]
[root@db03 /server/tools]# rpm -qa Atlas
Atlas-2.2.1-1.x86_64
[root@db03 /server/tools]# rpm -ql Atlas
/usr/local/mysql-proxy/bin/VERSION
/usr/local/mysql-proxy/bin/encrypt
/usr/local/mysql-proxy/bin/mysql-proxy
/usr/local/mysql-proxy/bin/mysql-proxyd
/usr/local/mysql-proxy/conf/test.cnf #配置文件
- 配置Atlas配置文件
[root@db03 /server/tools]# cd /usr/local/mysql-proxy/conf/
[root@db03 /usr/local/mysql-proxy/conf]# cp test.cnf{,.backup}
[root@db03 /usr/local/mysql-proxy/conf]# ll
total 8
-rw-r--r-- 1 root root 2810 Dec 17 2014 test.cnf
-rw-r--r-- 1 root root 2810 Jul 2 14:52 test.cnf.backup
[root@db03 /usr/local/mysql-proxy/conf]# cat test.cnf
[mysql-proxy]
#带#号的为非必需的配置项目
#管理接口的用户名
admin-username = user
#管理接口的密码
admin-password = pwd
#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 127.0.0.1:3306
#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
#proxy-read-only-backend-addresses = 127.0.0.1:3305@1
#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
pwds = user1:+jKsgB3YAG8=, user2:GS+tr4TPgqc=
#设置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
配置mysql管理员和后端连接用户的密码加密
rep用户
[root@db03 /usr/local/mysql-proxy/bin]# ./encrypt 123456
/iZxz+0GRoA=
mha用户
[root@db03 /usr/local/mysql-proxy/bin]# ./encrypt mha
O2jBXONX098=
修改如下:
[root@db03 /usr/local/mysql-proxy/conf]# cat test.cnf
[mysql-proxy]
admin-username = user #atlas 用户
admin-password = pwd #atlas 密码
proxy-backend-addresses = 10.4.7.55:3306 #写操作数据库 一般为MHAVIP
proxy-read-only-backend-addresses = 10.4.7.52:3306,10.4.7.53:3306 #读操作数据库
pwds = rep:/iZxz+0GRoA=,mha:O2jBXONX098= #mysql数据库管理员,后端连接用户的用户名和密码(这里是测试不代表生产环境) 上面的加密密码就放在这里(注意:密码需要对应用户)
daemon = true
keepalive = true
event-threads = 8
log-level = message
log-path = /usr/local/mysql-proxy/log #log日志
sql-log=ON
proxy-address = 0.0.0.0:33060
admin-address = 0.0.0.0:2345
charset=utf8
- 启动Atlas
[root@db03 /usr/local/mysql-proxy/bin]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started
注意:Atlas可以通过不同配置文件,管理多个读写分离项目
比如:
/usr/local/mysql-proxy/bin/mysql-proxyd test start (test=test.conf的前缀)
/usr/local/mysql-proxy/bin/mysql-proxyd test1 start
查看进程
[root@db03 /usr/local/mysql-proxy/bin]# ps -ef |grep proxy
root 53814 1 0 15:52 ? 00:00:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root 53815 53814 0 15:52 ? 00:00:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root 53980 18787 0 15:57 pts/5 00:00:00 grep --color=auto proxy
- 读写验证
读操作验证:
[root@db02 ~]# mysql -umha -pmha -h 10.4.7.53 -P33060 #这里的端口是Atlas proxy-address 代理的端口 而不是mysql的端口
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 2
Server version: 5.0.81-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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.
db02 [(none)]>select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 52 |
+-------------+
1 row in set (0.00 sec)
db02 [(none)]>select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 53 |
+-------------+
1 row in set (0.00 sec)
读操作的sever_id轮询到52 53 证明读写分离 读操作成功
写操作验证:
db02 [(none)]>begin;select @@server_id;commit; #这是三条语句,分号隔开的。
Query OK, 0 rows affected (0.00 sec)
+-------------+
| @@server_id |
+-------------+
| 51 |
+-------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
- 生产用户要求 (Atlas+MHA+VIP+SENDREPORT+BINLOG)
需求:开发人员申请一个应用用户 app( select update insert) 密码123456,要通过10网段登录
1. 在主库中,创建用户
grant select ,update,insert on *.* to app@‘10.0.0.%‘ identified by ‘123456‘;
2. 在atlas中添加生产用户
/usr/local/mysql-proxy/bin/encrypt 123456 ---->制作加密密码
3. 改配置文件
vim test.cnf
pwds = rep:/iZxz+0GRoA=,mha:O2jBXONX098=,app:/iZxz+0GRoA=
/usr/local/mysql-proxy/bin/mysql-proxyd test restart
[root@db03 conf]# mysql -uapp -p123456 -h 10.4.7.53 -P 33060
1.1.2 Atlas基本管理
- Atlas基本管理
登录:
[root@db03 ~]# mysql -uuser -ppwd -h 10.4.7.53 -P2345
查看帮助:
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 |
+----------------------------+---------------------------------------------------------+
16 rows in set (0.00 sec)
查看后端节点:
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.4.7.55:3306 | up | rw |
| 2 | 10.4.7.52:3306 | up | ro |
| 3 | 10.4.7.53:3306 | up | ro |
+-------------+----------------+-------+------+
3 rows in set (0.00 sec)
- 临时关闭维护和开启
SET OFFLINE $backend_id | offline backend server, $backend_id is backend_ndx‘s id |
| SET ONLINE $backend_id | online backend server, ...
例如:需要临时维护一下53 ,就可以临时关闭53,维护好之后在开启
db03 [(none)]>set offline 3; #3是backend_ndx 索引号
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 3 | 10.4.7.53:3306 | offline | ro |
+-------------+----------------+---------+------+
1 row in set (0.00 sec)
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 1 | 10.4.7.55:3306 | up | rw |
| 2 | 10.4.7.52:3306 | up | ro |
| 3 | 10.4.7.53:3306 | offline | ro |
+-------------+----------------+---------+------+
3 rows in set (0.00 sec)
维护完成开启
db03 [(none)]>set online 3;
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 3 | 10.4.7.53:3306 | unknown | ro |
+-------------+----------------+---------+------+
1 row in set (0.00 sec)
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.4.7.55:3306 | up | rw |
| 2 | 10.4.7.52:3306 | up | ro |
| 3 | 10.4.7.53:3306 | up | ro |
+-------------+----------------+-------+------+
- 动态删除和添加从库(读库)
删除:
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.4.7.55:3306 | up | rw |
| 2 | 10.4.7.52:3306 | up | ro |
| 3 | 10.4.7.53:3306 | up | ro |
+-------------+----------------+-------+------+
3 rows in set (0.00 sec)
db03 [(none)]>remove backend 3;
Empty set (0.00 sec)
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.4.7.55:3306 | up | rw |
| 2 | 10.4.7.52:3306 | up | ro |
+-------------+----------------+-------+------+
2 rows in set (0.00 sec)
保存:
db03 [(none)]>SAVE CONFIG; 保存到配置文件
Empty set (0.00 sec)
查看配置文件:
[root@db03 /usr/local/mysql-proxy/bin]# grep -i read-only /usr/local/mysql-proxy/conf/test.cnf
proxy-read-only-backend-addresses=10.4.7.52:3306
#配置文件里只剩下52了
添加从库(读库)
db03 [(none)]>add slave 10.4.7.53:3306;
Empty set (0.00 sec)
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.4.7.55:3306 | up | rw |
| 2 | 10.4.7.52:3306 | up | ro |
| 3 | 10.4.7.53:3306 | up | ro |
+-------------+----------------+-------+------+
3 rows in set (0.00 sec)
保存到配置文件:
db03 [(none)]>SAVE CONFIG;
Empty set (0.00 sec)
查看配置文件:
[root@db03 /usr/local/mysql-proxy/bin]# grep -i read-only /usr/local/mysql-proxy/conf/test.cnf
proxy-read-only-backend-addresses=10.4.7.52:3306,10.4.7.53:3306
#53添加成功
#注意最后一定要保存
- 动态删除和添加用户
删除用户:
删除rep用户:
db03 [(none)]>remove pwd rep;
Empty set (0.00 sec)
保存:
db03 [(none)]>save config;
Empty set (0.00 sec)
查看配置文件:
[root@db03 /usr/local/mysql-proxy/bin]# grep -i pwds /usr/local/mysql-proxy/conf/test.cnf
pwds=mha:O2jBXONX098=
#rep 用户被删除了
添加用户:
db03 [(none)]>add pwd rep:123456; #add pwd 用户:密码
Empty set (0.00 sec)
保存:
db03 [(none)]>save config;
Empty set (0.00 sec)
查看配置文件:
[root@db03 /usr/local/mysql-proxy/bin]# grep -i pwds /usr/local/mysql-proxy/conf/test.cnf
pwds=mha:O2jBXONX098=,rep:/iZxz+0GRoA=
#添加成功
db03 [(none)]>SELECT * FROM pwds ;
+----------+--------------+
| username | password |
+----------+--------------+
| mha | O2jBXONX098= |
| rep | /iZxz+0GRoA= |
+----------+--------------+
2 rows in set (0.00 sec)