Getting error mysqli::real_connect(): (HY000/2002): No such file or directory when I try to access my project on live server
MySQL connection driver does not get any meaningful server at the location of localhost. So use,
'hostname' => '127.0.0.1'
rather than
'hostname' => 'localhost'
mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2)
When you use just "localhost" the MySQL client library tries to use a Unix domain socket for the connection instead of a TCP/IP connection. The error is telling you that the socket, called MySQL
, cannot be used to make the connection, probably because it does not exist (error number 2).
From the MySQL Documentation:
On Unix, MySQL programs treat the host name localhost specially, in a
way that is likely different from what you expect compared to other
network-based programs. For connections to localhost, MySQL programs
attempt to connect to the local server by using a Unix socket file.
This occurs even if a --port or -P option is given to specify a port
number. To ensure that the client makes a TCP/IP connection to the
local server, use --host or -h to specify a host name value of
127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by
using the --protocol=TCP option.
There are a few ways to solve this problem.
- You can just use TCP/IP instead of the Unix socket. You would do this by using
127.0.0.1
instead oflocalhost
when you connect. The Unix socket might by faster and safer to use, though. - You can change the socket in
php.ini
: open the MySQL configuration filemy.cnf
to find where MySQL creates the socket, and set PHP'smysqli.default_socket
to that path. On my system it's/var/run/mysqld/mysqld.sock
. Configure the socket directly in the PHP script when opening the connection. For example:
$db = new MySQLi('localhost', 'kamil', '***', '', 0,
'/var/run/mysqld/mysqld.sock')
Warning: mysqli_connect(): (HY000/2002): No such file or directory
Alright, I just found the solution. The problem turned out to be that the host shouldn't have been localhost
, but 127.0.0.1
. I always thought localhost
and 127.0.0.1
was the same, but it turned out to be different.
So maybe as a tip for future users, always use the ip when in doubt.
Related Topics
Change Innerhtml of a PHP Domelement
Replace Values in a Uri Query String
PHP Regex for Validating a Url
Fatal Error: Call to Undefined Function Oci_Connect()
Cascade Dropdown List Using Jquery/Php
Is There Any Difference Between _Dir_ and Dirname(_File_) in PHP
How to Stop Gd2 from Washing Away the Colors Upon Resizing Images
Should I Be Using Assert in My PHP Code
How to Increment Count in the Replacement String When Using Preg_Replace
Simplexml Get Element Content Based on Attribute Value
Fatal Error: Call to Undefined Method MySQLi::Error()
How to Http Post Special Chars in Swift
How to Get the File Path in HTML <Input Type="File"> in PHP
Corresponding Nested Ternary Operator in PHP