ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2021-06-02 19:02:32  阅读:244  来源: 互联网

标签:stats 中间件 admin 0.0 proxysql ProxySQL mysql


 

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

 

 

mysql实现读写分离的方式

mysql 实现读写分离的方式有以下几种:

  • 程序修改mysql操作,直接和数据库通信,简单快捷的读写分离和随机的方式实现的负载均衡,权限独立分配,需要开发人员协助。
  • amoeba,直接实现读写分离和负载均衡,不用修改代码,有很灵活的数据解决方案,自己分配账户,和后端数据库权限管理独立,权限处理不够灵活。
  • mysql-proxy,直接实现读写分离和负载均衡,不用修改代码,master和slave用一样的帐号,效率低
  • mycat中间件
  • proxysql中间件(推荐使用)

ProxySQL简介

ProxySQL 是一款可以实际用于生产环境的 MySQL 中间件,它有官方版和 percona 版两种。percona版是在官方版的基础上修改的,添加了几个比较实用的工具。生产环境建议用官方版。

ProxySQL 是用 C++ 语言开发的,虽然也是一个轻量级产品,但性能很好(据测试,能处理千亿级的数据),功能也足够,能满足中间件所需的绝大多数功能,包括:

  • 最基本的读/写分离,且方式有多种
  • 可定制基于用户、基于schema、基于语句的规则对SQL语句进行路由。换句话说,规则很灵活。基于schema和与语句级的规则,可以实现简单的sharding(分库分表)
  • 可缓存查询结果。虽然ProxySQL的缓存策略比较简陋,但实现了基本的缓存功能,绝大多数时候也够用了。此外,作者已经打算实现更丰富的缓存策略
  • 监控后端节点。ProxySQL可以监控后端节点的多个指标,包括:ProxySQL和后端的心跳信息,后端节点的read-only/read-write,slave和master的数据同步延迟性(replication lag)

 

 

 

ProxySQL安装

[root@proxysql ~]# yum install -y http://repo.proxysql.com/ProxySQL/proxysql-2.1.x/centos/8/proxysql-2.1.1-1-centos8.x86_64.rpm




[root@proxysql ~]# systemctl enable --now proxysql
[root@proxysql ~]# ss -antl
State     Recv-Q     Send-Q          Local Address:Port         Peer Address:Port     
LISTEN    0          128                   0.0.0.0:6032              0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:6033              0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:6033              0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:6033              0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:6033              0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:22                0.0.0.0:*        
LISTEN    0          128                      [::]:22                   [::]:*        

 

 

 

ProxySQL的Admin管理接口

当 ProxySQL 启动后,将监听两个端口:

  • admin管理接口,默认端口为6032。该端口用于查看、配置ProxySQL
  • 接收SQL语句的接口,默认端口为6033,这个接口类似于MySQL的3306端口

 

 

 

 

ProxySQL 的 admin 管理接口是一个使用 MySQL 协议的接口,所以,可以直接使用 mysql 客户端、navicat 等工具去连接这个管理接口,其默认的用户名和密码均为 admin

例如,使用 mysql 客户端去连接 ProxySQL 的管理接口:

 

//先装一个mysql命令
[root@proxysql ~]# yum install -y mariadb

