MYSQL之读写分离搭建方案

  • 读写分离实现:360 Atlas(代理层实现,无需修改应用程序代码)

  • 实现步骤
# 安装
shell> rpm -i Atlas-2.2.1.el6.x86_64.rpm
# 卸载
shell> rpm -e Atlas-2.2.1.el6.x86_64.rpm
# 安装目录在 /usr/local/mysql-proxy/ 当中
# Atlass配置,在"/usr/local/mysql-proxy"下的conf目录中,配置test.cnf即可,以下为配置实例
[mysql-proxy]

#带#号的为非必需的配置项目

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

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

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

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

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

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

#慢日志输出设置。当设置了该参数时,则日志只输出执行时间超过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

#允许连接Atlas的客户端的IP,可以是精确IP,也可以是IP段,以逗号分隔,若不设置该项则允许所有IP连接,否则只允许列表中的IP连接
#client-ips = 127.0.0.1, 192.168.1

#Atlas前面挂接的LVS的物理网卡的IP(注意不是虚IP),若有LVS且设置了client-ips则此项必须设置,否则可以不设置
#lvs-ips = 192.168.1.1

# 启动与关闭Atlas
shell> ./mysql-proxyd test start
shell> ./mysql-proxyd test stop
# 以代理方式进行管理员登录
shell> mysql -h127.0.0.1 -P2345 -uroot -proot
# 在你设置的同步库中,新建表,查询/添加数据
# 查看test配置中的日志目录,查看目录下的sql_test.log文件
shell> tail -f sql_test.log
[08/20/2018 14:48:38] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.338 "select * from health_user LIMIT 0, 1000"
[08/20/2018 14:48:38] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 1.124 "SHOW STATUS"
[08/20/2018 14:48:38] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.207 "SHOW PROFILES"
[08/20/2018 14:48:38] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.410 "select state, round(sum(duration),5) as `duration (summed) in sec` from information_schema.profiling where query_id = 308 group by state order by `duration (summed) in sec` desc"
[08/20/2018 14:48:38] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.203 "SET PROFILING = 0"
[08/20/2018 14:48:38] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.393 "EXPLAIN select * from health_user LIMIT 0, 1000"
[08/20/2018 14:48:41] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.551 "SET PROFILING = 1"
[08/20/2018 14:48:41] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.264 "SET profiling_history_size = 0"
[08/20/2018 14:48:41] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.216 "SET profiling_history_size = 15"
[08/20/2018 14:48:41] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 1.077 "SHOW STATUS"
[08/20/2018 14:48:41] C:192.168.153.1:64678 S:127.0.0.1:3306 OK 0.653 "/*master*/select * from health_user LIMIT 0, 1000"
[08/20/2018 14:48:41] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 1.175 "SHOW STATUS"
[08/20/2018 14:48:41] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.190 "SHOW PROFILES"
[08/20/2018 14:48:41] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.348 "select state, round(sum(duration),5) as `duration (summed) in sec` from information_schema.profiling where query_id = 0 group by state order by `duration (summed) in sec` desc"
[08/20/2018 14:48:41] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.191 "SET PROFILING = 0"
[08/20/2018 14:48:41] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.446 "EXPLAIN /*master*/select * from health_user LIMIT 0, 1000"
[08/20/2018 14:55:55] C:192.168.153.1:64678 S:127.0.0.1:3306 OK 3.454 "insert into health_user values(6,'Gang')"
[08/20/2018 14:57:23] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.455 "SET PROFILING = 1"
[08/20/2018 14:57:23] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.303 "SET profiling_history_size = 0"
[08/20/2018 14:57:23] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 0.307 "SET profiling_history_size = 15"
[08/20/2018 14:57:23] C:192.168.153.1:64678 S:192.168.153.12:3306 OK 1.254 "SHOW STATUS"

# 可以看到,查询走了从库(192.168.153.12)且主从库数据一致([主从同步搭建](https://blog.csdn.net/qq724581322/article/details/81055994)),插入走了主库(127.0.0.1/192.168.153.11)
# 以上,Atlas搭建的读写分离成功!

你可能感兴趣的:(MYSQL)