暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

故障处理|无法连接到 ProxySQL:原因和固定装置

原创 小小亮 2022-10-13
4152

这篇 ProxySQL 帖子来自我最近遇到的一个错误

handler___status_CONNECTING_CLIENT___STATE_SERVER_HANDSHAKE(): [ERROR] ProxySQL Error: Access denied for user

ProxySQL 已成为 MySQL 数据库“代理”的流行选择之一。这篇文章提示您针对特定用例调试连接问题和解决方案。

ProxySQL 连接

当然,在连接 ProxySQL 时,我们有两种选择:Admin Interface 和 MySQL。

  • 管理界面允许我们配置和管理 ProxySQL。
  • MySQL 接口基本上是与 ProxySQL 中配置的 MySQL 数据库的“代理”连接。

连接管理界面:

mysql -uADMIN_USER -pADMIN_PASSWORD -h127.0.0.1 -P6032

ADMIN_USER 和 ADMIN_PASSWORD 取自 admin-admin_credentials 配置。端口 6032 在 mysql_ifaces 配置选项中定义,它也可以配置为多个端口。

连接配置的 MySQL 数据库

mysql -uUSER -pPASSWORD -h127.0.0.1 -P6033

在这里,USER 和 PASSWORD 在 mysql_users 表中定义(当然加载到运行时)。端口 6033 在接口配置选项中定义,它是可配置的。

现在我们已经对连通性建立了非常基本的了解,让我们提出我们将在解决方案中进一步讨论的连通性问题。


首先为我们的应用程序创建一个“管理员”用户以连接到数据库。该用户当然存在于 MySQL 服务器上。请注意,即使这里的密码保存为明文,我们也可以使用 MySQL 用户表中的加密密码。

[root@kedar ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1

mysql> insert into mysql_users (username, password,default_hostgroup) values ('admin','admin',0);
Query OK, 1 row affected (0.00 sec)
mysql> load mysql users to runtime; save mysql users to disk;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.01 sec)

让我们再次尝试重新连接到管理界面

[root@kedar ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): ProxySQL Error: Access denied for user 'admin'@'127.0.0.1' (using password: YES)
[root@kedar ~]#
# note that you can still connect to MySQL port though (6033)

查看错误日志

[root@kedar ~]# tail -1 /var/lib/proxysql/proxysql.log
2022-10-08 10:41:16 MySQL_Session.cpp:5439:handler___status_CONNECTING_CLIENT___STATE_SERVER_HANDSHAKE(): [ERROR] ProxySQL Error: Access denied for user 'admin'@'127.0.0.1' (using password: YES)
[root@kedar ~]#

我们在错误日志中看不到任何提示我们有关错误的任何信息。但是我们知道,自从我们创建了那个用户之后,访问就变得一团糟了!让我们摆脱它,可以吗?没有实际连接到 ProxySQL?
从技术上讲,我们可以。ProxySQL 使用 SQLite 作为默认数据存储来将数据保存在磁盘上,我们可以小心操作它。

编辑 ProxySQL 数据库

# Stop ProxySQL
[root@kedar ~]# systemctl stop proxysql

# Backup the db file
[root@kedar ~]# cd /var/lib/proxysql/
[root@kedar ~]# cp proxysql.db proxysql.db.1

# Open db file with sqlite3
[root@kedar proxysql]# sqlite3 proxysql.db
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
global_variables
mysql_aws_aurora_hostgroups
mysql_collations
mysql_firewall_whitelist_rules
mysql_firewall_whitelist_sqli_fingerprints
mysql_firewall_whitelist_users
mysql_galera_hostgroups
mysql_group_replication_hostgroups
mysql_query_rules
mysql_query_rules_fast_routing
mysql_replication_hostgroups
mysql_servers
mysql_users
proxysql_servers
restapi_routes
scheduler