//链接 ProxySQL 的管理接口
[root@proxysql ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+-----+---------------+-------------------------------------+
| seq | name          | file                                |
+-----+---------------+-------------------------------------+
| 0   | main          |                                     |
| 2   | disk          | /var/lib/proxysql/proxysql.db       |
| 3   | stats         |                                     |
| 4   | monitor       |                                     |
| 5   | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
5 rows in set (0.000 sec)

 

 

由于 ProxySQL 的配置全部保存在几个自带的库中,所以通过管理接口,可以非常方便地通过发送一些SQL命令去修改 ProxySQL 的配置。 ProxySQL 会解析通过该接口发送的某些对ProxySQL 有效的特定命令,并将其合理转换后发送给内嵌的 SQLite3 数据库引擎去运行

ProxySQL 的配置几乎都是通过管理接口来操作的,通过 Admin 管理接口,可以在线修改几乎所有的配置并使其生效。只有两个变量的配置是必须重启 ProxySQL 才能生效的,它们是:
mysql-threads 和 mysql-stacksize

 

 

ProxySQL配置

 

和admin管理接口相关的变量

 

admin-admin_credentials

admin-admin_credentials 变量控制的是admin管理接口的管理员账户。默认的管理员账户和密码为admin:admin,但是这个默认的用户只能在本地使用。如果想要远程连接到ProxySQL,例如用windows上的navicat连接Linux上的ProxySQL管理接口,必须自定义一个管理员账户。

 

添加管理员帐户

[root@proxysql ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> select @@admin-admin_credentials;  //查看当前用户名和密码
+---------------------------+
| @@admin-admin_credentials |
+---------------------------+
| admin:admin               |
+---------------------------+
1 row in set (0.001 sec)

//设置管理员帐号myadmin,密码mei123

MySQL [(none)]> set admin-admin_credentials='admin:admin;myadmin:mei123';
Query OK, 1 row affected (0.000 sec)

MySQL [(none)]> select @@admin-admin_credentials;
+----------------------------+
| @@admin-admin_credentials  |
+----------------------------+
| admin:admin;myadmin:mei123 |
+----------------------------+
1 row in set (0.001 sec)



//在另一台机子远程连接

[root@localhost ~]# mysql -uadmin -padmin -h192.168.170.133 -P6032
ERROR 1040 (42000): User 'admin' can only connect locally

//用另一个账号连接
[root@localhost ~]# mysql -umyadmin -pmei123 -h192.168.170.133 -P6032
ERROR 1045 (28000): ProxySQL Error: Access denied for user 'myadmin'@'192.168.170.134' (using password: YES)



//因为他没有在本机生效,所以远程连接不生效。

MySQL [(none)]> load admin variables to runtime;     //使之立即生效
Query OK, 0 rows affected (0.001 sec)

MySQL [(none)]> save admin variables to disk;   //使修改永久保存到磁盘
Query OK, 43 rows affected (0.001 sec)



//修改后,就可以使用该用户名和密码连接管理接口

[root@localhost ~]# mysql -umyadmin -pmei123 -h192.168.170.133 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 

 

 

所有的配置操作都是在修改main库中对应的表

MySQL [(none)]> select * from global_variables where variable_name='admin-admin_credentials';
+-------------------------+----------------------------+
| variable_name           | variable_value             |
+-------------------------+----------------------------+
| admin-admin_credentials | admin:admin;myadmin:mei123 |
+-------------------------+----------------------------+
1 row in set (0.002 sec)

 

必须要区分admin管理接口的用户名和mysql_users中的用户名

  • admin管理接口的用户是连接到管理接口(默认端口6032)上用来管理、配置ProxySQL的
  • mysql_users表中的用户名是应用程序连接ProxySQL(默认端口6033),以及ProxySQL连接后端MySQL Servers使用的用户。它的作用是发送、路由SQL语句,类似于MySQL Server的3306端口。所以,这个表中的用户必须已经在后端MySQL Server上存在且授权了

admin管理接口的用户必须不能存在于mysql_users中,这是出于安全的考虑,防止通过admin管理接口用户猜出mysql_users中的用户

mysq_users中放的是后端服务器里面数据库的账户

 

 

 

admin-stats_credentials

admin-stats_credentials 变量控制admin管理接口的普通用户,这个变量中的用户没有超级管理员权限,只能查看monitor库和main库中关于统计的数据,其它库都是不可见的,且没有任何写权限

默认的普通用户名和密码均为 stats ,与admin一样,它默认也只能用于本地登录,若想让人远程查看则要添加查看的专有用户

 

MySQL [(none)]> select @@admin-stats_credentials;     //查看当前所有账户和密码
+---------------------------+
| @@admin-stats_credentials |
+---------------------------+
| stats:stats               |
+---------------------------+
1 row in set (0.001 sec)



//添加专有的查看用户
MySQL [(none)]> set admin-stats_credentials='stats:stats;mystats:mei123';
Query OK, 1 row affected (0.000 sec)

MySQL [(none)]> select @@admin-stats_credentials;
+----------------------------+
| @@admin-stats_credentials  |
+----------------------------+
| stats:stats;mystats:mei123 |
+----------------------------+
1 row in set (0.001 sec)


MySQL [(none)]> load admin variables to runtime; //使之立即生效
Query OK, 0 rows affected (0.001 sec)

MySQL [(none)]> save admin variables to disk;   //永久保存
Query OK, 43 rows affected (0.002 sec)

同样,这个变量中的用户必须不能存在于mysql_users表中

 

使用mystats用户远程连接

[root@localhost ~]# mysql -umystats -pmei123 -h192.168.170.133 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show tables;
+--------------------------------------+
| tables                               |
+--------------------------------------+
| global_variables                     |
| stats_memory_metrics                 |
| stats_mysql_commands_counters        |
| stats_mysql_connection_pool          |
| stats_mysql_connection_pool_reset    |
| stats_mysql_errors                   |
| stats_mysql_errors_reset             |
| stats_mysql_free_connections         |
| stats_mysql_global                   |
| stats_mysql_gtid_executed            |
| stats_mysql_prepared_statements_info |
| stats_mysql_processlist              |
| stats_mysql_query_digest             |
| stats_mysql_query_digest_reset       |
| stats_mysql_query_rules              |
| stats_mysql_users                    |
| stats_proxysql_servers_checksums     |
| stats_proxysql_servers_metrics       |
| stats_proxysql_servers_status        |
+--------------------------------------+
19 rows in set (0.001 sec)

 

 

 

admin-mysql_ifaces

admin-mysql_ifaces 变量指定admin接口的监听地址,格式为冒号分隔的hostname:port列表。默认监听在 0.0.0.0:6032

注意,允许使用UNIX的domain socket进行监听,这样本主机内的应用程序就可以直接被处理。
例如:

 

MySQL [(none)]> select @@admin-mysql_ifaces;  //查看当前监听端口
+----------------------+
| @@admin-mysql_ifaces |
+----------------------+
| 0.0.0.0:6032         |
+----------------------+
1 row in set (0.001 sec)

 

//修改监听端口为7777
MySQL [(none)]> SET admin-mysql_ifaces='0.0.0.0:7777';
Query OK, 1 row affected (0.000 sec)

MySQL [(none)]> select @@admin-mysql_ifaces;
+----------------------+
| @@admin-mysql_ifaces |
+----------------------+
| 0.0.0.0:7777         |
+----------------------+
1 row in set (0.001 sec)

//使之生效并保存
MySQL [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.001 sec)

MySQL [(none)]> save admin variables to disk;
Query OK, 43 rows affected (0.001 sec)
MySQL [(none)]> quit
Bye

[root@proxysql ~]# ss -antl
State     Recv-Q     Send-Q          Local Address:Port         Peer Address:Port     
LISTEN    0          128                   0.0.0.0:6033              0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:6033              0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:6033              0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:6033              0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:22                0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:7777              0.0.0.0:*        
LISTEN    0          128                      [::]:22                   [::]:*  

 

 

用远程连接测试

[root@localhost ~]# mysql -umystats -pmei123 -h192.168.170.133 -P7777
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 

 

 

 

多层配置系统

proxysql中的库

使用ProxySQL的Admin管理接口连上ProxySQL,可查看ProxySQL拥有的库

 

[root@proxysql ~]# mysql -uadmin -padmin -h127.0.0.1 -P7777
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+-----+---------------+-------------------------------------+
| seq | name          | file                                |
+-----+---------------+-------------------------------------+
| 0   | main          |                                     |
| 2   | disk          | /var/lib/proxysql/proxysql.db       |
| 3   | stats         |                                     |
| 4   | monitor       |                                     |
| 5   | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
5 rows in set (0.000 sec)

其中:

  • main库是ProxySQL最主要的库,是需要修改配置时使用的库,它其实是一个内存数据库系统。所以,修改main库中的配置后,必须将其持久化到disk上才能永久保存
  • disk库是磁盘数据库,该数据库结构和内存数据库完全一致。当持久化内存数据库中的配置时,其实就是写入到disk库中。磁盘数据库的默认路径为 $DATADIR/proxysql.db
  • stats库是统计信息库。这个库中的数据一般是在检索其内数据时临时填充的,它保存在内存中。因为没有相关的配置项,所以无需持久化
  • monitor库是监控后端MySQL节点相关的库,该库中只有几个log类的表,监控模块收集到的监控信息全都存放到对应的log表中
  • stats_history库是1.4.4版新增的库,用于存放历史统计数据。默认路径为 $DATADIR/proxysql_stats.db

ProxySQL 内部使用的是 SQLite3 数据库,无论是内存数据库还是磁盘数据库,都是通过SQLite3引 擎进行解析、操作的。它和 MySQL 的语法可能稍有不同,但ProxySQL会对不兼容的语法自动进行调整,最大程度上保证MySQL语句的有效率。
上面描述main库的时候,只是说了内存数据库需要持久化到disk库才能永久保存配置。但实际上,修改了main库中的配置后,并不会立即生效,它还需要load到runtime的数据结构中才生效,只有在runtime数据结构中的配置才是对ProxySQL当前有效的配置

 

 

 

 

 

 

修改6033端口为3306

//找到这一行并且将端口号改为3306,保存退出
[root@proxysql ~]# vim /etc/proxysql.cnf
#       interfaces="0.0.0.0:6033;/tmp/proxysql.sock"
        interfaces="0.0.0.0:3306;"

[root@proxysql ~]# pkill  proxysql 
[root@proxysql ~]# ps -ef|grep proxysql
root       3032   1543  0 02:40 pts/0    00:00:00 grep --color=auto proxysql
[root@proxysql ~]# systemctl restart proxysql
[root@proxysql ~]# ps -ef|grep proxysql
proxysql   3037      1  0 02:40 ?        00:00:00 /usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf
proxysql   3038   3037  6 02:40 ?        00:00:00 /usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf
root       3064   1543  0 02:40 pts/0    00:00:00 grep --color=auto proxysql
[root@proxysql ~]# ss -antl
State     Recv-Q     Send-Q          Local Address:Port         Peer Address:Port     
LISTEN    0          128                   0.0.0.0:6033              0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:6033              0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:6033              0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:6033              0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:22                0.0.0.0:*        
LISTEN    0          128                   0.0.0.0:7777              0.0.0.0:*        
LISTEN    0          128                      [::]:22                   [::]:*        

 

新下一个proxysql尝试

 

标签:stats,中间件,admin,0.0,proxysql,ProxySQL,mysql
来源: https://www.cnblogs.com/meijianbiao/p/14842492.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有