ProxySQL Getting started

一、安装

下载安装包:下载

下载完成后用系统包管理器安装程序:

wget https://github.com/sysown/proxysql/releases/download/v1.4.9/proxysql_1.4.9-ubuntu16_amd64.deb
dpkg -i proxysql_1.4.9-ubuntu16_amd64.deb

你也可以配置apt(deb/ubuntu)或者yum(centos/redhat/...)源来安装

1.Ubuntu / Debian:

配置repo

apt-get install -y lsb-release
wget -O - 'http://repo.proxysql.com/ProxySQL/repo_pub_key' | apt-key add -
echo deb http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/$(lsb_release -sc)/ ./ 
| tee /etc/apt/sources.list.d/proxysql.list

安装

apt-get update
apt-get install proxysql OR apt-get install proxysql=version

2.Red Hat / CentOS:

配置repo

cat <<EOF | tee /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
EOF

安装

yum install proxysql OR yum install proxysql-version

二、service管理服务

程序安装完成后,我们可以通过service来管理服务进程

1.starting ProxySQL:

service proxysql start

2.stopping ProxySQL:

service proxysql stop

我们还可以通过admin管理接口来管理服务

$ mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> '
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

Admin> proxysql stop

3.Restarting ProxySQL:

service proxysql restart

同理可以通过Admin管理接口来重启服务

$ mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> '
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

Admin> proxysql restart

通过配置文件来重新初始化ProxySQL(首次启动只是创建了DB file,并没有用到config文件的所有选项)

service proxysql initial

三、升级

安装新的安装包并重启ProxySQL:

wget https://github.com/sysown/proxysql/releases/download/v1.4.9/proxysql_1.4.9-ubuntu16_amd64.deb
dpkg -i proxysql_1.4.9-ubuntu16_amd64.deb
service proxysql restart

四、查看软件版本

$ proxysql --version
ProxySQL version v1.4.9-1.1, codename Truls

debug版本的话输出结果会带有_DEBUG,debug版本比非debug版本启动得更慢,但是万一启动失败的话会更容易调试

$ proxysql --version
Main init phase0 completed in 0.000146 secs.
ProxySQL version v1.4.9-1.1_DEBUG, codename Truls

五、通过Admin管理接口配置ProxySQL

首先,记住最好通过admin管理接口来配置ProxySQL,本来我们就可以更新admin数据库来配置ProxySQL,而不用重启服务,手动、自动来更新都可以。

还有就是可以通过配置文件来配置

1.Configuring ProxySQL through the admin interface

用mysql客户端登录admin管理接口,本地6032端口,默认admin用户

$ mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> '
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

Admin>

注意:如果采用的mysql client版本等于或高于8.0.4,那么上面的登录admin的命令得加上--default-auth=mysql_native_password选项

登录admin后,可以通过SQL查询到所有的数据库和表

Admin> SHOW DATABASES;
+-----+---------+-------------------------------+
| seq | name    | file                          |
+-----+---------+-------------------------------+
| 0   | main    |                               |
| 2   | disk    | /var/lib/proxysql/proxysql.db |
| 3   | stats   |                               |
| 4   | monitor |                               |
+-----+---------+-------------------------------+
4 rows in set (0.00 sec)

这就是由你来管理的后台服务管理列表,配置如何分流路由连接、以及其他重要的设置(查询缓存,访问控制等等),如果你在内存中修改了配置,那么你必须把它load到runtime状态才会生效,最好把他持久化到disk(重启proxysql服务后配置还在),详细配置介绍看here.

2.Configuring ProxySQL through the config file

就算配置文件只是配置proxysql的备选方案,其实刚刚安装完成的时候用他来启动服务也是不错的

那我们来快速浏览一遍配置文件中一些主要的section

从上到下

  • admin_variables:囊括了控制admin管理接口功能的所有全局变量
  • mysql_variables:包括了处理如何分流路由连接功能的所有全局变量
  • mysql_servers:包含了mysql_servers表的所有记录,这些记录定义了客户端连接该被路由到哪些后台的mysql server,文件采用cfg文件格式来组织记录,样例如下:
 mysql_servers =
 (
     {
         address="127.0.0.1"
         port=3306
         hostgroup=0
         max_connections=200
     }
 )
  • mysql_users:包含mysql_users表的所有记录,这些记录定义了能连接到proxy的用户,而且这些用户能通过proxy跳转到后台mysql server,文件采用cfg文件格式来组织记录,样例如下:
 mysql_users:
 (
     {
         username = "root"
         password = "root"
         default_hostgroup = 0
         max_connections=1000
         default_schema="information_schema"
         active = 1
     }
 )
  • mysql_query_rules:

  包含mysql_query_rules表的所有记录。这些记录定义了这样的一些规则,这些规则将连接进行分类并路由到不同的mysql server,文件采用cfg文件格式来组织记录,样例如下:(示例只是举一个通用路由规则的例子,具体还得根据实际情况创建特定的规则)

 mysql_query_rules:
 (
     {
         rule_id=1
         active=1
         match_pattern="^SELECT .* FOR UPDATE$"
         destination_hostgroup=0
         apply=1
     },
     {
         rule_id=2
         active=1
         match_pattern="^SELECT"
         destination_hostgroup=1
         apply=1
     }
 )
  • 头配置项:datadir,指明初始化数据目录

翻译源至:getting started

原文地址:https://www.cnblogs.com/geek-ace/p/9523615.html