Database Error
SQLSTATE[HY000] [2002] Connection to MySQL server at 'localhost:3306' failed: unable to verify the first certificate
What This Error Means
This error occurs when the MySQL connection fails due to a certificate verification issue. It usually happens when the server's certificate cannot be verified by the client, which is trying to connect to the server.
Why It Happens
This error is typically caused by one of the following reasons: 1) The MySQL server's certificate is not properly configured or is self-signed, 2) The client's certificate verification settings are too strict, or 3) There's an issue with the server's hostname verification.
How to Fix It
- 1To resolve this issue, you can try the following: 1) Update the MySQL server's certificate to a trusted one, 2) Relax the client's certificate verification settings, or 3) Disable hostname verification on the server. Here's an example of how to update the connection settings in PHP to disable hostname verification:
- 2// Before (broken code)
- 3$dsn = 'mysql:host=localhost;dbname=mydb';
- 4$username = 'myuser';
- 5$password = 'mypassword';
- 6// After (fixed code)
- 7$dsn = 'mysql:host=localhost;dbname=mydb';
- 8$username = 'myuser';
- 9$password = 'mypassword';
- 10$options = [PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false];
- 11$pdo = new PDO($dsn, $username, $password, $options);
Example Code Solution
$dsn = 'mysql:host=localhost;dbname=mydb';
$username = 'myuser';
$password = 'mypassword';$dsn = 'mysql:host=localhost;dbname=mydb';
$username = 'myuser';
$password = 'mypassword';
$options = [PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false];
$pdo = new PDO($dsn, $username, $password, $options);Fix for SQLSTATE[HY000] [2002] Connection to MySQL server at 'localhost:3306' failed: unable to verify the first certificate
Browse Related Clusters
Related SQL Errors
Related SQL Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error