mysql 实现读写分离的方式有以下几种:
ProxySQL 是一款可以实际用于生产环境的 MySQL 中间件,它有官方版和 percona 版两种。percona版是在官方版的基础上修改的,添加了几个比较实用的工具。生产环境建议用官方版。
ProxySQL 是用 C++ 语言开发的,虽然也是一个轻量级产品,但性能很好(据测试,能处理千亿级的数据),功能也足够,能满足中间件所需的绝大多数功能,包括:
//配置yum源 //脚本如下 #!/bin/bash curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo sed -i -e ‘/mirrors.cloud.aliyuncs.com/d‘ -e ‘/mirrors.aliyuncs.com/d‘ /etc/yum.repos.d/CentOS-Base.repo sed -i ‘s#\$releasever#8#g‘ /etc/yum.repos.d/CentOS-Base.repo yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm sed -i ‘s|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|‘ /etc/yum.repos.d/epel* yum clean all && yum makecache
# yum -y install proxysql
当 ProxySQL 启动后,将监听两个端口:
ProxySQL 的 admin 管理接口是一个使用 MySQL 协议的接口,所以,可以直接使用 mysql 客户端、navicat 等工具去连接这个管理接口,其默认的用户名和密码均为 admin
例如,使用 mysql 客户端去连接 ProxySQL 的管理接口:
[root@test2 ~]# systemctl start proxysql [root@test2 ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* 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 [::]:22 [::]:*
//登录的时候,提示没有mysql这个命令,需要安装sql [root@test1 ~]# mysql -uadmin -padmin -h127.0.0.1 -p6032 -bash: mysql: 未找到命令 //安装 # yum -y install mariadb //登录到数据库 # mysql -uadmin -padmin -P6032 -h127.0.0.1 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
admin-admin_credentials 变量控制的是admin管理接口的管理员账户。默认的管理员账户和密码为admin:admin,但是这个默认的用户只能在本地使用。如果想要远程连接到ProxySQL,例如用windows上的navicat连接Linux上的ProxySQL管理接口,必须自定义一个管理员账户。
添加管理员帐户
//查看当前用户名和密码 MySQL [(none)]> select @@admin-admin_credentials; +---------------------------+ | @@admin-admin_credentials | +---------------------------+ | admin:admin | +---------------------------+ 1 row in set (0.001 sec) //设置管理员帐号erciyuan,密码123 MySQL [(none)]> set admin-admin_credentials=‘admin:admin;erciyuan:123‘; Query OK, 1 row affected (0.000 sec) MySQL [(none)]> select @@admin-admin_credentials; +---------------------------+ | @@admin-admin_credentials | +---------------------------+ | admin:admin;erciyuan:123 | +---------------------------+ 1 row in set (0.001 sec) //ProxySQL main库内的表 MySQL [(none)]> show tables; +--------------------------------------------+ | tables | +--------------------------------------------+ | global_variables | | mysql_aws_aurora_hostgroups | | mysql_collations | | mysql_galera_hostgroups | | 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_aws_aurora_hostgroups | | runtime_mysql_galera_hostgroups | | 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 | +--------------------------------------------+ 24 rows in set (0.000 sec) //使修改立即生效 MySQL [(none)]> load admin variables to runtime; Query OK, 0 rows affected (0.000 sec) //使修改永久保存到磁盘 MySQL [(none)]> save admin variables to disk; Query OK, 33 rows affected (0.002 sec)
修改后,就可以使用该用户名和密码连接管理接口
[root@test1 ~]# mysql -uerciyuan -p123 -P6032 -h127.0.0.1 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)]>
所有的配置操作都是在修改main库中对应的表
MySQL [(none)]> select * from global_variables where variable_name=‘admin-admin_credentials‘;+-------------------------+--------------------------+ | variable_name | variable_value | +-------------------------+--------------------------+ | admin-admin_credentials | admin:admin;erciyuan:123 | +-------------------------+--------------------------+ 1 row in set (0.001 sec)
必须要区分admin管理接口的用户名和mysql_users中的用户名
admin管理接口的用户必须不能存在于mysql_users中,这是出于安全的考虑,防止通过admin管理接口用户猜出mysql_users中的用户
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)]> //添加专有的查看用户 MySQL [(none)]> set admin-stats_credentials=‘stats:stats;erciyuanstats:123‘; Query OK, 1 row affected (0.000 sec) MySQL [(none)]> load admin variables to runtime; Query OK, 0 rows affected (0.000 sec) MySQL [(none)]> save admin variables to disk; Query OK, 33 rows affected (0.001 sec)
同样,这个变量中的用户必须不能存在于mysql_users表中
使用mystats用户远程连接查看
[root@test2 ~]# mysql -uerciyuanstats -p123 -h192.168.100.100 -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)]>
//如果连接不上请关闭防火墙和selinux # systemctl disable --now firewalld # setenforce 0
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_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.000 sec)
admin-mysql_ifaces 变量指定admin接口的监听地址,格式为冒号分隔的hostname:port列表。默认监听在 0.0.0.0:6032
注意,允许使用UNIX的domain socket进行监听,这样本主机内的应用程序就可以直接被处理。
例如:
MySQL [(none)]> SET admin-mysql_ifaces=‘0.0.0.0:6032;/tmp/proxysql_admin.sock‘; Query OK, 1 row affected (0.000 sec) MySQL [(none)]> load admin variables to runtime; Query OK, 0 rows affected (0.000 sec) MySQL [(none)]> save admin variables to disk; Query OK, 33 rows affected (0.002 sec)
使用ProxySQL的Admin管理接口连上ProxySQL,可查看ProxySQL拥有的库
[root@test2 ~]# mysql -uerciyuan -p123 -h192.168.100.100 -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)]> 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)
其中:
ProxySQL 内部使用的是 SQLite3 数据库,无论是内存数据库还是磁盘数据库,都是通过SQLite3引 擎进行解析、操作的。它和 MySQL 的语法可能稍有不同,但ProxySQL会对不兼容的语法自动进行调整,最大程度上保证MySQL语句的有效率。
上面描述main库的时候,只是说了内存数据库需要持久化到disk库才能永久保存配置。但实际上,修改了main库中的配置后,并不会立即生效,它还需要load到runtime的数据结构中才生效,只有在runtime数据结构中的配置才是对ProxySQL当前有效的配置
# vim /etc/proxysql.cnf # interfaces="0.0.0.0:6033;/tmp/proxysql.sock" interfaces="0.0.0.0:3306" default_schema="information_schema" stacksize=1048576 server_version="5.5.30" connect_timeout_server=3000
[root@test2 ~]# systemctl stop proxysql [root@test2 ~]# systemctl start proxysql [root@test2 ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:3306 0.0.0.0:* LISTEN 0 128 0.0.0.0:3306 0.0.0.0:* LISTEN 0 128 0.0.0.0:3306 0.0.0.0:* LISTEN 0 128 0.0.0.0:3306 0.0.0.0:* LISTEN 0 128 0.0.0.0:6032 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:*
原文:https://www.cnblogs.com/lichouluoyu/p/14842735.html