基于Atlas实现mysql读写分离
Posted 小侠猪猪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Atlas实现mysql读写分离相关的知识,希望对你有一定的参考价值。
一、实验环境
主机名
IP地址
master
192.168.200.111
192.168.200.111
slave
192.168.200.112
192.168.200.112
atlas
192.168.200.113
192.168.200.113
主从复制不再赘述,链接地址:
https://blog.csdn.net/weixin_42480196/article/details/102565189https://blog.csdn.net/weixin_42480196/article/details/102565189
授权Atlas登录用户
grant all on *.* to admin@‘192.168.200.113‘ identified by ‘123‘;
flush privileges;
二、安装atlas
#下载rpm包
wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm
#安装
rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
https://blog.csdn.net/weixin_42480196/article/details/102565189https://blog.csdn.net/weixin_42480196/article/details/102565189
授权Atlas登录用户
grant all on *.* to admin@‘192.168.200.113‘ identified by ‘123‘;
flush privileges;
二、安装atlas
#下载rpm包
wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm
#安装
rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
三、配置读写分离
加密密码
配置配置文件,实现读写分离
vim /usr/local/mysql-proxy/conf/test.cnf
[mysql-proxy]
[mysql-proxy]
#带#号的为非必需的配置项目
#管理接口的用户名
admin-username = user
admin-username = user
#管理接口的密码
admin-password = pwd
admin-password = pwd
#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 192.168.200.111:3306
proxy-backend-addresses = 192.168.200.111:3306
#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
proxy-read-only-backend-addresses = 192.168.200.112:3306@1
proxy-read-only-backend-addresses = 192.168.200.112:3306@1
#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
pwds = admin:3yb5jEku5h4=
pwds = admin:3yb5jEku5h4=
#设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。
daemon = true
daemon = true
#设置Atlas的运行方式,设为true时Atlas会启动两个进程,一个为monitor,一个为worker,monitor在worker意外退出后会自动将其重启,设为false时只有worker,没有monitor,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。
keepalive = true
keepalive = true
#工作线程数,对Atlas的性能有很大影响,可根据情况适当设置
event-threads = 8
event-threads = 8
#日志级别,分为message、warning、critical、error、debug五个级别
log-level = message
log-level = message
#日志存放的路径
log-path = /usr/local/mysql-proxy/log
log-path = /usr/local/mysql-proxy/log
#SQL日志的开关,可设置为OFF、ON、REALTIME,OFF代表不记录SQL日志,ON代表记录SQL日志,REALTIME代表记录SQL日志且实时写入磁盘,默认为OFF
sql-log = ON
sql-log = ON
#慢日志输出设置。当设置了该参数时,则日志只输出执行时间超过sql-log-slow(单位:ms)的日志记录。不设置该参数则输出全部日志。
#sql-log-slow = 10
#sql-log-slow = 10
#实例名称,用于同一台机器上多个Atlas实例间的区分
#instance = test
#instance = test
#Atlas监听的工作接口IP和端口
proxy-address = 0.0.0.0:1234
proxy-address = 0.0.0.0:1234
#Atlas监听的管理接口IP和端口
admin-address = 0.0.0.0:2345
admin-address = 0.0.0.0:2345
#分表设置,此例中person为库名,mt为表名,id为分表字段,3为子表数量,可设置多项,以逗号分隔,若不分表则不需要设置该项
#tables = person.mt.id.3
#tables = person.mt.id.3
#默认字符集,设置该项后客户端不再需要执行SET NAMES语句
charset = utf8
charset = utf8
#允许连接Atlas的客户端的IP,可以是精确IP,也可以是IP段,以逗号分隔,若不设置该项则允许所有IP连接,否则只允许列表中的IP连接
#client-ips = 127.0.0.1, 192.168.1
#client-ips = 127.0.0.1, 192.168.1
#Atlas前面挂接的LVS的物理网卡的IP(注意不是虚IP),若有LVS且设置了client-ips则此项必须设置,否则可以不设置
#lvs-ips = 192.168.1.1
四、启动Atlas服务
/usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started #启动成功
#查看端口
netstat -lnupt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:2345 0.0.0.0:* LISTEN 2156/mysql-proxy
tcp 0 0 0.0.0.0:1234 0.0.0.0:* LISTEN 2156/mysql-proxy
五、登录测试
mysql -uuser -ppwd -h127.0.0.1 -P2345
select * from backends;
+-------------+----------------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------------+-------+------+
| 1 | 192.168.200.111:3306 | up | rw |
| 2 | 192.168.200.112:3306 | up | ro |
+-------------+----------------------+-------+------+
#lvs-ips = 192.168.1.1
四、启动Atlas服务
/usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started #启动成功
#查看端口
netstat -lnupt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:2345 0.0.0.0:* LISTEN 2156/mysql-proxy
tcp 0 0 0.0.0.0:1234 0.0.0.0:* LISTEN 2156/mysql-proxy
五、登录测试
mysql -uuser -ppwd -h127.0.0.1 -P2345
select * from backends;
+-------------+----------------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------------+-------+------+
| 1 | 192.168.200.111:3306 | up | rw |
| 2 | 192.168.200.112:3306 | up | ro |
+-------------+----------------------+-------+------+
以上是关于基于Atlas实现mysql读写分离的主要内容,如果未能解决你的问题,请参考以下文章
专职DBA-基于MHA高可用搭建MySQL读写分离架构-Atlas