mysqljava 设计MySQL实战干货

MySQL-Atlas实现读写分离

2019-08-22  本文已影响0人  文娟_狼剩

1、Atlas实现读写分离

1.1 Atlas介绍

摘自oldguo
Atlas是由 Qihoo 360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目。
它是在mysql-proxy 0.8.2版本的基础上,对其进行了优化,增加了一些新的功能特性。
360内部使用Atlas运行的mysql业务,每天承载的读写请求数达几十亿条。
下载地址
https://github.com/Qihoo360/Atlas/releases
注意:
1、Atlas只能安装运行在64位的系统上
2、Centos 5.X安装 Atlas-XX.el5.x86_64.rpm,Centos 6.X安装Atlas-XX.el6.x86_64.rpm。
3、后端mysql版本应大于5.1,建议使用Mysql 5.6以上

1.1 安装ATLAS

[root@db03 ~]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm 

1.2 配置

cd /usr/local/mysql-proxy/conf
mv test.cnf test.cnf.bak

cat > test.cnf <<EOF
[mysql-proxy]
admin-username = user
admin-password = pwd
proxy-backend-addresses = 10.0.0.55:3306
proxy-read-only-backend-addresses = 10.0.0.52:3306,10.0.0.53:3306
pwds = repl:3yb5jEku5h4=,mha:O2jBXONX098=
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
EOF

1.3 启动atlas

/usr/local/mysql-proxy/bin/mysql-proxyd test start
ps -ef |grep proxy

1.4 测试读写分离

[root@db03 ~]# mysql -umha -pmha -h 10.0.0.53 -P 33060
读: 
db03 [(none)]>select @@server_id;
写:
db03 [(none)]>begin;select @@server_id; commit;

1.5 生产授权用户

例子: 创建用户app,密码123

(1)业务主库中

db01 [(none)]>grant all on *.* to root@'10.0.0.%' identified by '123';

(2)将密码加密处理

cd /usr/local/mysql-proxy/bin
[root@m01 /usr/local/mysql-proxy/bin]# ./encrypt 123
3yb5jEku5h4=

(3)修改配置文件

vim /usr/local/mysql-proxy/conf/test.cnf 
pwds = repl:3yb5jEku5h4=,mha:O2jBXONX098=,root:3yb5jEku5h4=

(4)重启atlas

/usr/local/mysql-proxy/bin/mysql-proxyd test restart

(5)用户测试

[root@db03 ~]# mysql -uroot -p123 -h 10.0.0.61 -P 33060

1.6 atlas基础管理

[root@db03 ~]# mysql -uuser -ppwd -h 10.0.0.61 -P 2345
[root@db03 ~]# mysql -uuser -ppwd -h 10.0.0.53 -P 2345

db03 [(none)]>select * from help;
1.6.1 打印帮助
db03 [(none)]> SELECT * FROM help;
1.6.2 查看节点信息
db03 [(none)]> SELECT * FROM backends ;
1.6.3 上线和下线节点
db03 [(none)]> SET OFFLINE $backend_id;
db03 [(none)]> SET ONLINE $backend_id;
1.6.4 删除和添加节点
db03 [(none)]> REMOVE BACKEND 3;
db03 [(none)]> ADD SLAVE 10.0.0.53:3306;
1.6.5 添加用户和删除用户
db03 [(none)]> SELECT * FROM pwds;
db03 [(none)]> REMOVE PWD root;
db03 [(none)]> ADD PWD root:123
db03 [(none)]> ADD ENPWD root:3yb5jEku5h4=
1.6.6 持久化配置
db03 [(none)]> SAVE CONFIG 
上一篇下一篇

猜你喜欢

热点阅读