前言
ProxySQL 是什么
ProxySQL 是一个高性能、高可用、兼容 MySQL 通信协议、支持多种 MySQL 分支的代理中间件。
准备
系统硬件
滴滴云服务器 ( DC2 ) : 安全可靠,拥有极高的性价比高,为开发者的需求而设计。适合大中小型用户购买使用。
本文示例采用:通用型 2核CPU 4GB内存 500G的EBS 云盘的服务器配置
依赖软件
系统:CentOS 7.4
MariaDB 5.5.56
ProxySQL 1.4.13
安装过程
1. 登录服务器
SSH 登录购买的滴滴云 DC2 主机:
ssh dc2-user@ip
2. 安装 MySQL
由于安装软件需要 root 权限,切换到 root 账户:
sudo -i
yum install mariadb-server mariadb mariadb-devel
添加 ProxySQL 官方 yum repo 仓库:
注意:本次安装需从互联网获取安装包,请先确认滴滴云 DC2 主机已开通 EIP。
vi /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
安装 ProxySQL:
![](/qrcode.jpg)
yum install proxysql
启动 ProxySQL:
service proxysql start
首先在两台 DC2 主机上搭建 MySQL 主从集群:(192.168.1.101:3306(Master), 192.168.1.102:3306(Slave)) 。
如果不清楚怎么配置,请参考《如何在滴滴云 DC2 上搭建高可用 MySQL 集群》。
注意:需事先在两个 MySQL 实例上配置主从同步,并打开 192.168.1.102:3306(Slave) 的 read_only 选项。
使用 ProxySQL 管理接口配置 ProxySQL。
配置 MySQL 集群实例信息:
mysql -h127.0.0.1 -P6032 -uadmin -padmin
Admin> INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (1,'192.168.1.101',3306);
Query OK, 1 row affected (0.00 sec)
Admin> INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (1,'192.168.1.102',3306);
Query OK, 1 row affected (0.00 sec)
Admin> SELECT * FROM mysql_servers;
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 1 | 192.168.1.101 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 1 | 192.168.1.102 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
2 rows in set (0.00 sec)
Admin> LOAD MYSQL VARIABLES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)
Admin> SAVE MYSQL VARIABLES TO DISK;
Query OK, 96 rows affected (0.01 sec)
配置 MySQL 集群监控信息:
注意:需事先在两个 MySQL 实例上创建 monitor 用户,密码设置为 monitor。
mysql -h127.0.0.1 -P6032 -uadmin -padmin
Admin> UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_username';
Query OK, 1 row affected (0.00 sec)
Admin> UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_password';
Query OK, 1 row affected (0.00 sec)
Admin> UPDATE global_variables SET variable_value='2000' WHERE variable_name IN ('mysql-monitor_connect_interval','mysql-monitor_ping_interval','mysql-monitor_read_only_interval');
Query OK, 3 rows affected (0.01 sec)
Admin> SELECT * FROM global_variables WHERE variable_name LIKE 'mysql-monitor_%';
+-----------------------------------------------------+----------------+
| variable_name | variable_value |
+-----------------------------------------------------+----------------+
| mysql-monitor_enabled | true |
| mysql-monitor_connect_timeout | 600 |
| mysql-monitor_ping_max_failures | 3 |
| mysql-monitor_ping_timeout | 1000 |
| mysql-monitor_read_only_max_timeout_count | 3 |
| mysql-monitor_replication_lag_interval | 10000 |
| mysql-monitor_replication_lag_timeout | 1000 |
| mysql-monitor_groupreplication_healthcheck_interval | 5000 |
| mysql-monitor_groupreplication_healthcheck_timeout | 800 |
| mysql-monitor_replication_lag_use_percona_heartbeat | |
| mysql-monitor_query_interval | 60000 |
| mysql-monitor_query_timeout | 100 |
| mysql-monitor_slave_lag_when_null | 60 |
| mysql-monitor_wait_timeout | true |
| mysql-monitor_writer_is_also_reader | true |
| mysql-monitor_username | monitor |
| mysql-monitor_password | monitor |
| mysql-monitor_history | 600000 |
| mysql-monitor_connect_interval | 2000 |
| mysql-monitor_ping_interval | 2000 |
| mysql-monitor_read_only_interval | 2000 |
| mysql-monitor_read_only_timeout | 500 |
+-----------------------------------------------------+----------------+
22 rows in set (0.01 sec)
Admin> LOAD MYSQL VARIABLES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)
Admin> SAVE MYSQL VARIABLES TO DISK;
Query OK, 96 rows affected (0.00 sec)
配置 MySQL 集群主从信息:
注意:再次提醒,需保证 192.168.1.102:3306(Slave) 的 read_only 选项为 ON。
mysql -h127.0.0.1 -P6032 -uadmin -padmin
Admin> INSERT INTO mysql_replication_hostgroups VALUES (1,2,'cluster1');
Query OK, 1 row affected (0.00 sec)
Admin> LOAD MYSQL SERVERS TO RUNTIME;
Query OK, 0 rows affected (0.01 sec)
Admin> SELECT * FROM monitor.mysql_server_read_only_log ORDER BY time_start_us DESC LIMIT 3;
+-----------+------+------------------+-----------------+-----------+-------+
| hostname | port | time_start_us | success_time_us | read_only | error |
+-----------+------+------------------+-----------------+-----------+-------+
| 127.0.0.1 | 3306 | 1544450936282201 | 113 | 0 | NULL |
| 127.0.0.1 | 3307 | 1544450936253842 | 152 | 1 | NULL |
| 127.0.0.1 | 3307 | 1544450934292240 | 116 | 1 | NULL |
+-----------+------+------------------+-----------------+-----------+-------+
3 rows in set (0.00 sec)
Admin> SELECT * FROM mysql_servers;
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 1 | 192.168.1.101 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 2 | 192.168.1.102 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
2 rows in set (0.00 sec)
配置 MySQL 集群用户信息:
注意:需事先在两个 MySQL 实例上创建 work_user 用户,密码设置为 work_passwd。
mysql -h127.0.0.1 -P6032 -uadmin -padmin
Admin> INSERT INTO mysql_users(username,password,default_hostgroup) VALUES ('work_user','work_passwd',1);
Query OK, 1 row affected (0.00 sec)
Admin> SELECT * FROM mysql_users;
+-----------+-------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
+-----------+-------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| work_user | work_passwd | 1 | 0 | 1 | NULL | 0 | 1 | 0 | 1 | 1 | 10000 |
+-----------+-------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
1 row in set (0.00 sec)
Admin> LOAD MYSQL USERS TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)
Admin> SAVE MYSQL USERS TO DISK;
Query OK, 0 rows affected (0.01 sec)
测试 ProxySQL 是否成功配置:
mysql -u work_user -pwork_passwd -h 127.0.0.1 -P6033 -e "SELECT @@hostname"
+---------------+
| @@hostname |
+---------------+
| 192-168-1-101 |
+---------------+
配置路由规则:
mysql -h127.0.0.1 -P6032 -uadmin -padmin
Admin> INSERT INTO mysql_query_rules (rule_id,active,username,match_digest,destination_hostgroup,apply) VALUES (1,1,'work_user','^SELECT \@\@hostname',2,1);
Query OK, 1 row affected (0.00 sec)
Admin> SELECT match_digest,destination_hostgroup FROM mysql_query_rules WHERE active=1;
+-------------------+-----------------------+
| match_digest | destination_hostgroup |
+-------------------+-----------------------+
| ^SELECT \@\@port | 2 |
+-------------------+-----------------------+
1 row in set (0.00 sec)
Admin> LOAD MYSQL QUERY RULES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)
测试路由规则是否成功配置:
mysql -u work_user -pwork_passwd -h 127.0.0.1 -P6033 -e "SELECT @@hostname"
+---------------+
| @@hostname |
+---------------+
| 192-168-1-102 |
+---------------+