1

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.

Naresh Chaurasia
  • 419
  • 5
  • 21
  • So you have your host OS, and inside that you have your Linux VM (192.168.56.101) and inside that you have your MySQL docker container, right? If you ssh into your Linux VM, can you connect from the Linux VM to MySQL, using the mysql command-line client? e.g. `mysql -u root -h 127.0.0.1 -p`. If so, can you connect from your host OS into the Linux VM using a mysql client (CLI or GUI) to make sure that port 3306 is open to the VM? – antun Jun 09 '20 at 05:11
  • The port is open. I am able to ```telnet 192.168.56.101 3306``` from my host OS. 192.168.56.101 is IP of my VM. – Naresh Chaurasia Jun 09 '20 at 05:17

1 Answers1

3

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.

Naresh Chaurasia
  • 419
  • 5
  • 21