When upgrading to PHP 5.4.1 from earlier versions, many developers encounter this specific authentication error when connecting to MySQL databases. The core issue stems from MySQL's transition from the older mysql_old_password
hashing method to the more secure mysql_native_password
protocol.
The PHP 5.4.1 mysql extension was compiled with stronger security defaults that no longer support the legacy authentication method by default. This creates incompatibility when connecting to MySQL servers that still use the old password hashing for certain user accounts.
// Typical error scenario:
$link = mysql_connect('localhost', 'user', 'password');
// Throws: Warning: mysql_connect(): The server requested authentication
// method unknown to the client [mysql_old_password]
Option 1: Reset the user password to use new authentication:
SET old_passwords = 0;
SET PASSWORD FOR 'user'@'localhost' = PASSWORD('newpassword');
Option 2: Recompile PHP with legacy support (not recommended for production):
./configure --with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd \
--with-mysql-socket=/tmp/mysql.sock \
--with-mysql-old-password
For MySQL Server: Upgrade your user accounts to modern authentication:
UPDATE mysql.user SET plugin = 'mysql_native_password'
WHERE User = 'username' AND Host = 'localhost';
FLUSH PRIVILEGES;
For Application Code: Consider migrating to MySQLi or PDO:
// MySQLi example
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// PDO example
try {
$pdo = new PDO("mysql:host=localhost;dbname=database", "user", "password");
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
Verify your current authentication method with:
SELECT user, host, plugin FROM mysql.user;
And check PHP's mysql extension capabilities:
php -i | grep mysql.default_auth
When migrating from PHP 5.3 to 5.4+, many developers encounter this puzzling error during MySQL connections. The root cause lies in MySQL's evolving security protocols and PHP's updated client libraries.
PHP 5.4 introduced more secure client libraries that dropped support for the older mysql_old_password
authentication method. Meanwhile, your MySQL server might still be configured to use this legacy method, especially if:
- Using MySQL versions prior to 4.1
- Server was upgraded without password hash migration
- Specific user accounts still use old-style passwords
Here are three approaches to resolve this:
// Option 1: Update user password hashes (recommended)
ALTER USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword';
// Option 2: Force old password method (temporary fix)
SET old_passwords = 1;
SET PASSWORD FOR 'username'@'localhost' = OLD_PASSWORD('password');
// Option 3: Modify PHP connection (if using ext/mysql)
$link = mysql_connect('localhost', 'user', 'pwd');
mysql_query("SET old_passwords=1", $link);
The proper long-term solution involves upgrading your authentication system:
- Identify affected users:
SELECT User, plugin FROM mysql.user WHERE plugin = 'mysql_old_password';
- Update their authentication:
ALTER USER 'legacyuser'@'%' IDENTIFIED WITH mysql_native_password BY 'newsecurepassword';
- Flush privileges:
FLUSH PRIVILEGES;
Consider migrating to newer APIs:
// PDO example
try {
$pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
// MySQLi example
$mysqli = new mysqli('localhost', 'user', 'pass', 'db');
if ($mysqli->connect_error) {
die('Connect Error: ' . $mysqli->connect_error);
}
For MySQL 5.7+ servers, you may need to modify my.cnf
:
[mysqld]
default-authentication-plugin=mysql_native_password
Remember to restart MySQL after configuration changes.