sqlite> .headers on
sqlite> .schema mysql_users
CREATE TABLE mysql_users (username VARCHAR NOT NULL , password VARCHAR , active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1 , use_ssl INT CHECK (use_ssl IN (0,1)) NOT NULL DEFAULT 0 , default_hostgroup INT NOT NULL DEFAULT 0 , default_schema VARCHAR , schema_locked INT CHECK (schema_locked IN (0,1)) NOT NULL DEFAULT 0 , transaction_persistent INT CHECK (transaction_persistent IN (0,1)) NOT NULL DEFAULT 1 , fast_forward INT CHECK (fast_forward IN (0,1)) NOT NULL DEFAULT 0 , backend INT CHECK (backend IN (0,1)) NOT NULL DEFAULT 1 , frontend INT CHECK (frontend IN (0,1)) NOT NULL DEFAULT 1 , max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 10000 , attributes VARCHAR CHECK (JSON_VALID(attributes) OR attributes = '') NOT NULL DEFAULT '' , comment VARCHAR NOT NULL DEFAULT '' , PRIMARY KEY (username, backend) , UNIQUE (username, frontend));
sqlite> select * from mysql_users;
username|password|active|use_ssl|default_hostgroup|default_schema|schema_locked|transaction_persistent|fast_forward|backend|frontend|max_connections|attributes|comment
kedar|*8D39C185A69E3453E27F96FBDBFFC701DE027750|1|0|10|test|0|1|0|0|1|10000||
kedar|*8D39C185A69E3453E27F96FBDBFFC701DE027750|1|0|10|test|0|1|0|1|0|10000||
admin|*4ACFE3202A5FF5CF467898FC58AAB1D615029441|1|0|10|test|0|1|0|1|1|10000||

# Delete the user we created and verify it is gone
sqlite> delete from mysql_users where username='admin';
sqlite> select * from mysql_users;
username|password|active|use_ssl|default_hostgroup|default_schema|schema_locked|transaction_persistent|fast_forward|backend|frontend|max_connections|attributes|comment
kedar|*8D39C185A69E3453E27F96FBDBFFC701DE027750|1|0|10|test|0|1|0|0|1|10000||
kedar|*8D39C185A69E3453E27F96FBDBFFC701DE027750|1|0|10|test|0|1|0|1|0|10000||
sqlite> .quit

# Start ProxySQL
[root@kedar proxysql]# systemctl start proxysql

# Attempt connectivity (Voila!)
[root@kedar proxysql]# mysql -uadmin -padmin -P6032 -h127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2009-2021 Percona LLC and/or its affiliates
Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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.

mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.7.36-39, for Linux (x86_64) using  6.2

Connection id:		1
Current database:	admin
Current user:		percona
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.5.30 (ProxySQL Admin Module)
Protocol version:	10
Connection:		127.0.0.1 via TCP/IP
Server characterset:	utf8
Db     characterset:	utf8
Client characterset:	utf8
Conn.  characterset:	utf8
TCP port:		6032
Uptime:			2 sec

Threads: 0  Questions: 0  Slow queries: 0
--------------

同样,在使用 ProxySQL 的后端数据库之前,请确保备份到位。

但是为什么我们被阻止了?我们遇到了这个问题,因为文档清楚地提到了用户名的限制。

在 admin-admin_credentials 或 admin-stats_credentials 中定义的用户也不能在 mysql_users 表中使用。

因此,这不仅限于“管理员”用户,还包括为这两个角色选择的任何其他用户名。如果你这样做了,ProxySQL 不会警告你或通过错误日志让你知道,只会阻止你访问它!

我们还可以在这里做一件事,但这就像硬重置一样。我们还可以重置 ProxySQL 设置以从配置中初始化后端数据库。

[root@kedar ~]# proxysql --initial
… [INFO] Using config file /etc/proxysql.cnf
Renaming database file /var/lib/proxysql/proxysql.db

这里的初始标志会将 SQLite 数据库文件重置为其从配置加载它的原始状态,并重命名现有的 SQLite 数据库文件,以防需要回滚。

这就是您在 ProxySQL 数据目录中看到的内容:

[root@kedar ~]# ls proxysql.db*
proxysql.db proxysql.db.bak

快速提示:要准备 mysql_users 插入语句,您可以在数据库上使用以下插入查询。这里我假设默认主机组是 10。

select concat('INSERT INTO mysql_users(username,password,default_hostgroup) VALUES (',user,',',authentication_string,',10);') from mysql.user group by user;

希望这可以帮助!快乐的 ProxySQLing。


原文标题:Can not connect to ProxySQL: reasons and fixtures

原文作者:Kedar

原文链接:https://kedar.nitty-witty.com/blog/can-not-connect-to-proxysql-reasons-and-fixtures?utm_source=rss&utm_medium=rss&utm_campaign=can-not-connect-to-proxysql-reasons-and-fixtures



「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论