Naresh Chaurasia
Naresh Chaurasia

Reputation: 469

Not Able to connect mysql server running on docker container

I am running sql server in docker container`

docker pull mysql/mysql-server:latest

docker run --name=my_sql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 mysql/mysql-server:latest

docker exec -it my_sql mysql -u root -p

#Create table in mysql running in container.
CREATE DATABASE hello_java CHARACTER SET utf8 COLLATE utf8_general_ci;

All of this is done successfully. Now when I try to connnect to db using Java, I am getting error.

String url = "jdbc:mysql://192.168.56.101:3306/hello_java";
DriverManager.getConnection(url, "root", "1234");
System.out.println("Connection establised....");

192.168.56.101 is the ip of my VM. I am running Linux on VM.

I am getting communications link failure error.

I googled and found out few solutions on following links, but none of the worked for me.

Solving a "communications link failure" with JDBC and MySQL

How to connect with MySQL DB running as container in docker?

I made changes to /etc/my.cnf file, but it does not seem to help.

Thanks.

Upvotes: 1

Views: 3073

Answers (1)

Naresh Chaurasia
Naresh Chaurasia

Reputation: 469

I did some more RnD and found the following solution.

String url = "jdbc:mysql://192.168.56.101:3306/mysql?allowPublicKeyRetrieval=true&useSSL=false";

Adding allowPublicKeyRetrieval=true&useSSL=false to the query helped me to connect with the database.

The following links were useful for solution.

Connection Java-MySql : Public Key Retrieval is not allowed

https://www.tutorialspoint.com/how-to-disable-establishing-ssl-connection-without-server-s-identity-verification-is-not-recommended-warning-when-connecting-to-mysql-database-in-java

Thanks.

Upvotes: 3

Related Questions