码迷,mamicode.com
首页 > 数据库 > 详细

mysql读写分离-proxysql

时间:2021-01-02 10:27:53      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:指定   dom   help   tab   replica   proxy   相关   套接字   column   

mysql读写分离方式

1.开发人员修改mysql操作,直接和数据库通信,实现简单快捷的读写分离和负载均衡,但是权则限独立分配。
2.amoeba,直接实现读写分离和负载均衡,不用修改代码,有很灵活的数据解决方案,自己分配账户,和后端数据库权限管理独立,权限处理不够灵活。
3.mysql-proxy,直接实现读写分离和负载均衡,不用修改代码,master和slave用一样的帐号,效率低
4.mycat中间件
5.proxysql中间件

proxysql

简介

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

ProxySQL 是用 C++ 语言开发的,能满足中间件所需的绝大多数功能功能:

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

proxysql安装

[root@vm3 ~]# vim /etc/yum.repos.d/proxysql.repo
[proxysql_repo]
name=ProxySQL
baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/7
gpgcheck=1
gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
[root@vm3 ~]# yum clean all 
[root@vm3 ~]# yum makecache
CentOS-8 - AppStream                            1.5 MB/s | 6.3 MB     00:04    
CentOS-8 - Base                                 1.0 MB/s | 2.3 MB     00:02    
CentOS-8 - Extras                               5.6 kB/s | 8.6 kB     00:01    
ProxySQL                                        2.8 kB/s | 4.4 kB     00:01    
Last metadata expiration chec

  • 安装proxysql
[root@vm3 ~]# yum -y install proxysql mariadb

proxysql侦听端口

[root@vm3 ~]# systemctl start proxysql
[root@vm3 ~]# 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                  [::]:*    

admin管理接口

默认端口为6032。该端口用于查看、配置ProxySQL,该接口使用MySQL协议所以,可以直接使用mysql客户端、navicat等工具去连接这个管理接口,其默认的用户名和密码均为 admin。

[root@vm3 ~]# 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.001 sec)

MySQL [(none)]> 

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

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

接收SQL语句的接口

  • 默认端口为6033,这个接口类似于MySQL的3306端口

admin管理接口的相关变量

admin-admin_credentials

该变量控制的是admin管理接口的管理员账户。默认的管理员账户和密码为admin:admin,但是这个默认的用户只能在本地使用。若需要远程连接则必须自定义一个管理员用户。

  • 添加管理员账户
##查看当前管理员账户信息
MySQL [(none)]> select @@admin-admin_credentials;
+---------------------------+
| @@admin-admin_credentials |
+---------------------------+
| admin:admin               |
+---------------------------+
1 row in set (0.003 sec)

##设置新的管理员账户
MySQL [(none)]> set admin-admin_credentials=‘admin:admin;wisan:wisan‘;
Query OK, 1 row affected (0.001 sec)

MySQL [(none)]> select @@admin-admin_credentials;
+---------------------------+
| @@admin-admin_credentials |
+---------------------------+
| admin:admin;wisan:wisan   |
+---------------------------+
1 row in set (0.003 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, 31 rows affected (0.006 sec)
  • 远程登录验证
[root@vm5 ~]# mysql -uadmin -padmin -h192.168.225.130 -P6032
ERROR 2002 (HY000): Can‘t connect to MySQL server on ‘192.168.225.130‘ (115)
[root@vm5 ~]# mysql -uwisan -pwisan -h192.168.225.130 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
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库中对应的表即位配置proxysql
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.002 sec)

MySQL [(none)]> use main;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL [main]> show tables;
+--------------------------------------------+
| tables                                     |
+--------------------------------------------+
| global_variables                           |
| mysql_collations                           |
| mysql_group_replication_hostgroups         |
| mysql_query_rules                          |
| mysql_query_rules_fast_routing             |
| mysql_replication_hostgroups               |
| mysql_servers                              |
| mysql_users                                |
| proxysql_servers                           |
| runtime_checksums_values                   |
| runtime_global_variables                   |
| runtime_mysql_group_replication_hostgroups |
| runtime_mysql_query_rules                  |
| runtime_mysql_query_rules_fast_routing     |
| runtime_mysql_replication_hostgroups       |
| runtime_mysql_servers                      |
| runtime_mysql_users                        |
| runtime_proxysql_servers                   |
| runtime_scheduler                          |
| scheduler                                  |
+--------------------------------------------+
20 rows 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中的用户

