Can't connect to MYSQL from CLI with --port specified

Good day and thanks for hestiacp!

I am setting up Laravel app. I’ve created a database and set up env variables accordingly. Then I tried to check the connection with a cli command, that ultimately expands to this:

mysql --host=127.0.0.1 --port=3306 --user=abc_xyz --password=123456 --default-character-set=utf8mb4 abc_xyz

The result was this:

ERROR 2002 (HY000): Can't connect to server on '127.0.0.1' (115)

My second try was to change 127.0.0.1 to localhost like this:

mysql --host=localhost --port=3306 --user=abc_xyz --password=123456 --default-character-set=utf8mb4 abc_xyz

The result was this:

ERROR 2002 (HY000): Can't connect to server on 'localhost' (115)

Then I remover the port like this:

mysql --host=localhost --user=abc_xyz --password=123456 --default-character-set=utf8mb4 abc_xyz

This time I was able to connect as expected.

My last try was to connect to 127.0.0.1 without a port like this:

mysql --host=127.0.0.1 --user=abc_xyz --password=123456 --default-character-set=utf8mb4 abc_xyz

And it resulted with an error again:

ERROR 2002 (HY000): Can't connect to server on '127.0.0.1' (115)

It seems that I need to use localhost with no port. But Laravel doesn’t seem to have an option to omit the port. And it seems strange anyway. What could that be?

More to that, I do have another site with a CMS that can connect to db server with same port specified.

Please suggest what can I do to fix it or debug furher.

I thought that the problem will go away with the upgrade. I performed it like advised here but with no success. The other aspect of this is that I cannot connect to a db through the ssh tunnel using dbeaver.

Does it sound like an installation-specific issue? Or is that a general behaviour and I just need to fix something? Please, share your knowledge.

Hello @ivangretsky,

I don’t use Laravel but I just tried to simulate your connections from my own Hestia Server and redirecting a port via ssh tunnel from another machine.

Note: I’m using a fresh Hestia CP 1.8.8 installed on Debian 12 (AMD64) and MariaDB 10.11.4.

1.- Testing connection to MariaDB from local user in Hestia Server (localhost, loopback ip, localhost + port 3306, loopback ip + port 3306):

localhost only

$ mysql --host=localhost --user=test_MyUser --password='MyPass' --default-character-set=utf8mb4 test_testmysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4117
Server version: 10.11.4-MariaDB-1~deb12u1 Debian 12

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [test_testmysql]> quit
Bye

loopback ip

$ mysql --host=127.0.0.1 --user=test_MyUser --password='MyPass' --default-character-set=utf8mb4 test_testmysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4118
Server version: 10.11.4-MariaDB-1~deb12u1 Debian 12

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [test_testmysql]> quit
Bye

localhost + port 3306

$ mysql --host=localhost --port=3306 --user=test_MyUser --password='MyPass' --default-character-set=utf8mb4 test_testmysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4119
Server version: 10.11.4-MariaDB-1~deb12u1 Debian 12

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [test_testmysql]> quit
Bye

loopback ip + port 3306

$ mysql --host=127.0.0.1 --port=3306 --user=test_MyUser --password='MyPass' --default-character-set=utf8mb4 test_testmysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4130
Server version: 10.11.4-MariaDB-1~deb12u1 Debian 12

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [test_testmysql]> quit
Bye

All tests are working fine.

2.- Testing connection to MariaDB from another machine and redirect local port 3336 (I can’t use port 3306 because I’ve a MariaDB server running on that port) to port 3306 via ssh tunnel with Hestia Server:

$ ssh -N -L 3336:127.0.0.1:3306 -i ~/.ssh/MyUser.hestiaserver MyUser@hestiaserver

Now I try to connect using loopback ip and localhost:

loopback ip + port 3336

$ mysql --host=127.0.0.1 --port=3336 --user=test_MyUser --password='MyPass' --default-character-set=utf8mb4 test_testmysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4138
Server version: 10.11.4-MariaDB-1~deb12u1 Debian 12

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [test_testmysql]> quit
Bye

localhost + port 3336

$ mysql --host=localhost --port=3336 --user=test_MyUser --password='MyPass' --default-character-set=utf8mb4 test_testmysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4139
Server version: 10.11.4-MariaDB-1~deb12u1 Debian 12

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [test_testmysql]> quit
Bye

And again, all is working fine.

I don’t know what is doing Laravel or how are you runnig the tunnel but as you can see, in my tests all is working as expected.

Cheers,
sahsanu

1 Like

I do use it with Laravel and haven’t experienced any issue yet …

2 Likes

Thank you, friends!

After your input I realized I need to contact my hosting provider, who shipped a VPS with hestiacp as a ready-made image. They checked everything and said that MariaDB was configured in a different way and has been running on a socket not a port. There must be a way to run Laravel with a DB via a socket, but they already reconfigured MariaDB to be available on a port.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.