Node.js Unable to Authenticate to MySQL 8.0
In Node.js applications, connecting to MySQL databases has been met with difficulties after upgrading to MySQL 8.0. This issue stems from the root account using the new caching_sha2_password hashing method.
Authentication Mechanism Differences
MySQL 8.0 introduced a new default authentication plugin, caching_sha2_password, while MySQL 5.7 employed mysql_native_password. Unfortunately, current Node.js drivers for MySQL lack support for compatible client-side authentication mechanisms.
Workarounds to Establish Connection
One workaround involves altering the root user account to utilize the older mysql_native_password plugin:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'MyNewPass';
Alternatively, you could create a new user with the same authentication plugin:
CREATE USER 'foo'@'localhost' IDENTIFIED WITH mysql_native_password BY 'bar';
Alternative Connector
The official MySQL Node.js connector, based on the X Protocol, supports the new authentication mode and provides a viable alternative.
Pull Request for Resolution
A pull request is currently in progress to address this issue within the community MySQL drivers.
Conclusion
Node.js applications can successfully connect to MySQL 8.0 by either using the workarounds mentioned above or employing the official MySQL Node.js connector, which offers native support for the new authentication mechanism.
The above is the detailed content of How Can I Solve Node.js Authentication Problems with MySQL 8.0?. For more information, please follow other related articles on the PHP Chinese website!