admin-stats_credentials

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

默认的普通用户名和密码均为stats,与admin一样,它默认也只能用于本地登录,若想让人远程查看则要添加查看的专有用户。同样,这个变量中的用户必须不能存在于mysql_users表中

  • 添加查看用户
MySQL [(none)]> select @@admin-stats_credentials;
+---------------------------+
| @@admin-stats_credentials |
+---------------------------+
| stats:stats               |
+---------------------------+
1 row in set (0.003 sec)

MySQL [(none)]> set admin-stats_credentials=‘stats:stats;mystats:mystats‘;
Query OK, 1 row affected (0.001 sec)

MySQL [(none)]> select @@admin-stats_credentials;
+---------------------------+
| @@admin-stats_credentials |
+---------------------------+
| stats:stats;mystats:mystats   |
+---------------------------+
1 row in set (0.002 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, 31 rows affected (0.005 sec)

MySQL [(none)]> 

  • 验证
[root@vm5 ~]# mysql -umystats -pmystats -h192.168.225.130 -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 databases;
+-----+---------------+-------------------------------------+
| seq | name          | file                                |
+-----+---------------+-------------------------------------+
| 0   | main          |                                     |
| 2   | monitor       |                                     |
| 3   | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
3 rows in set (0.001 sec)

MySQL [(none)]> show tables from main;
+--------------------------------------+
| tables                               |
+--------------------------------------+
| global_variables                     |
| stats_memory_metrics                 |
| stats_mysql_commands_counters        |
| stats_mysql_connection_pool          |
| stats_mysql_connection_pool_reset    |
| stats_mysql_global                   |
| 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        |
+--------------------------------------+
15 rows in set (0.002 sec)

MySQL [(none)]> 

admin-mysql_ifaces

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

  • 修改admin接口的监听端口为6666
MySQL [(none)]> select @@admin-mysql_ifaces;
+----------------------+
| @@admin-mysql_ifaces |
+----------------------+
| 0.0.0.0:6032         |
+----------------------+
1 row in set (0.002 sec)

MySQL [(none)]> set admin-mysql_ifaces=‘0.0.0.0:6666‘;
Query OK, 1 row affected (0.001 sec)

MySQL [(none)]> select @@admin-mysql_ifaces;
+----------------------+
| @@admin-mysql_ifaces |
+----------------------+
| 0.0.0.0:6666         |
+----------------------+
1 row in set (0.002 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, 31 rows affected (0.029 sec)

MySQL [(none)]> quit

  • 查看端口
[root@vm3 ~]# ss -antl 
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port    
LISTEN    0         128                0.0.0.0:6666             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                  [::]:*       
[root@vm3 ~]# 

同时还允许使用UNIX的domain socket进行监听,这样本主机内的应用程序就可以直接被处理。

  • 使用socket监听
MySQL [(none)]> set admin-mysql_ifaces=‘0.0.0.0:6666;/tmp/proxysql_admin.sock‘;
Query OK, 1 row affected (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, 31 rows affected (0.006 sec)

MySQL [(none)]> select @@admin-mysql_ifaces;
+---------------------------------------+
| @@admin-mysql_ifaces                  |
+---------------------------------------+
| 0.0.0.0:6666;/tmp/proxysql_admin.sock |
+---------------------------------------+
1 row in set (0.003 sec)

  • 验证
[root@vm3 ~]# cd /tmp/
[root@vm3 tmp]# ls
proxysql_admin.sock
[root@vm3 tmp]# ll
total 0
srwxrwxrwx. 1 root root 0 Dec 28 20:57 proxysql_admin.sock

##套接字登录
[root@vm3 tmp]# mysql -uadmin -padmin  -S proxysql_admin.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
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)]> 

mysql读写分离-proxysql

标签:指定   dom   help   tab   replica   proxy   相关   套接字   column   

原文地址:https://www.cnblogs.com/fyjpeng/p/14203617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!