W3docs

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:

  1. Make sure that the MySQL server is running and that you are using the correct hostname and port number in your mysqli_connect() call.
  2. 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.
  3. If you are using a firewall or security group to block incoming connections, make sure that the MySQL port (3306 by default) is open.
  4. Make sure that the MySQL server is not being blocked by any other firewall or security software on your server.
  5. If you are using a local MySQL server, make sure that you have not specified the localhost address in your mysqli_connect() call. Instead, use 127.0.0.1 or the actual hostname of your server. On Unix systems, localhost often 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());
}
  1. Verify that the MySQL extension is enabled in your php.ini file (extension=mysqli).
  2. Check the MySQL error log for more specific details when the 2002 error occurs.

I hope this helps! Let me know if you have any other questions.