简体   繁体   中英

Unable to connect to MySQL on localhost from a Java program on Ubuntu

Following is the code.

import java.sql.*;

public class ActivityReader {
    public static void main(String[] args) {
                      System.out.println("MySQL Connect Example.");
              Connection conn = null;
              String url = "jdbc:mysql://localhost:3306/";
              String dbName = "jdbctutorial";
              String driver = "com.mysql.jdbc.Driver";
              String userName = "root"; 
              String password = "password";
              try {
                  Class.forName("com.mysql.jdbc.Driver").newInstance();
                  conn = DriverManager.getConnection(url,userName,password);
                  System.out.println("Connected to the database");
                  conn.close();
                  System.out.println("Disconnected from database");
              } catch (Exception e) {
                      e.printStackTrace();
              }
    }

} 

I have already added mysql-connector-java-5.1.20-bin.jar in project from this url . Mysql service is already running.

mysql start/running, process 25326

MySQL Connect Example.

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.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:348)
    at c

om.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2391)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2428)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2213)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:797)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at ActivityReader.main(ActivityReader.java:45)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:218)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)
    ... 15 more

Any idea.... ??????

Connect using 127.0.0.1 instead localhost , maybe a clue of what is happening on your system:

MySQL will try to connect to the unix socket if you tell it to connect to "localhost". If you tell it to connect to 127.0.0.1 you are forcing it to connect to the network socket. So probably you have MySQL configured to only listen to the network socket and not to the file system socket.

https://serverfault.com/questions/295285/mysql-cannot-connect-via-localhost-only-127-0-0-1

try

>telnet localhost 3306

You are supposed to see some server response similar to

"5.5.19☺(:*b2p`p!☻ǧ4Oph7xxsV5C5mysql_native_password"

Most probably problem will be fixed by replacing hostname with ip, check https://serverfault.com/a/90055

I think you are not specified your db name in your connection URL
try this
conn = DriverManager.getConnection(url +"/"+ dbName ,userName,password);
And be sure that you have created the database with same name.

I face the same issue on a VPS linux server. I solve it by replacing localhost or 127.0.0.1 by the real IP address.

If you are trying to run your code on a Linux Machine or Windows Server.

You can change the server from 'localhost' to specific IP 'xx.xx.xx.xx'

and it should work fine.

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