Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

JDBC says Public Key Retrieval is not allowed when connecting to MySQL

ErrorPublic Key Retrieval is not allowed

Short 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.

  • Applies to MySQL 8.4, MySQL Connector/J 8.x, mysql client 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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';
  2. 2Match what you see, then jump to its fix

    1. 1Public 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. 2Public 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. 3ERROR 2061 (HY000) ... Authentication requires secure connection.A command-line client gets the same refusalFix

What does the error look like?

  • Public Key Retrieval is not allowed
  • ERROR 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=DISABLED

2Fix

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=REQUIRED

3Verify

The application connects without any public key option.

SELECT 1;

Confirmed by dev.mysql.com

Did this fix it?

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/app

2Fix

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=DISABLED

3Verify

The connection opens and a simple query runs.

SELECT 1;

Confirmed by dev.mysql.com

Did this fix it?

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.1

2Fix

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-key

3Verify

The prompt opens after the password.

SELECT CURRENT_USER();

Confirmed by dev.mysql.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.