JDBC says Public Key Retrieval is not allowed when connecting to MySQL
Public Key Retrieval is not allowedShort answer
The account uses caching_sha2_password and your connection is not encrypted, so the client needs the server's RSA public key to send the password. By default Connector/J refuses to fetch it. Best fix: connect over TLS. Otherwise set allowPublicKeyRetrieval=true, or give the client a trusted copy of the key.
Fix it fast
1Find out which cause you have
Find out which plugin the account uses. If it says caching_sha2_password (the default plugin in MySQL 8.4), the causes below apply.
SELECT user, host, plugin FROM mysql.user WHERE user = 'appuser';2Match what you see, then jump to its fix
- 1
Public Key Retrieval is not allowed, with a JDBC URL that has no TLS setting or with sslMode=DISABLEDThe connection is not encrypted, so the password needs the RSA keyFix - 2
Public Key Retrieval is not allowed on a network you control, with no TLS on the serverTLS is not available, and the driver may not request the public keyFix - 3
ERROR 2061 (HY000) ... Authentication requires secure connection.A command-line client gets the same refusalFix
- 1
What does the error look like?
Public Key Retrieval is not allowedERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
Cause 1 of 3
The connection is not encrypted, so the password needs the RSA key
1Check
For caching_sha2_password the MySQL manual says you must use either a secure connection or an unencrypted one that exchanges the password with an RSA key pair. With TLS, no key pair is used. Check whether the JDBC URL turns TLS off.
jdbc:mysql://db.example.com:3306/app?sslMode=DISABLED2Fix
Use TLS. Connector/J takes an sslMode value (DISABLED, PREFERRED, REQUIRED, VERIFY_CA, VERIFY_IDENTITY). Require it, and make sure the server has TLS set up.
jdbc:mysql://db.example.com:3306/app?sslMode=REQUIRED3Verify
The application connects without any public key option.
SELECT 1;Confirmed by dev.mysql.com
Cause 2 of 3
TLS is not available, and the driver may not request the public key
1Check
Connector/J's allowPublicKeyRetrieval property lets the driver request the RSA public key from the server during the handshake. It defaults to false. Requesting the key from the server is convenient, but the MySQL manual says a trusted local copy is more secure.
jdbc:mysql://db.example.com:3306/app2Fix
Only on a network you trust, such as a private Docker network or a local test database, turn the property on.
jdbc:mysql://127.0.0.1:3306/app?allowPublicKeyRetrieval=true&sslMode=DISABLED3Verify
The connection opens and a simple query runs.
SELECT 1;Confirmed by dev.mysql.com
Cause 3 of 3
A command-line client gets the same refusal
1Check
The mysql client does not request the key unless asked. Per the manual, --get-server-public-key asks the server for it, and --server-public-key-path points at a local copy, which wins if both are given.
mysql --ssl-mode=DISABLED -u appuser -p -h 127.0.0.12Fix
Prefer a trusted key file. Use --get-server-public-key only where the network is secure.
mysql --ssl-mode=DISABLED -u appuser -p -h 127.0.0.1 --server-public-key-path=/path/to/public_key.pem
mysql --ssl-mode=DISABLED -u appuser -p -h 127.0.0.1 --get-server-public-key3Verify
The prompt opens after the password.
SELECT CURRENT_USER();Confirmed by dev.mysql.com
How do I stop it coming back?
- Turn TLS on for every connection that crosses a network, so the password never needs the RSA exchange.
- Keep allowPublicKeyRetrieval=true for local and test setups only.
- If you use a key file, copy it from the server through a trusted channel and keep it with the app configuration.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
A client says it cannot load the caching_sha2_password plugin
A client or connector built before MySQL 8.0 cannot connect to an account that uses caching_sha2_password
MySQL 8 and 8.4 create accounts with the caching_sha2_password plugin.
3 causes, each with a fixDatabases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fixDatabases
A query on Aurora MySQL dies with 'table /rdsdbdata/tmp/#sql... is full'
ERROR 1114 (HY000): The table '/rdsdbdata/tmp/#sqlfd_1586_2' is full
In Aurora MySQL 3, internal temporary tables share one pool, and when that pool and the memory-mapped overflow file are used up, a reader instance ends the query with error 1114.
3 causes, each with a fixDatabases
Azure Database for MySQL refuses a connection that does not use SSL
ERROR 3159 (HY000): Connections using insecure transport are prohibited while --require_secure_transport=ON.
Azure Database for MySQL Flexible Server requires encrypted connections by default (require_secure_transport = ON, TLS 1.2).
4 causes, each with a fixDatabases
Compiling a C or C++ program that includes mysql.h fails
undefined reference to `mysql_init'
The compiler only finds mysql.h when the MySQL client development files are installed and their folder is on the include path.
3 causes, each with a fixDatabases
DBeaver fails to resolve Maven dependencies when downloading a database driver
A driver library does not download automatically
DBeaver downloads driver jars from a Maven repository.
3 causes, each with a fix