Updating MySQL Connector for PHP (MySQLi / PDO) to Fix Errors
Update MySQL Client Libraries
Sometimes, outdated MySQL client libraries cause authentication issues (e.g., mysql_native_password or caching_sha2_password). Update these libraries:
For related MySQL connector topics, see MySql.Data 0x80004005, Update MySQL Connector (admin), native_password Access Denied, and MySQL Access Denied (admin).
1. Update the MySQL client:
- Debian/Ubuntu:
sudo apt update && sudo apt install libmysqlclient-dev - CentOS/RHEL:
sudo yum update && sudo yum install mysql-community-libs
2. Restart the web server to apply the updated libraries:
sudo systemctl restart apache2
Update the PHP MySQL Connector
If updating the system packages doesn't resolve the issue, explicitly update the PHP MySQL extension.
1. For PHP mysqli or PDO_MYSQL, reinstall the MySQL extension:
sudo apt install php-mysql
or for CentOS/RHEL:
sudo yum reinstall php-mysqlnd
2. Verify the version:
php -i | grep "mysqlnd"
It should show the latest mysqlnd version compatible with your PHP.
Check Database Connection Script
Ensure that your PHP code uses the correct credentials and connection settings. A modern MySQL server may require specific configurations.
Example: MySQLi
<?php
$host = "localhost";
$username = "myuser";
$password = "mypassword";
$database = "mydb";
$conn = new mysqli($host, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully!";
?>
Example: PDO
<?php
$dsn = "mysql:host=localhost;dbname=mydb;charset=utf8mb4";
$username = "myuser";
$password = "mypassword";
try {
$pdo = new PDO($dsn, $username, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
echo "Connected successfully!";
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
?>
Ensure Correct Authentication Plugin
Newer MySQL servers (8.0+) use caching_sha2_password by default, which older PHP connectors may not support. If this is the issue:
1. Check the authentication plugin for the MySQL user:
SELECT user, host, plugin FROM mysql.user WHERE user = 'myuser';
2. If the plugin is caching_sha2_password, switch to mysql_native_password:
ALTER USER 'myuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mypassword';
FLUSH PRIVILEGES;
Debugging Connection Issues
If the problem persists, enable error reporting in PHP to get more details about the issue:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
Run your PHP script again and note the exact error message.
Final Test
Once everything is updated:
- Run the PHP script to test the connection.
- Ensure the application connects successfully to the MySQL database without any MySqlException.
By following these steps, a programmer can ensure the MySQL connector for PHP (MySQLi or PDO) is up-to-date, compatible with the server, and properly configured.


