ProxySQL实现mysql中间件读写分离

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ProxySQL实现mysql中间件读写分离相关的知识,希望对你有一定的参考价值。

1、初始化准备,四台设备

master:192.168.2.6
slave:192.168.2.16
proxysql:192.168.2.26
Client:192.168.2.100,用于测试

2、搭建好主从服务器

master:主
[[email protected] ~]# yum -y install mariadb-server
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server_id=6
binlog_format=row
log_bin
skip_name_resolve
[[email protected] ~]# systemctl start mariadb
MariaDB [(none)]> grant replication slave on *.* to [email protected]‘192.168.2.%‘ identified by ‘123456‘;      #创建复制账号

slave:从
[[email protected] ~]# yum -y install mariadb-server
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
read-only
server_id=16
skip_name_resolve
binlog_format=row
[[email protected] ~]# systemctl start mariadb
MariaDB [(none)]> CHANGE MASTER TO
    -> MASTER_HOST=‘192.168.2.6‘,
    ->   MASTER_USER=‘repluser‘,
    ->   MASTER_PASSWORD=‘123456‘,
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE=‘mariadb-bin.000001‘,
    ->   MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G

3、测试主从同步是否正常

master:
MariaDB [(none)]> create database db1;

slave:
MariaDB [(none)]> show databases;

4、搭建proxysql yum仓库

proxysql:
[[email protected] ~]# cat <<EOF | tee /etc/yum.repos.d/proxysql.repo
> [proxysql_repo]
> name= ProxySQL YUM repository
> baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/\$releasever
> gpgcheck=1
> gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
> EOF

5、安装proxysql和mariadb客户端工具

proxysql:       6032管理端口、  6033连接端口
[[email protected] ~]# yum -y install proxysql mariadb
[[email protected] ~]# rpm -ql proxysql     
[[email protected] ~]# service proxysql start      #老的启动方法
[[email protected] ~]# ll /var/lib/proxysql/       #proxysql简化版的关系型数据库,用来存放proxysql配置

6、连接proxysql,在main. mysql_servers 加入主从服务器记录

proxysql:
[[email protected] ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1     #账号密码默认admin
MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(10,‘192.168.2.6‘,3306);                 #组id,ip,端口
MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(10,‘192.168.2.16‘,3306);
MySQL [(none)]> select * from mysql_servers;    #查看添加的记录信息

7、主从服务器添加管理账户供proxysql机器来管理

master:
MariaDB [(none)]> grant replication client on *.* to [email protected]‘192.168.2.%‘ identified by ‘123456‘;      #主从复制已经建立,只要在主服务器添加即可

8、设置使用monitor用户连接主从服务器 #main.global_variables表

proxysql:
MySQL [(none)]>  set mysql-monitor_username=‘monitor‘;  
MySQL [(none)]>  set mysql-monitor_password=‘123456‘;
MySQL [(none)]>  load mysql variables to runtime;       #加载生效
MySQL [(none)]> save mysql variables to disk;           #保存到磁盘

9、设置分组信息 #需要修改的是main库中的mysql_replication_hostgroups表

proxysql:
MySQL [(none)]> insert into mysql_replication_hostgroups values(10,20,"test");      #10组负责写,20负责读
MySQL [(none)]> select * from mysql_replication_hostgroups;     #查看添加的记录
MySQL [(none)]>  load mysql servers to runtime;     #加载生效
MySQL [(none)]> save mysql servers to disk;     #保存到磁盘
MySQL [(none)]> select * from mysql_servers;        #再次查看记录,proxysql已经区分读写id了

10、在master节点创建访问用户,用于主从服务器去访问中间件proxysql #主从复制已经建立,只要在主服务器添加即可

master:
MariaDB [(none)]> grant all on *.* to [email protected]‘192.168.2.%‘ identified by ‘123456‘;

11、将创建访问调度器proxysql的客户端用户sqluser加入proxysql服务器的

mysql_users表中
proxysql:
MySQL [(none)]> insert into mysql_users(username,password,default_hostgroup) values(‘sqluser‘,‘123456‘,10);     #default_hostgroup默认
组设置为写组10,

MySQL [(none)]>  load mysql users to runtime;       #生效
MySQL [(none)]>  save mysql users to disk;      #存盘

12、使用客户端测试

Client:
[[email protected] ~]# mysql -usqluser -p123456 -h192.168.2.26 -P6033 -e ‘select @@server_id‘;

13、设置调度规则

proxysql:
MySQL [(none)]>  insert into mysql_query_rules
    -> (rule_id,active,match_digest,destination_hostgroup,apply)VALUES
    -> (1,1,‘^SELECT.*FOR UPDATE$‘,10,1),(2,1,‘^SELECT‘,20,1);
Query OK, 2 rows affected (0.00 sec)  #读调度到20组,写调度到10组 

MySQL [(none)]>  load mysql query rules to runtime;     #生效

MySQL [(none)]>  save mysql query rules to disk;        #保存

14、客户端测试

Client:
[[email protected] ~]# mysql -usqluser -p123456 -h192.168.2.26 -P6033 -e ‘select @@server_id‘;

以上是关于ProxySQL实现mysql中间件读写分离的主要内容,如果未能解决你的问题,请参考以下文章

MySQL中间件proxysql实现MySQL读写分离

MySQL中间件之ProxySQL:初试读写分离

Mysql中间件Proxysql实现mysql主从架构读写分离

Mysql使用ProxySQL实现读写分离

ProxySQL实现mysql中间件读写分离

Mysql使用ProxySQL实现读写分离