本文共 7244 字,大约阅读时间需要 24 分钟。
一、说明
ProxySQL是一个开源的MySQL代理服务器,这意味着它充当MySQL服务器和访问其数据库的应用程序之间的中介。ProxySQL可以通过在多个数据库服务器池之间分配流量来提高性能,并且如果一个或多个数据库服务器发生故障,还可以通过自动故障切换到备用数据库来提高可用性。系统环境:master1:ubuntu16.04 mysql5.6 192.168.1.10 3307
master2:ubuntu16.04 mysql5.6 192.168.1.20 3307slave1: ubuntu16.04 mysql5.6 192.168.1.10 3308 slave2: ubuntu16.04 mysql5.6 192.168.1.10 3309slave3: ubuntu16.04 mysql5.6 192.168.1.20 3308 slave4: ubuntu16.04 mysql5.6 192.168.1.20 3309【proxysql】:ubuntu16.04 mysql 192.168.1.30 3306master1与master2为双主双从模式。
主从复制前边文章和网上文章都特别详细。这里不做介绍。请参考https://blog.51cto.com/13120271/2140400。第一步 - 安装ProxySQLProxySQL的开发人员在他们的GitHub版本页面上为所有ProxySQL版本提供官方Ubuntu软件包,因此我们将从那里下载最新的软件包版本并进行安装。您可以在发布列表中找到最新的软件包。命名约定是proxysql_version-distribution.deb,其中version类似于版本1.4.4的1.4.4字符串,并且distribution是一个类似于64位Ubuntu 16.04的ubuntu16_amd64字符串。
将最新的官方软件包(编写本文时为1.4.4)下载到/tmp目录中。
cd /tmp
curl -OL
用dpkg安装包,用于管理.deb软件包。-i标志表示我们要从指定的文件安装。
sudo dpkg -i proxysql_*
此时,您不再需要.deb文件,因此您可以将其删除。
rm proxysql_*
接下来,我们需要一个MySQL客户端应用程序来连接到ProxySQL实例。这是因为ProxySQL内部使用一个MySQL兼容的接口来执行管理任务。我们将使用mysql命令行工具,它是mysql-client Ubuntu存储库中可用包的一部分。
更新软件包存储库以确保获得最新的预捆绑版本,然后安装mysql-client软件包。
sudo apt-get update
sudo apt-get install mysql-client
您现在满足运行ProxySQL的所有要求,但该服务在安装后不会自动启动,因此请立即手动启动。
sudo systemctl start proxysql
现在,ProxySQL应该以其默认配置运行。你可以使用systemctl查看。
systemctl status proxysql
输出看起来像这样:
● proxysql.service - LSB: High Performance Advanced Proxy for MySQL
Loaded: loaded (/etc/init.d/proxysql; bad; vendor preset: enabled)Active: active (running) since Thu 2017-12-21 19:19:20 UTC; 5s agoDocs: man:systemd-sysv-generator(8)Process: 12350 ExecStart=/etc/init.d/proxysql start (code=exited, status=0/SUCCESS)Tasks: 23Memory: 30.9MCPU: 86msCGroup: /system.slice/proxysql.service├─12355 proxysql -c /etc/proxysql.cnf -D /var/lib/proxysql└─12356 proxysql -c /etc/proxysql.cnf -D /var/lib/proxysql
Active (running)行表示ProxySQL已安装并正在运行。
接下来,我们将通过设置用于访问ProxySQL管理界面的密码来提高安全性。
第二步 - 设置ProxySQL管理员密码第一次启动新的ProxySQL安装时,它使用程序包提供的配置文件来初始化其所有配置变量的默认值。初始化后,ProxySQL将其配置存储在数据库中,您可以通过命令行进行管理和修改。要在ProxySQL中设置管理员密码,我们将连接到该配置数据库并更新相应的变量。
首先,访问管理界面。系统将提示您输入密码,在默认安装时,密码为admin。
mysql -u admin -p -h 127.0.0.1 -P 6032 --prompt='ProxySQLAdmin> '
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2Server version: 5.5.30 (ProxySQL Admin Module)Copyright (c) 2000, 2017, 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 respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
ProxySQLAdmin>
通过更新(UPDATE)global_variables数据库中的admin-admin_credentials配置变量来更改管理帐户密码。请住将以下命令的password更改为您选择的强密码。
UPDATE global_variables SET variable_value='admin:password' WHERE variable_name='admin-admin_credentials';
Query OK, 1 row affected (0.00 sec)
由于ProxySQL的配置系统的工作方式,此更改不会立即生效。它由三个独立的层组成:
内存,在从命令行界面进行修改时会被更改。
运行时,ProxySQL使用它作为有效配置。磁盘,用于使配置在重新启动时保持不变。现在,你所做的改变是在内存中。要使更改生效,您必须将内存设置复制到运行时领域,然后将它们保存到磁盘以使其保持不变。ProxySQLAdmin> LOAD ADMIN VARIABLES TO RUNTIME;
ProxySQLAdmin> SAVE ADMIN VARIABLES TO DISK;
第三步-修改配置文件
egrep -v '(^$|^#)' /etc/proxysql.cnf
admin_credentials="admin:admin"
mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock"}mysql_variables={ threads=4max_connections=2048default_query_delay=0default_query_timeout=36000000have_compress=truepoll_timeout=2000interfaces="0.0.0.0:6033;/tmp/proxysql.sock"default_schema="information_schema"stacksize=1048576server_version="5.5.30"connect_timeout_server=3000monitor_username="monitor"monitor_password="monitor"monitor_history=600000monitor_connect_interval=60000monitor_ping_interval=10000monitor_read_only_interval=1500monitor_read_only_timeout=500ping_interval_server_msec=120000ping_timeout_server=500commands_stats=truesessions_sort=trueconnect_retries_on_failure=10}mysql_servers =({ address = "192.168.1.20" # no default, required . If port is 0 , address is interpred as a Unix Socket Domainport = 3307 # no default, required . If port is 0 , address is interpred as a Unix Socket Domainhostgroup = 1 # no default, requiredstatus = "ONLINE" # default: ONLINEweight = 1 # default: 1compression = 0 # default: 0max_connections = 200},{ address = "192.168.1.10" # no default, required . If port is 0 , address is interpred as a Unix Socket Domainport = 3307 # no default, required . If port is 0 , address is interpred as a Unix Socket Domainhostgroup = 1 # no default, requiredstatus = "ONLINE" # default: ONLINEweight = 1 # default: 1compression = 0 # default: 0max_connections = 200},{ address = "192.168.1.20" # no default, required . If port is 0 , address is interpred as a Unix Socket Domainport = 3308 # no default, required . If port is 0 , address is interpred as a Unix Socket Domainhostgroup = 2 # no default, requiredstatus = "ONLINE" # default: ONLINEweight = 1 # default: 1compression = 0 # default: 0max_connections = 1000},{ address = "192.168.1.20" # no default, required . If port is 0 , address is interpred as a Unix Socket Domainport = 3309 # no default, required . If port is 0 , address is interpred as a Unix Socket Domainhostgroup = 2 # no default, requiredstatus = "ONLINE" # default: ONLINEweight = 1 # default: 1compression = 0 # default: 0max_connections = 1000},{ address = "192.168.1.10" # no default, required . If port is 0 , address is interpred as a Unix Socket Domainport = 3308 # no default, required . If port is 0 , address is interpred as a Unix Socket Domainhostgroup = 2 # no default, requiredstatus = "ONLINE" # default: ONLINEweight = 1 # default: 1compression = 0 # default: 0max_connections = 1000},{ address = "192.168.1.10" # no default, required . If port is 0 , address is interpred as a Unix Socket Domainport = 3309 # no default, required . If port is 0 , address is interpred as a Unix Socket Domainhostgroup = 2 # no default, requiredstatus = "ONLINE" # default: ONLINEweight = 1 # default: 1compression = 0 # default: 0max_connections = 1000})mysql_users:({ username = "admin" # no default , requiredpassword = "password" # default: ''default_hostgroup = 1 # default: 0max_connections=1000default_schema="test"active = 1 # default: 1})mysql_query_rules:()scheduler=()mysql_replication_hostgroups=({ writer_hostgroup=1reader_hostgroup=2comment="test repl 1"})
从新启动proxysql。
sudo systemctl restart proxysql
然后我们登录上去验证一下。mysql -u admin -p -h 127.0.0.1 -P 6032 --prompt='ProxySQLAdmin> 'mysql > show databases;mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || study || sys |
以上stats,monitor,main都从配置文件中获取的数据库;可以通过类似mysql运行时修改;而不需要重启;更多命令行的配置请参考github;
至此我们基于proxysql主从复制读写分离架构已经完成;双主或多主模型是无须实现读写分离,仅需要负载均衡:haproxy, nginx, lvs等;proxysql并没有解决,当主数据岩机时的问题;此时就需要量MHA来解决 ;后续再介绍;转载于:https://blog.51cto.com/13120271/2313106