无法从Ubuntu上的Java程序连接到localhost上的MySQL
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法从Ubuntu上的Java程序连接到localhost上的MySQL相关的知识,希望对你有一定的参考价值。
以下是代码。
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();
}
}
}
我已经在this url的项目中添加了mysql-connector-java-5.1.20-bin.jar。 Mysql服务已经在运行。
mysql start/running, process 25326
MySQL Connect示例。
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
任何的想法.... ??????
使用127.0.0.1
而不是localhost
连接,可能是您系统上发生的事情的线索:
如果你告诉它连接到“localhost”,MySQL将尝试连接到unix套接字。如果您告诉它连接到127.0.0.1,则强制它连接到网络套接字。因此,您可能已将MySQL配置为仅侦听网络套接字而不是文件系统套接字。
https://serverfault.com/questions/295285/mysql-cannot-connect-via-localhost-only-127-0-0-1
尝试
>telnet localhost 3306
您应该会看到类似的服务器响应
“5.5.19☺(:* b2p`p☻ǧ4Oph7xxsV5C5mysql_native_password”
最可能的问题将通过用ip替换hostname来修复,检查https://serverfault.com/a/90055
我认为您没有在连接URL中指定您的数据库名称 试试这个 conn = DriverManager.getConnection(url +“/”+ dbName,userName,password); 并确保您已创建具有相同名称的数据库。
我在VPS linux服务器上面临同样的问题。我通过用真实IP地址替换localhost或127.0.0.1来解决它。
以上是关于无法从Ubuntu上的Java程序连接到localhost上的MySQL的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Ubuntu 14.04 上的 mod_jk 无法连接到 tomcat
azure ubuntu 16.04 VM 无法连接到 azure 上的数据库