Atlas是由奇虎360软件有限公司(纽约证券交易所:QIHU)的Web平台部门的基础架构团队开发和维护的基于MySQL协议的数据库中间件项目。它在MySQL-Proxy 0.8.2的基础上修复了许多错误并添加了许多新功能。目前该项目已在QIHU中得到广泛应用,许多MySQL业务已连接到Atlas平台。Atlas转发的读写请 求数量已达数十亿。

目录

Mysql读写分离-Atlas

2.主要功能

1.读取/写入拆分。
2.负载平衡和故障转移处理。
3.IP过滤。
4.数据分片
5.DBA可以使后端数据库服务器顺利地联机或脱机。
6.自动删除故障数据库服务器。
7.Config文件重新加载而无需停机。

3. Atlas与Mysql-proxy相比的改进

1.用C重写所有lua代码,Lua仅用于管理接口。
2.重写网络模型和线程模型。
3.安装连接池。
4.优化锁紧机构,性能大大提高。

角色 系统 服务 IP地址
master centos7 mysql5.7 192.168.100.10
slave centos7 mysql5.7 192.168.100.20
slave centos7 mysql5.7 192.168.100.30
proxy centos7 Atlas 192.168.100.40

搭建主从

1.安装mysql5.7并配置主从
请参考Mysql主从复制

2.主从创建proxy用户
在主库操作即可,从库自动同步

1
2
> grant all on *.* to 'proxy'@'%' identified by '123456.Com';
> flush privilegse;

安装Atlas

3.下载rpm

1
$ wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm

4.安装rpm

1
$ rpm -ivh Atlas-2.2.1.el6.x86_64.rpm

配置Atlas

5.加密proxy用户密码

1
2
$ /usr/local/mysql-proxy/bin/encrypt 123456.Com
TPDI43nv/SzgSv1GServJg==

6.修改Altas配置文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$ vim /usr/local/mysql-proxy/conf/test.cnf
[mysql-proxy]

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

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

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

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

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

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

7.启动Atlas

1
$ /usr/local/mysql-proxy/bin/mysql-proxyd test start 

8.安装mysql(为了使用mysql命令)

1
$ yum install mysql-server mysql -y #安装后不可启动

9.登管理接口查看,使用管理接口2345 管理账户 user:pwd

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ mysql -h127.0.0.1 -P2345 -uuser -ppwd
> 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                            |
+----------------------------+---------------------------------------------------------+

10.登录工作接口查看,使用工作接口1234 工作账户 proxy: 123456.Com

1
2
3
4
5
6
7
8
$ mysql -h127.0.0.1 -P1234 -uproxy -p123456.Com
> select * from test.student;
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  1 | 张三   |  20 ||
|  2 | 李四   |  19 ||
+----+--------+-----+-----+

验证读写分离

11.在master,salve机器上,用下面命令查询访问量

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
192.168.100.10:

> show  global  status like 'Question%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Questions     | 100   |
+---------------+-------+

192.168.100.20:

> show  global  status like 'Question%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Questions     | 100   |
+---------------+-------+

192.168.100.30:

> show  global  status like 'Question%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Questions     | 100   |
+---------------+-------+

每执行一次访问量增加1,确保两个从库访问量相同

12.登录工作接口
查询两次student表,然后查看访问量

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
192.168.100.10:

> show  global  status like 'Question%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Questions     | 100   |
+---------------+-------+

192.168.100.20:

> show  global  status like 'Question%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Questions     | 102   |
+---------------+-------+

192.168.100.30:

> show  global  status like 'Question%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Questions     | 102   |
+---------------+-------+

从库值访问量各增加1,读取负载成功

student表添加两条数据,然后查看访量

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
192.168.100.10:

> show  global  status like 'Question%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Questions     | 103   |
+---------------+-------+

192.168.100.20:

> show  global  status like 'Question%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Questions     | 102   |
+---------------+-------+

192.168.100.30:

> show  global  status like 'Question%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Questions     | 102   |
+---------------+-------+

主库访问量增加2,读写分离成功