PHP database connection error
Posted: Tue Feb 19, 2019 7:52 pm
I haven't worked on php pages for some time. When I tried to work on a site it was working before, it failed. From the error log, I saw:
I then checked online and found someone suggesting to replace "localhost" with "127.0.0.1" in the command.
From:
To:
After making the change, it worked. Not sure if this was because of the different PHP versions. I had just upgraded from 5.6.20 to 7.3.1 2 weeks ago. Anyway, here is the reasoning from the post(https://stackoverflow.com/questions/421 ... connect-vi):
Code: Select all
PHP Warning: mysqli_connect(): (HY000/2002): No such file or directory in <PATH>
From:
Code: Select all
$link = mysqli_connect("localhost","<username>","<password>","<DB>");
Code: Select all
$link = mysqli_connect("127.0.0.1","<username>","<password>","<DB>");
Code: Select all
When you use localhost as name, mysql will use a Unix socket instead that is handled through a file entry (mysql.sock, for example), not an Inet one, no matter if localhost is declared in /etc/hosts or not. However, when you use an ip address instead, even 127.0.0.1, you force mysql to open an Inet socket instead.