MySQL读写分离(十四)

第1章 MySQL中间件Atlas

1.1 Atlas简介

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

1.2 Atlas主要功能

  1. 读写分离
  2. 从库负载均衡
  3. IP过滤
  4. 自动分表
  5. DBA可平滑上下线数据库
  6. 自动摘除宕机的数据库

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

  1. 将主流程中所有Lua代码用C重写,Lua仅用于管理接口
  2. 重写网络模型、线程模型
  3. 实现了真正意义上的连接池
  4. 优化了锁机制,性能提高数十倍

1.4 安装部署Atlas

1.4.1 获取Atlas

下载地址:https://github.com/Qihoo360/Atlas/releases

1.4.2 安装Atlas

[root@db01 ~]# cd /server/tools/
[root@db01 tools]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm

1.4.3 编辑配置文件

[root@db01 ~]# /usr/local/mysql-proxy/bin/encrypt 123456
/iZxz+0GRoA=
[root@db01 ~]# vim /usr/local/mysql-proxy/conf/test.cnf
#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 10.0.0.41:3306

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

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

#SQL日志的开关,可设置为OFF、ON、REALTIME,OFF代表不记录SQL日志,ON代表记录SQL日志,REALTIME>代表记录SQL日志且实时写入磁盘,默认为OFF
sql-log = ON

1.4.4 启动Atlas

[root@db01 ~]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started

1.4.5 Atlas管理操作

# 使用管理用户登录
[root@db01 ~]# mysql -uroot -p123456 -h10.0.0.41 -P2345
# 查看帮助信息
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 | 10.0.0.41:3306 | up    | rw   |
|           2 | 10.0.0.42:3306 | up    | ro   |
|           3 | 10.0.0.43:3306 | up    | ro   |
+-------------+----------------+-------+------+
3 rows in set (0.01 sec)
# 平滑摘除MySQL
mysql> REMOVE BACKEND 2;
Empty set (0.00 sec)
# 查看摘除结果
mysql> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address        | state | type |
+-------------+----------------+-------+------+
|           1 | 10.0.0.41:3306 | up    | rw   |
|           2 | 10.0.0.43:3306 | up    | ro   |
+-------------+----------------+-------+------+
2 rows in set (0.01 sec)
# 平滑添加从库
mysql> ADD SLAVE 10.0.0.42:3306;
Empty set (0.00 sec)
# 查看添加结果
mysql> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address        | state | type |
+-------------+----------------+-------+------+
|           1 | 10.0.0.41:3306 | up    | rw   |
|           2 | 10.0.0.43:3306 | up    | ro   |
|           3 | 10.0.0.42:3306 | up    | ro   |
+-------------+----------------+-------+------+
3 rows in set (0.00 sec)
# 将结果保存到配置文件
mysql> SAVE CONFIG;
Empty set (0.11 sec)

 

温馨提示:本文最后更新于2022-12-20 20:57:41,已超过431天没有更新。某些文章具有时效性,若文章内容或图片资源有错误或已失效,请联系站长。谢谢!
转载请注明本文链接:https://blog.leonshadow.cn/763482/1942.html
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享