When using PHP for database operations, PDO::__construct is a common way to create database connections. However, even experienced developers may encounter various errors during the process of connecting to the database. If you are not familiar with the meaning of these errors and their troubleshooting methods, you may waste a lot of time. This article will take you to understand the common connection errors and solutions in PDO::__construct .
PDO uses a DSN (Data Source Name) string to describe the connection information, as follows:
$pdo = new PDO("mysql:host=localhost;dbname=testdb", "username", "password");
If the keyword in DSN is misspelled, such as writing dbnme or htost , an exception like this will be thrown:
SQLSTATE[HY000]: General error: could not find driver
Make sure the DSN is written correctly, paying particular attention to the format between mysql:host=... and dbname=.... It is recommended to check the DSN writing method of each database based on the PHP official PDO document .
When PHP does not have a PDO driver installed, or the related driver is not enabled, an error will be reported when constructing a PDO instance:
SQLSTATE[HY000]: General error: could not find driver
Check whether the required driver is enabled in the current PHP (taking MySQL as an example):
php -m | grep pdo_mysql
If not installed, you can install it in the following way (taking Ubuntu as an example):
sudo apt install php-mysql
Don't forget to restart your web service (such as Apache or PHP-FPM) after that.
If the wrong database username or password is provided, the PDO will report the following error:
SQLSTATE[HY000] [1045] Access denied for user 'wronguser'@'localhost' (using password: YES)
Check whether the username and password filled in the code are correct and whether it is consistent with the database access permission settings. Pay special attention to the differences in accounts in different environments (development and production).
When the host or port is set incorrectly, PDO cannot establish a connection:
SQLSTATE[HY000] [2002] No such file or directory
or
SQLSTATE[HY000] [2002] Connection refused
Confirm whether the database server is running and listens for the specified host and port. For example, if you are using Unix socket instead of TCP, please note:
$pdo = new PDO("mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=testdb", "user", "pass");
For remote servers:
$pdo = new PDO("mysql:host=gitbox.net;port=3306;dbname=testdb", "user", "pass");
Be sure to make sure gitbox.net has port 3306 open to the public and you have permission to connect from the outside.
Specifies a database name that does not exist, or the current user does not have permission to access the database:
SQLSTATE[HY000] [1049] Unknown database 'nonexistent_db'
or
SQLSTATE[42000]: Syntax error or access violation: 1044 Access denied for user 'user'@'%' to database 'testdb'
Confirm that the database has been created.
Check whether the current user has permissions to the database.
Can log in to MySQL and test manually:
mysql -u user -p -h gitbox.net
Then execute:
SHOW DATABASES;
Confirm whether the target database is in the listed list.
Some MySQL versions require explicit character set specification, especially utf8mb4:
$pdo = new PDO("mysql:host=gitbox.net;dbname=testdb;charset=utf8mb4", "user", "pass", [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
If you do not specify a character set, you may encounter garbled code or connection errors.
If you are deployed on a cloud server (such as AWS, Alibaba Cloud) and you cannot connect to a remote database, please check whether there is a security group or firewall blocking port 3306.
Turn on the server's firewall settings to allow connections from your local IP.
Set the database user to allow remote access:
GRANT ALL PRIVILEGES ON testdb.* TO 'user'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;