mysql 实现读写分离的方式有以下几种:
ProxySQL 是一款可以实际用于生产环境的 MySQL 中间件,它有官方版和 percona 版两种。percona版是在官方版的基础上修改的,添加了几个比较实用的工具。生产环境建议用官方版。
ProxySQL 是用 C++ 语言开发的,虽然也是一个轻量级产品,但性能很好(据测试,能处理千亿级的数据),功能也足够,能满足中间件所需的绝大多数功能,包括:
[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 启动后,将监听两个端口:
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
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管理接口的用户必须不能存在于mysql_users中,这是出于安全的考虑,防止通过admin管理接口用户猜出mysql_users中的用户
mysq_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)]> 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接口的监听地址,格式为冒号分隔的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的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)
其中:
ProxySQL 内部使用的是 SQLite3 数据库,无论是内存数据库还是磁盘数据库,都是通过SQLite3引 擎进行解析、操作的。它和 MySQL 的语法可能稍有不同,但ProxySQL会对不兼容的语法自动进行调整,最大程度上保证MySQL语句的有效率。
上面描述main库的时候,只是说了内存数据库需要持久化到disk库才能永久保存配置。但实际上,修改了main库中的配置后,并不会立即生效,它还需要load到runtime的数据结构中才生效,只有在runtime数据结构中的配置才是对ProxySQL当前有效的配置
//找到这一行并且将端口号改为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 [::]:*