简体   繁体   English

无法从 Ubuntu 上的 Java 程序连接到本地主机上的 MySQL

[英]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 .我已经从这个 url在项目中添加了 mysql-connector-java-5.1.20-bin.jar 。 Mysql service is already running. Mysql 服务已经在运行。

mysql start/running, process 25326

MySQL Connect Example. MySQL 连接示例。

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:使用127.0.0.1而不是localhost连接,这可能是您系统上发生的事情的线索:

MySQL will try to connect to the unix socket if you tell it to connect to "localhost".如果您告诉它连接到“localhost”,MySQL 将尝试连接到 unix 套接字。 If you tell it to connect to 127.0.0.1 you are forcing it to connect to the network socket.如果你告诉它连接到 127.0.0.1 你是在强迫它连接到网络套接字。 So probably you have MySQL configured to only listen to the network socket and not to the file system socket.因此,您可能将 MySQL 配置为仅侦听网络套接字而不是文件系统套接字。

https://serverfault.com/questions/295285/mysql-cannot-connect-via-localhost-only-127-0-0-1 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" "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最有可能的问题将通过用 ip 替换主机名来解决,检查https://serverfault.com/a/90055

I think you are not specified your db name in your connection URL我认为您没有在连接 URL 中指定您的数据库名称
try this试试这个
conn = DriverManager.getConnection(url +"/"+ dbName ,userName,password); 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.我在 VPS linux 服务器上遇到了同样的问题。 I solve it by replacing localhost or 127.0.0.1 by the real IP address.我通过将 localhost 或 127.0.0.1 替换为真实 IP 地址来解决它。

If you are trying to run your code on a Linux Machine or Windows Server.如果您尝试在 Linux 机器或 Windows 服务器上运行您的代码。

You can change the server from 'localhost' to specific IP 'xx.xx.xx.xx'您可以将服务器从'localhost' to specific IP 'xx.xx.xx.xx'更改'localhost' to specific IP 'xx.xx.xx.xx'

and it should work fine.它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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