mysqli_connect(): (HY000/2002): No connection could be made
This error message typically indicates that there was a problem trying to establish a connection to the MySQL server.
This error message typically indicates that there was a problem trying to establish a connection to the MySQL server. There can be a number of reasons for this error. Here are some things you can try:
- Make sure that the MySQL server is running and that you are using the correct hostname and port number in your
mysqli_connect()call. - If you are trying to connect to the MySQL server from a remote machine, make sure that the MySQL server is configured to allow remote connections.
- If you are using a firewall or security group to block incoming connections, make sure that the MySQL port (3306 by default) is open.
- Make sure that the MySQL server is not being blocked by any other firewall or security software on your server.
- If you are using a local MySQL server, make sure that you have not specified the
localhostaddress in yourmysqli_connect()call. Instead, use127.0.0.1or the actual hostname of your server. On Unix systems,localhostoften triggers a Unix socket connection, which can fail if the socket path is misconfigured.
// Use 127.0.0.1 to force a TCP/IP connection instead of a Unix socket
$link = mysqli_connect('127.0.0.1', 'username', 'password', 'database', 3306);
if (!$link) {
die('Connection failed: ' . mysqli_connect_error());
}- Verify that the MySQL extension is enabled in your
php.inifile (extension=mysqli). - Check the MySQL error log for more specific details when the
2002error occurs.
I hope this helps! Let me know if you have any other questions.