In an attempt to connect to a MySQL database with Java, you may encounter an exception: _Public Key Retrieval is not allowed_. This issue arises due to a security measure implemented when using the MySQL connector.
To address this issue, you need to enable public key retrieval by adding the following option to your MySQL connection string:
allowPublicKeyRetrieval=true
This option allows the client to automatically request the public key from the server, resolving the "Public Key Retrieval is not allowed" exception. However, it's important to note that enabling public key retrieval could potentially allow a malicious proxy to retrieve your password. Hence, it is recommended to use this option only for testing or development purposes.
Including the allowPublicKeyRetrieval option, your connection string would look something like this:
jdbc:mysql://localhost:3306/db?allowPublicKeyRetrieval=true&useSSL=false
Additionally, for testing/development purposes, you could also set useSSL=false to disable SSL encryption.
<code class="java">import com.mysql.cj.jdbc.MysqlDataSource; ... MysqlDataSource dataSource = new MysqlDataSource(); dataSource.setUseSSL(false); ...</code>
By implementing these adjustments to your connection string and disabling SSL for testing, you can overcome the "Public Key Retrieval is not allowed" exception and successfully connect to your MySQL database.
The above is the detailed content of Why Am I Getting a \'Public Key Retrieval is not allowed\' Error When Connecting to My MySQL Database?. For more information, please follow other related articles on the PHP Chinese website!