MHA-Atlas实现读写分离
安装
[root@db3 software]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
[root@db3 software]# cp /usr/local/mysql-proxy/conf/test.cnf /usr/local/mysql-proxy/conf/test.cnf.bak
[root@db3 software]# vim /usr/local/mysql-proxy/conf/test.cnf
[mysql-proxy]
admin-username = user
admin-password = pwd
proxy-backend-addresses = 192.168.31.190:3306
proxy-read-only-backend-addresses = 192.168.31.180:3306,192.168.31.181:3306
pwds = user1:+jKsgB3YAG8=, user2:GS+tr4TPgqc=
daemon = true
keepalive = true
event-threads = 8
log-level = message
log-path = /usr/local/mysql-proxy/log
sql-log = ON
proxy-address = 0.0.0.0:33060
admin-address = 0.0.0.0:2345
charset=utf8
# 启动Atlas
[root@db3 software]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started
# 查看端口
[root@db3 software]# ps -ef | grep proxy
[root@db3 software]# netstat -tnlp | grep proxy
tcp 0 0 0.0.0.0:2345 0.0.0.0:* LISTEN 3784/mysql-proxy
tcp 0 0 0.0.0.0:33060 0.0.0.0:* LISTEN 3784/mysql-proxy
生产用户要求
开发人员申请一个应用用户app(select update insert) 密码123456,要通过192.168.31.%网段登录
# 1.在主库中,创建用户
grant select,update,insert on *.* to app@'192.168.31.%' identified by '123456';
# 2.在Atlas中添加用户
[root@db3 ~]# /usr/local/mysql-proxy/bin/encrypt 123456
/iZxz+0GRoA=
[root@db3 ~]# vim /usr/local/mysql-proxy/conf/test.cnf
pwds = app:/iZxz+0GRoA=
# 3.重启服务
[root@db3 ~]# /usr/local/mysql-proxy/bin/mysql-proxyd test restart
OK: MySQL-Proxy of test is stopped
OK: MySQL-Proxy of test is started
# 4.连接测试
[root@db3 ~]# mysql -uapp -p123456 -h 192.168.31.182 -P 33060
db3 [(none)]>select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 52 |
+-------------+
1 row in set (0.00 sec)
db3 [(none)]>select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 51 |
+-------------+
Atlas基本管理
在线修改Atlas,不用修改配置文件重启
# 连接管理接口
[root@db3 ~]# mysql -uuser -ppwd -h127.0.0.1 -P2345
# 查看所有管理命令
db3 [(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)
# 查看后端所有节点的情况
db3 [(none)]>select * from backends;
+-------------+---------------------+-------+------+
| backend_ndx | address | state | type |
+-------------+---------------------+-------+------+
| 1 | 192.168.31.190:3306 | up | rw |
| 2 | 192.168.31.180:3306 | up | ro |
| 3 | 192.168.31.181:3306 | up | ro |
+-------------+---------------------+-------+------+
3 rows in set (0.00 sec)
# 下线3号节点
db3 [(none)]>set offline 3;
# 动态删除节点
db3 [(none)]>remove backend 3;
# 动态添加节点
db3 [(none)]>add slave 192.168.31.181:3306
# 保存配置到配置文件
db3 [(none)]>save config;