前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何实现MySQL读写分离?

如何实现MySQL读写分离?

作者头像
吴柯
发布2018-04-16 14:52:38
1.5K0
发布2018-04-16 14:52:38
举报

一:部署mysql-proxy代理服务器

1)安装mariadb官方提供的maxscale软件包

# rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm # vim /etc/maxscale.cnf

  • [maxscale]
  • threads=1
  • [server1] #指定ip地址对应的名字
  • type=server
  • address=192.168.4.10 #主数据库服务器ip地址
  • port=3306
  • protocol=MySQLBackend
  • [server2] #指定ip地址对应的名字
  • type=server
  • address=192.168.4.20 #从数据库服务器ip地址
  • port=3306
  • protocol=MySQLBackend
  • [MySQL Monitor] #指定要监控的主机 和监控时连接的用户
  • type=monitor
  • module=mysqlmon
  • servers=server1, server2 #前边定义的主机名
  • user=scalemon # 用户名
  • passwd=111111 # 密码
  • monitor_interval=10000
  • #[Read-Only Service]
  • #type=service
  • #router=readconnroute
  • #servers=server1
  • #user=myuser
  • #passwd=mypwd
  • #router_options=slave
  • [Read-Write Service] #定义服务器列表
  • type=service
  • router=readwritesplit
  • servers=server1, server2 #前边定义的主机名
  • user=maxscale # 用户名
  • passwd=111111 # 密码
  • max_slave_connections=100%
  • [MaxAdmin Service]
  • type=service
  • router=cli
  • #[Read-Only Listener]
  • #type=listener
  • #service=Read-Only Service
  • #protocol=MySQLClient
  • #port=4008
  • [Read-Write Listener]
  • type=listener
  • service=Read-Write Service
  • protocol=MySQLClient
  • port=4006

1)分别在主、从数据库服务器上添加授权用户(只在主服务器授权即可 从服务器会自动同步):

mysql> grant replication slave, replication client on *.* to scalemon@'%' identified by “111111”; //创建监控用户

mysql> grant select on mysql.* to maxscale@'%' identified by “111111”; //创建路由用户

mysql> grant all on *.* to student@'%' identified by “111111”; //创建客户端访问用户

2)启动maxscale服务

# maxscale --config=/etc/maxscale.cnf # netstat -utnalp | grep maxscale # kill -9 进程id //通过杀进程的方式停止服务

二:测试配置

1)在客户端192.168.4.120上使用上边授权用户student 连接代理服务器192.168.4.100:

# mysql -h192.168.4.100 -P4006 -ustudent -p111111 MySQL [(none)]> select @@hostname; //显示当前访问的主机

MySQL [(none)]> insert into bbsdb.a values(111);//插入新纪录

2)在主从数据库分别查看

...

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-09-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 吴柯的运维笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档