简体   繁体   中英

Unable to connect MYSQL database from a Java programm in Ubuntu 16.04 (Communications link failure)

My java program is working fine when I use localhot instead of IP address, but getting error when I connect using IP address. (I have to use IP address to connect, because it is a client server application).

I have also changed this /etc/mysql/mysql.conf.d/mysqld.cnf file bind address from 127.0.0.1 to 0.0.0.0, But again I get the error message like -

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

My code is:-

try{
      Connection con;
      Statement st;
      Class.forName("com.mysql.jdbc.Driver");
      con=DriverManager.getConnection(
      "jdbc:mysql://172.16.16.185:3306/itcentre?
       verifyServerCertificate=false&useSSL=true","root","");

    st=con.createStatement();
    ResultSet rs=st.executeQuery("select * from teacher");
    while(rs.next())
    {
        System.out.println(rs.getString(2));
    }
    rs.close();
    st.close();
    con.close();
 }
 catch(Exception e)
 {
    System.out.println(e);
 }

Java can definitely establish an SSL connection without a client validating the certificate chain of the server. The classes that are establishing the connection (javax.net.ssl classes) would normally treat the unverified server certificate with suspicion and would fail the handshake. But they provide a way for the user's of those classes to in effect say "It's ok if the server's certificate doesn't validate, go ahead and establish the connection". That is what's happening when you say verifyServerCertificate=false.

Thanks to everyone to help me. After struggling to many other issues, finally I have the luck with this solution. I logged into my mysql database using phpmyadmin and then

User Accounts ---> login information ---> and changed Host name from localhost to any host.

See the screen shot - 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM