{"id":2620,"date":"2022-10-11T10:53:12","date_gmt":"2022-10-11T10:53:12","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=2620"},"modified":"2024-01-27T17:27:34","modified_gmt":"2024-01-27T17:27:34","slug":"can-not-connect-to-proxysql-reasons-and-fixtures","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/can-not-connect-to-proxysql-reasons-and-fixtures","title":{"rendered":"Can not connect to ProxySQL: reasons and fixtures"},"content":{"rendered":"\n<p>This ProxySQL post is sourced from an error I faced recently<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>handler___status_CONNECTING_CLIENT___STATE_SERVER_HANDSHAKE(): &#91;ERROR] ProxySQL Error: Access denied for user<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignright size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/www.percona.com\/sites\/default\/files\/sm-share-default.png\" alt=\"\" width=\"266\" height=\"138\"\/><\/figure><\/div>\n\n\n\n<p>ProxySQL has become one of the popular choice as the &#8220;proxy&#8221; for MySQL databases. This post is tipping you off for debugging connectivity issue and fixtures around it for a specific use-case.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ProxySQL Connectivity<\/h2>\n\n\n\n<p>Ofcourse while to connect ProxySQL we have two choices for what we want to do: Admin Interface and MySQL.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Admin interface allows us to configure and manage the ProxySQL.<\/li><li>MySQL interface is basically the &#8220;proxy&#8221; connectivity to MySQL databases configured in ProxySQL.<\/li><\/ul>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Connecting ProxySQL Admin Interface<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql -uADMIN_USER -pADMIN_PASSWORD -h127.0.0.1 -P6032<\/code><\/pre>\n\n\n\n<p>The ADMIN_USER and ADMIN_PASSWORD is taken from admin-admin_credentials configuration. Port 6032 is defined in mysql_ifaces configuration option and it is configurable with multiple ports as well.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Connecting backend MySQL Servers from ProxySQL<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql -uUSER -pPASSWORD -h127.0.0.1 -P6033<\/code><\/pre>\n\n\n\n<p>Here, USER and PASSWORD are defined in mysql_users table (and ofcourse loaded to runtime). The Port 6033 is defined in interfaces configuration option and it is configurable.<\/p>\n\n\n\n<p>Now that we have established very basic understanding of connectivity, let&#8217;s produce the connectivity issue that we&#8217;re going to discuss further along with the resolution.<\/p>\n\n\n\n<p>Start with creating an &#8220;admin&#8221; user for our application to connect to the database. This user is ofcourse present on MySQL server. Note that even though the password here is kept as plaintext, we can also use the encrypted password from MySQL&#8217;s user table.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@kedar ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1<br>\u2026<br>mysql&gt; insert into mysql_users (username, password,default_hostgroup) values ('admin','admin',0);<br>Query OK, 1 row affected (0.00 sec)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql&gt; load mysql users to runtime; save mysql users to disk;\nQuery OK, 0 rows affected (0.00 sec)\nQuery OK, 0 rows affected (0.01 sec)<\/code><\/pre>\n\n\n\n<p>Let&#8217;s try re-connecting to the admin interface again<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@kedar ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1\nmysql: &#91;Warning] Using a password on the command line interface can be insecure.\nERROR 1045 (28000): ProxySQL Error: Access denied for user 'admin'@'127.0.0.1' (using password: YES)\n&#91;root@kedar ~]#\n# note that you can still connect to MySQL port though (6033)<\/code><\/pre>\n\n\n\n<p>Looking at error log<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@kedar ~]# tail -1 \/var\/lib\/proxysql\/proxysql.log<br>2022-10-08 10:41:16 MySQL_Session.cpp:5439:handler___status_CONNECTING_CLIENT___STATE_SERVER_HANDSHAKE(): &#91;ERROR] ProxySQL Error: Access denied for user 'admin'@'127.0.0.1' (using password: YES)<br>&#91;root@kedar ~]#<\/code><\/pre>\n\n\n\n<p>There is nothing we can see in the error log that hints us anything about the error. But we know that the access is a mess since we created that user! Let&#8217;s get rid of that, can we? Without actually connecting to ProxySQL?<br>Technically we can. ProxySQL uses SQLite as default datastore to save the data on disk and we can manipulate it with CAUTION.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Edit ProxySQL Database<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Stop ProxySQL\n&#91;root@kedar ~]# systemctl stop proxysql\n\n# Backup the db file\n&#91;root@kedar ~]# cd \/var\/lib\/proxysql\/\n&#91;root@kedar ~]# cp proxysql.db proxysql.db.1\n\n# Open db file with sqlite3\n&#91;root@kedar proxysql]# sqlite3 proxysql.db\nSQLite version 3.7.17 2013-05-20 00:56:22\nEnter \".help\" for instructions\nEnter SQL statements terminated with a \";\"\nsqlite&gt; .tables\nglobal_variables\nmysql_aws_aurora_hostgroups\nmysql_collations\nmysql_firewall_whitelist_rules\nmysql_firewall_whitelist_sqli_fingerprints\nmysql_firewall_whitelist_users\nmysql_galera_hostgroups\nmysql_group_replication_hostgroups\nmysql_query_rules\nmysql_query_rules_fast_routing\nmysql_replication_hostgroups\nmysql_servers\nmysql_users\nproxysql_servers\nrestapi_routes\nscheduler\n\nsqlite&gt; .headers on\nsqlite&gt; .schema mysql_users\nCREATE 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 &gt;=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));\nsqlite&gt; select * from mysql_users;\nusername|password|active|use_ssl|default_hostgroup|default_schema|schema_locked|transaction_persistent|fast_forward|backend|frontend|max_connections|attributes|comment\nkedar|*8D39C185A69E3453E27F96FBDBFFC701DE027750|1|0|10|test|0|1|0|0|1|10000||\nkedar|*8D39C185A69E3453E27F96FBDBFFC701DE027750|1|0|10|test|0|1|0|1|0|10000||\n<strong>admin|*4ACFE3202A5FF5CF467898FC58AAB1D615029441|1|0|10|test|0|1|0|1|1|10000||<\/strong>\n\n# Delete the user we created and verify it is gone\nsqlite&gt; delete from mysql_users where username='admin';\nsqlite&gt; select * from mysql_users;\nusername|password|active|use_ssl|default_hostgroup|default_schema|schema_locked|transaction_persistent|fast_forward|backend|frontend|max_connections|attributes|comment\nkedar|*8D39C185A69E3453E27F96FBDBFFC701DE027750|1|0|10|test|0|1|0|0|1|10000||\nkedar|*8D39C185A69E3453E27F96FBDBFFC701DE027750|1|0|10|test|0|1|0|1|0|10000||\nsqlite&gt; .quit\n\n# Start ProxySQL\n&#91;root@kedar proxysql]# systemctl start proxysql\n\n# Attempt connectivity (Voila!)\n&#91;root@kedar proxysql]# mysql -uadmin -padmin -P6032 -h127.0.0.1\nWelcome to the MySQL monitor.  Commands end with ; or \\g.\nYour MySQL connection id is 1\nServer version: 5.5.30 (ProxySQL Admin Module)\n\nCopyright (c) 2009-2021 Percona LLC and\/or its affiliates\nCopyright (c) 2000, 2021, Oracle and\/or its affiliates.\n\nOracle is a registered trademark of Oracle Corporation and\/or its\naffiliates. Other names may be trademarks of their respective\nowners.\n\nType 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.\n\nmysql&gt; \\s\n--------------\nmysql  Ver 14.14 Distrib 5.7.36-39, for Linux (x86_64) using  6.2\n\nConnection id:\t\t1\nCurrent database:\tadmin\nCurrent user:\t\tpercona\nSSL:\t\t\tNot in use\nCurrent pager:\t\tstdout\nUsing outfile:\t\t''\nUsing delimiter:\t;\nServer version:\t\t5.5.30 (ProxySQL Admin Module)\nProtocol version:\t10\nConnection:\t\t127.0.0.1 via TCP\/IP\nServer characterset:\tutf8\nDb     characterset:\tutf8\nClient characterset:\tutf8\nConn.  characterset:\tutf8\nTCP port:\t\t6032\nUptime:\t\t\t2 sec\n\nThreads: 0  Questions: 0  Slow queries: 0\n--------------\n<\/code><\/pre>\n\n\n\n<p>Again, ensure that you have the backups in place before playing with the ProxySQL&#8217;s backend database. <\/p>\n\n\n\n<p>But why were we blocked? We faced this issue as the documentation clearly mentions the limitations for usernames. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>The users defined in admin-admin_credentials or admin-stats_credentials cannot be used also in mysql_users table.<\/p><\/blockquote>\n\n\n\n<p>Thus this is not only limited to &#8220;admin&#8221; user but also to any other usernames chosen for those two roles. And if you do so, ProxySQL will not warn you or let you know via error log but just block you from accessing it!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reset ProxySQL<\/h2>\n\n\n\n<p>There&#8217;s one more thing we can do here but it is like a hard-reset. We can also reset the ProxySQL setup to initialize the backend database from the configuration.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@kedar ~]# proxysql --initial<br>\u2026 &#91;INFO] Using config file \/etc\/proxysql.cnf<br>Renaming database file \/var\/lib\/proxysql\/proxysql.db<br>\u2026<\/code><\/pre>\n\n\n\n<p>The initial flag here will reset the SQLite database file to its original state loading it from configuration and rename the existing SQLite database file in case a rollback is required.<\/p>\n\n\n\n<p>That&#8217;s what you see in ProxySQL data dir:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@kedar ~]# ls proxysql.db*<br>proxysql.db proxysql.db.bak<\/code><\/pre>\n\n\n\n<p><strong>Quick Tip<\/strong>: To prepare mysql_users insert statements you may use following insert query on the database. Here I&#8217;m assuming default hostgroup is 10.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select concat('INSERT INTO mysql_users(username,password,default_hostgroup) VALUES (',user,',',authentication_string,',10);') from mysql.user group by user;<\/code><\/pre>\n\n\n\n<p>Hope this helps! Happy ProxySQLing.<\/p>\n","protected":false},"excerpt":{"rendered":"This ProxySQL post is sourced from an error I faced recently ProxySQL has become one of the popular choice as the &#8220;proxy&#8221; for MySQL databases. This post is tipping you&hellip;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,377,866,6],"tags":[508,517,427,479,509,507],"class_list":{"0":"post-2620","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-mysql","7":"category-mysql-articles","8":"category-proxysql","9":"category-technical","10":"tag-access-denied","11":"tag-access-denied-for-user-admin127-0-0-1","12":"tag-mysql","13":"tag-proxysql","14":"tag-proxysql-error","15":"tag-sqlite"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=2620"}],"version-history":[{"count":11,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2620\/revisions"}],"predecessor-version":[{"id":2714,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2620\/revisions\/2714"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=2620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=2620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=2620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}