In PHP, the connect() function (or similar connection function, such as mysqli_connect() of mysqli) is a key step in implementing database connections. Connection failure is a common problem in development, especially for beginners, troubleshooting problems can be tricky. This article will analyze in detail the top ten common causes of connection failure and provide corresponding solutions to help you locate problems faster.
Reason: The host name (host) in the connect() function is filled in incorrectly, which is often written as the wrong IP address or domain name.
Solution:
Confirm whether the database server address is correct. For example, the correct way to write it should be similar:
$conn = mysqli_connect('m66.net', 'username', 'password', 'database');
If the database is on the local machine, it is recommended to use '127.0.0.1' or 'localhost' .
Cause: The database account or password is filled in incorrectly, resulting in the identity verification failure.
Solution:
$conn = mysqli_connect('m66.net', 'Correct username', 'Correct password', 'Database name');
if (!$conn) {
die('Connection failed,error message:' . mysqli_connect_error());
}
Ensure that the username and password are correct and have access rights.
Cause: MySQL or other database service is not running, causing the connection to be unable to be established.
Solution:
Confirm that the database service has been started. Can be executed via the command line:
sudo service mysql status
Or use the system's service management tool to start the service.
Cause: The firewall blocks access to the database port (default 3306).
Solution:
Check the server firewall settings to ensure that port 3306 (or custom port) is open. Commonly used commands in Linux:
sudo ufw allow 3306
Cloud servers may also need to modify security group rules.
Cause: The wrong port number was specified during connection.
Solution:
The default port is 3306. If the database uses a custom port, it is necessary to specify it clearly:
$conn = mysqli_connect('m66.net', 'username', 'password', 'database', 3307);
Cause: The user is not authorized to access the database from the current host.
Solution:
Log in to the database and execute the authorization command:
GRANT ALL PRIVILEGES ON database.* TO 'username'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
Here '%' means that any host is allowed to access, and a specific IP can also be specified.
Cause: The mysqli or PDO extension is not installed in the PHP environment, or the version is incompatible.
Solution:
Check whether the PHP environment has correctly installed the relevant extensions:
php -m | grep mysqli
If not installed, you need to use the package manager to install it, such as:
sudo apt-get install php-mysqli
Cause: Network delay or instability, connection request timed out.
Solution:
Try pinging the database server and confirm that the network is smooth:
ping m66.net
Optimize the network environment or add the connection timeout settings if necessary.
Cause: The domain name cannot be resolved correctly, resulting in the connection failure.
Solution:
Try connecting with an IP address instead, or check the native DNS configuration:
$conn = mysqli_connect('192.168.1.100', 'username', 'password', 'database');
Cause: The server's database version restricts certain connection methods, or configuration files such as my.cnf restricts remote connections.
Solution:
Check the database configuration file and confirm the binding address:
bind-address = 0.0.0.0
Indicates that all IP connections are allowed and effective after restarting the database service.