0
点赞
收藏
分享

微信扫一扫

ProxySQL搭建MySQL数据库的读写分离

dsysama 2022-03-27 阅读 193

ProxySQL是一个对应用无感知的数据库读写分离中间件,这种最大的好处就是对应用系统简单,对基础架构实现了分层。

在我司ProxySQL承担了在活动期间数据库的扩容,在使用的过程中也出现过小插曲。

环境说明:

角色

IP

端口

master

192.168.0.200

3306

slave

192.168.0.201

3306

slave

192.168.0.202

3306


一 ProxySQL搭建

这里我在使用docker来进行搭建ProxySQL,

proxysql.cnf  配置文件

#file proxysql.cfg


datadir="/var/lib/proxysql"
errorlog="/var/lib/proxysql/proxysql.log"

admin_variables=
{
#管理proxysql的账号密码:前面是账号,后面是密码
#管理proxysql的账号如果为admin,则只允许从本地访问,不允许远程访问,所以这里改用proxysql
admin_credentials="proxysql:123456"
mysql_ifaces="0.0.0.0:6032"
# refresh_interval=2000
# debug=true
}

mysql_variables=
{
threads=4
max_connections=5000
default_query_delay=0
default_query_timeout=36000000
have_compress=true
poll_timeout=2000
interfaces="0.0.0.0:6033"
default_schema="information_schema"
stacksize=1048576
server_version="5.5.30"
connect_timeout_server=3000
monitor_username="proxysql" #监控数据库的账号,需要在主数据库创建并且授权
monitor_password="proxysql" #监控数据库的密码
monitor_history=600000
monitor_connect_interval=60000
monitor_ping_interval=10000
monitor_read_only_interval=1500
monitor_read_only_timeout=500
ping_interval_server_msec=120000
ping_timeout_server=500
commands_stats=true
sessions_sort=true
connect_retries_on_failure=10
}


# defines all the MySQL servers
#服务器组
#定义了2个服务器组,一个是0组(master用于数据库写入),一个是1组(slave 用于数据库查询)
#为啥需要把192.168.0.200也放入1组呢,就是防止出现数据库从库全部延迟的情况下没有查询的服务器
#max_replication_lag 这个参数是数据库主从延迟达到这个阈值,自动从查询组中剔除
mysql_servers =
(
{ address="192.168.0.200" , port=3306 , hostgroup=0, weight=1, status = "ONLINE", max_connections=2000 },
{ address="192.168.0.200" , port=3306 , hostgroup=1, weight=1, status = "ONLINE", max_connections=2000 },
{ address="192.168.0.201" , port=3306 , hostgroup=1, weight=20, status = "ONLINE", max_connections=2000, max_replication_lag=5 },
{ address="192.168.0.202" , port=3306 , hostgroup=1, weight=20, status = "ONLINE", max_connections=2000, max_replication_lag=5 },
)


# defines all the MySQL users
# 定义了MySQL连接的用户,这里需要在数据库中已经存在的用户以及密码
mysql_users:
(
{ username = "root" , password = "123456" , default_hostgroup = 0 , active = 1 },
{ username = "user1" , password = "123456" , default_hostgroup = 0 , active = 1 },
)

#defines MySQL Query Rules
#定义了查询策略
#写的SQL路由到hostgroup 0
#查询的SQL路由到hostgroup 1
mysql_query_rules:
(
{
rule_id=1
active=1
match_pattern="^SELECT .* FOR UPDATE$"
destination_hostgroup=0
apply=1
},
{
rule_id=2
active=1
match_pattern="^SELECT"
destination_hostgroup=1
apply=1
}
)
scheduler=
(
)
mysql_replication_hostgroups=
(
)

二 监控账号的权限

在上面的配置文件中,我们需要创建一个用户名为proxysql密码为proxysql的数据库账号用于监控

GRANT SUPER, REPLICATION SLAVE ON *.* TO 'proxysql'@'%' IDENTIFIED BY PASSWORD 'proxysql';

三 启动服务

docker run -d -p 6032:6032 -p 6033:6033 -v $(pwd)/proxysql.cnf:/etc/proxysql.cnf proxysql/proxysql

四 注意点

1.如果需要临时添加读库怎么办?

mysql -h127.0.0.1 -P6032 -uproxysql -p123456 -e 'insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag) values(1,"192.168.0.203",3306,20,2000,5);'
mysql -h127.0.0.1 -P6032 -uproxysql -p123456 -e 'load mysql servers to runtime;'
mysql -h127.0.0.1 -P6032 -uproxysql -p123456 -e 'save mysql servers to disk;'

2.max_replication_lag的坑

当主从同步延迟较大,如果没有配置max_replication_lag,这个时候依然会从从库查询数据,就出现数据不一致了,用户刚刚下的单查询不到。这个是时候需要配置max_replication_lag。一般情况下不宜过大,但是也不能较小比如设置为1,那主库的查询压力较大。

举报

相关推荐

0 条评论