Cannot connect to mysql server from java

I get a "Communications link failure" error in this line of code:

 mySqlCon = DriverManager.getConnection("jdbc:mysql://**server ip address**:3306/db-name", "mu-user-name", "my-password"); 

I checked everything in this post :

  • I increased the max-allowed-package in my.cnf to etc / mysql: max_allowed_packet = 5073741824 ------ [mysqldump] max_allowed_packet = 1G
  • Binding Address: 127.0.0.1
  • All timeout values ​​are equal to the number
  • Tomcat is not yet installed on the server (new server)
  • There are no skip networks in my.cnf
  • I can execute a ping server
  • I am connected to mysql database via ssh

When I change the query string to this:

 mySqlCon = DriverManager.getConnection("jdbc:mysql://**server ip address**:22/127.0.0.1:3306/db-name", "mu-user-name", "my-password"); 

I get the error message Packet for query is too large (4739923 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable. Packet for query is too large (4739923 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.

So far I have resized the package to my.cnf and restarted the mysql service after that.

Any suggestions?

Note:

I can connect via ssh with this code, but this method does not seem rational! I can connect once basically, and then I have to pass the connection to all classes.

 public my-class-constructor() { try { go(); } catch (Exception e) { e.printStackTrace(); } mySqlCon = null; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://" + rhost + ":" + lport + "/"; String db = "my-db-name"; String dbUser = "dbuser"; String dbPasswd = "pass"; try { Class.forName(driver); mySqlCon = DriverManager.getConnection(url + db, dbUser, dbPasswd); } catch (Exception e) { e.printStackTrace(); } } public static void go() { String user = "ssh-user"; String password = "ssh-pass"; String host = "ips-address"; int port = 22; try { JSch jsch = new JSch(); Session session = jsch.getSession(user, host, port); lport = 4321; rhost = "localhost"; rport = 3306; session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); System.out.println("Establishing Connection..."); session.connect(); int assinged_port = session.setPortForwardingL(lport, rhost, rport); System.out.println("localhost:" + assinged_port + " -> " + rhost + ":" + rport); } catch (Exception e) { System.err.print(e); e.printStackTrace(); } } 
+5
source share
2 answers

I changed the binding address in the my.cnf file in the / etc / mysql file to the server ip address and solved the problem.

0
source

Take a look here . The following describes your case.

The largest package that can be transferred to or from MySQL 5.7 server or client is 1 GB.

When a MySQL client or mysqld server receives a package larger than max_allowed_packet, it throws an ER_NET_PACKET_TOO_LARGE error and closes the connection. With some clients, you can also get a connection to the MySQL server during a query error if the connection packet is too large.

Both the client and the server have their own max_allowed_packet variable, so if you want to process large packets, you must increase this both on the client and on the server.

So it looks like you need to change max_allowed_packet on the client:

 mySqlCon = DriverManager.getConnection("jdbc:mysql://**server ip address**:3306/db-name?max_allowed_packet= 5073741824", "mu-user-name", "my-password"); 
+3
source

All Articles