MySQL PHP Incompatibility: The Auth Plugin Trap
Connecting to a legacy MySQL server running version 5.0.22 with modern PHP 5.3.0 often results in cryptic connection warnings. You might encounter an error stating that the "OK packet is shorter than expected," followed by a specific note from mysqlnd (MySQL Native Driver) claiming it cannot connect using old authentication. This isn't just a configuration mismatch; it is a fundamental clash between how your PHP client negotiates credentials and how the older server expects to validate them.
The core issue lies in the evolution of the MySQL authentication plugin system. Older versions of MySQL, such as 5.0.x, rely on the "old_password" format or mysql_native_password with specific hashing algorithms that differ from modern defaults. When PHP 5.3.0 initializes the mysql_connect() function (or its mysqli equivalent), it attempts to handshake using the authentication plugin capabilities available in the driver version bundled with PHP 5.3.
The error message mysqlnd cannot connect to MySQL 4.1+ using old authentication is technically accurate but often misleading if you are not aware of the underlying protocol change. The server sends a packet indicating it supports the new authentication method, but the client (PHP) tries to authenticate using the legacy hash stored in the user table. If the server expects a specific length for the response packet based on the plugin version, and PHP sends a truncated or differently structured response due to the plugin mismatch, you get the "OK packet 6 bytes shorter than expected" warning.
The practical implication is clear: your code is trying to speak a dialect that the older MySQL instance no longer understands or rejects. The fact that the same database works perfectly on a newer server (5.0.45) confirms that the issue is strictly client-side version incompatibility, not a database corruption problem. The newer server likely has updated plugin handlers that can gracefully handle the legacy hash formats, whereas the 5.0.22 instance enforces stricter packet validation for its specific authentication flow.
Why Downgrading PHP Works
The update noting that reverting to PHP 5.2.* resolves the issue highlights a significant shift in how PHP handled MySQL connections between these minor versions. In PHP 5.3, the mysqlnd driver became more aggressive about enforcing modern authentication standards and stricter packet size checks. While this is a security improvement for production environments facing current threats, it breaks backward compatibility with legacy servers that have not migrated their user tables to the new caching_sha2_password or updated mysql_native_password formats.
If you are forced to maintain connectivity to a 5.0.22 database while running PHP 5.3.0+, you face a binary choice: downgrade your PHP version or migrate your authentication plugin. Downgrading is the path of least resistance for legacy integrations, as it restores the loose handshake behavior expected by older MySQL instances. However, this introduces its own security trade-offs, as PHP 5.2 reached end-of-life long ago and lacks patches for known vulnerabilities.
The Migration Path
The most robust solution, rather than locking yourself to an outdated PHP version, is to upgrade the user authentication plugins on the MySQL 5.0.22 server. If you have administrative access, you can force the user table entries to use a plugin format that the newer PHP driver understands.
You would need to run SQL commands similar to the following:
ALTER USER 'your_user'@'localhost' IDENTIFIED BY PASSWORD '*new_hash_here' PLUGIN mysql_native_password;
FLUSH PRIVILEGES;
However, generating a valid hash for an old MySQL version requires care. You cannot simply use PASSWORD('newpassword') in a modern client because that function generates hashes for the current server version, which the 5.0.22 instance might reject if its plugin logic is strict. Instead, you must generate the legacy hash format compatible with the specific MySQL 5.0.22 build.
Alternatively, if you are using Laravel or a modern PHP framework, switching from the deprecated mysql_* functions to mysqli or PDO provides better control over connection parameters. You can explicitly set the authentication plugin via the connection string:
$dsn = 'mysql:host=localhost;dbname=legacy_db;charset=utf8';
$pdo = new PDO($dsn, $user, $pass, [
PDO::MYSQL_ATTR_AUTH_PLUGIN => 'mysql_native_password'
]);
This explicitly tells the driver to use the native password plugin regardless of the server's default expectation, bypassing the handshake confusion. While this is a workaround, it underscores that the root cause is the lack of compatibility between the PHP 5.3.0 security model and the MySQL 5.0.22 legacy model.
Final Considerations
If you are maintaining a stack where you cannot upgrade the database server, downgrading PHP to 5.2.* remains the only viable option for seamless connectivity without code changes. However, this should be treated as a temporary measure. The "weirdness" described in the source material is not an anomaly; it is a documented incompatibility resulting from the transition of MySQL authentication protocols.
For developers managing heterogeneous environments, always verify that the client PHP version's mysqlnd driver expectations align with the server's plugin capabilities. If you must connect to legacy 5.0.x instances, ensure your connection code explicitly handles the authentication method or accepts a lower PHP version in your deployment constraints. Avoid relying on generic "it works" behaviors without understanding the handshake protocol details, as packet size mismatches can silently break connections before any data is exchanged.