Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try both 127.0.0.1 (port) and localhost (socket) when using the installer #2520

Closed
DrupaListo-com opened this issue Feb 7, 2017 · 4 comments

Comments

@DrupaListo-com
Copy link

DrupaListo-com commented Feb 7, 2017

error: Failed to connect to your database server. The server reports the following message: No such file or directory.

I think my mariadb/mysql setup on the server is pretty standard and default.
Since a while, mysql client-server combo don't use tcp/networking to talk, but use sockets...
So using a hostname = "localhost" is invalid in many situations while 127.0.0.1 - is valid.

And finally: trying to install backdrop with the default host = localhost - fails with such an unfriendly message - see above - that it;s disgusting!

For better UX we should at least add a hint when host=localhost in one of two ways:

  1. either write some nice info like : "try 127.0.0.1 if localhost does not work" - next to the localhost/host field description
  2. and/or add a similar hint up where the ugly 'The server reports the following message: No such file or directory.' message pops up....

Ideally, the installer should test with host=localhost first and if it fails - try 127.0.0.1, and when this fails too - then give some error.

I don't know if being smart in such a way could endanger security, break things... if it could, then at least the hints/better UX proposal from above would do just fine!


PR by @quicksketch: backdrop/backdrop#2126

@DrupaListo-com
Copy link
Author

s31

@quicksketch quicksketch changed the title can't install with default mysql host=localhost Can't install with default mysql host=localhost Apr 13, 2018
@quicksketch
Copy link
Member

Thanks for filing this issue @DrupaListo-com. I've been encountering this same problem for a while now as well, and now I know what the cause of the problem is.

There are many, many people who seem to run into this problem. For example:

https://serverfault.com/questions/295285/mysql-cannot-connect-via-localhost-only-127-0-0-1
https://stackoverflow.com/questions/21536034/127-0-0-1-not-working-on-wamp-server
https://stackoverflow.com/questions/31413728/connect-mysql-through-localhost-not-working-but-127-0-0-1-is-working
https://stackoverflow.com/questions/31413728/connect-mysql-through-localhost-not-working-but-127-0-0-1-is-working
https://stackoverflow.com/questions/11663860/mysql-connect-localhost-127-0-0-1-slow-on-windows-platform
https://stackoverflow.com/questions/5239376/mysql-localhost-127-0-0-1-problem
https://serverfault.com/questions/838851/mysql-php-cant-connect-to-localhost-127-0-0-1-working

You can read any of those posts and they all come to the same conclusion: When you use localhost with MySQL, it uses a socket connection. If you use 127.0.0.1, it uses TCP/IP and connects over port 3306 (or whatever port you specify). A socket connection is a special file location where data is "written" by one application while another application "reads" the file as new contents are added. This is a highly efficient mechanism, BUT it requires that both both PHP and MySQL are read/writing to the SAME socket file.

What happens frequently is that PHP and MySQL are not reading/writing to the same place. This can happen on any platform, and it's very common when you combine two different solutions locally. For example, if you use Homebrew Apache and the built-in MacOS MySQL. Or if you use multiple MAMP/WAMP stacks for PHP, but use a common MySQL between them.

The socket location is usually in the my.cnf file for MySQL and will look like this:

[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock

While PHP configures its socket in php.ini like this:

pdo_mysql.default_socket=/var/mysql/mysql.sock

If those two files are pointing at different sockets, things will not work. It also explains the error message "No such file or directory." It means PHP was looking for the socket file, but it wasn't there.

I'm not certain what the best solution is here. Just switching from localhost to 127.0.0.1 would probably have fewer problems, though it would necessitate the minor overhead of using TCP/IP instead of a socket file. Benchmarking has shown that to to be negligible these days.

Showing a better error message would also definitely help.

I'm not entirely sure attempting to fix the connection values and use localhost instead of 127.0.0.1 or vice-versa would be a good thing. Though it is inline with what we are discussing over in #496 (automatically create the database if it does not exist). Anything that reduces the installation friction is a huge boon.

@quicksketch
Copy link
Member

I've combined a fix for this into the PR at backdrop/backdrop#2126. It does the following:

  • Sets the default host to 127.0.0.1. Considering the reports from StackOverflow, this seems to be the most likely working configuration. It also avoids slowness problems with Windows resolving localhost to an IPv6 IP, then falling back to IPv4, as described in https://stackoverflow.com/questions/11663860/mysql-connect-localhost-127-0-0-1-slow-on-windows-platform
  • If 127.0.0.1 or localhost does not work, the installer attempts to use the opposite mechanism.
  • If the connection is successful using the opposite, settings.php is written with the new approach.

This combines with automatically creating the database as discussed in #496.

@quicksketch quicksketch changed the title Can't install with default mysql host=localhost Try both 127.0.0.1 (port) and localhost (socket) when using the installer Apr 14, 2018
@serundeputy serundeputy added this to the 1.10.0 milestone Apr 22, 2018
@serundeputy
Copy link
Member

This is fixed in backdrop/backdrop#2126
Thanks for the report @DrupaListo-com and the fix @quicksketch
Merged into 1.x and will ship with 1.10.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants