Java Driver.getConnection() 从实时系统上的 mysql 产生“拒绝连接”,而不是开发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/366861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Driver.getConnection() yields "Connection Refused" from mysql on live system, not dev
提问by Olie
Pretty straightforward stuff, here -- I'm just not good enough with mysql to understand what it wants from me.
非常简单的东西,在这里——我只是对 mysql 不够了解它想要我做什么。
I've got a short java test-case that opens a connection on mysql on my dev system but, when I try to put it to my server, it fails.
我有一个简短的 java 测试用例,它在我的开发系统上的 mysql 上打开一个连接,但是当我尝试将它放到我的服务器时,它失败了。
Any help in tracking this down would be most appreciated.
任何帮助追踪此事的帮助将不胜感激。
Thanks!
谢谢!
Test Code
测试代码
import java.util.*;
import java.sql.*;
public class mysqltest {
static public void getDBConnection() {
System.out.println ("Start of getDBConnection.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "magnets_development";
String driver = "com.mysql.jdbc.Driver";
String userName = "*****"; // blanked for publication
String password = "*****";
try {
Class.forName (driver).newInstance();
System.out.println ("driver.newInstance gotten.");
conn = DriverManager.getConnection (url+dbName, userName, password);
System.out.println ("Connection gotten: " + conn + ".");
Statement sql = conn.createStatement ();
ResultSet results = sql.executeQuery ("use " + dbName + ";");
}
catch (Exception ex) {
System.out.println ("*** Got exception.");
ex.printStackTrace();
}
}
public static void main(String args[]) {
System.out.println ("Main started.");
mysqltest.getDBConnection();
}
}
Dev System Output(Expected/correct response)
开发系统输出(预期/正确响应)
olie$ java mysqltest
Main started.
Start of getDBConnection.
Properties set.
driver.newInstance gotten.
Connection gotten: com.mysql.jdbc.Connection@c980c9.
olie$
Server Output(Error I'm trying to track-down)(Some blank lines removed.)
服务器输出(我试图追踪的错误)(删除了一些空行。)
mini$ java mysqltest
Main started.
Start of getDBConnection.
Properties set.
driver.newInstance gotten.
*** Got exception.
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection refused
STACKTRACE:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
at java.net.Socket.connect(Socket.java:520)
at java.net.Socket.connect(Socket.java:470)
at java.net.Socket.<init>(Socket.java:367)
at java.net.Socket.<init>(Socket.java:209)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at mysqltest.getDBConnection(mysqltest.java:34)
at mysqltest.main(mysqltest.java:49)
** END NESTED EXCEPTION **
Last packet sent to the server was 3 ms ago.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at mysqltest.getDBConnection(mysqltest.java:34)
at mysqltest.main(mysqltest.java:49)
mini$
采纳答案by Olie
Bah! My apologies -- I just had "local connections only" set up. I was confused by the fact that the java app was running locally (so seemed a "local connection" but, because it is connecting via TCP, it's a "remote" connection, even though it originates on localhost.
呸! 我很抱歉——我刚刚设置了“仅限本地连接”。我对 java 应用程序在本地运行这一事实感到困惑(所以看起来是“本地连接”,但是,因为它是通过 TCP 连接的,所以它是一个“远程”连接,即使它起源于本地主机。
I will eventually learn how to administer these things. In the mean time, I hope someone else can learn from my mistakes.
我最终将学习如何管理这些事情。同时,我希望其他人可以从我的错误中吸取教训。
Thank you to all who put time into trying to help me. I have since allowed "remote" connections, but all of my user-accounts are 'username'@'localhost" only (i.e., there is no 'username'@'%', which would allow any machine to connect.) Hopefully, I got that part right, at least.
感谢所有花时间帮助我的人。我已经允许“远程”连接,但我所有的用户帐户都是“用户名”@“本地主机”(即,没有“用户名”@“%”,这将允许任何机器连接。)希望,至少我做对了那部分。
Thanks again!
再次感谢!
回答by Milhous
Have you verified that you can connect to the database on the server, with the name and password.
您是否已验证可以使用名称和密码连接到服务器上的数据库。
Also if you are connecting via a url you should not need the following line.
此外,如果您通过 url 连接,则不需要以下行。
properties.setProperty("socket", "/Applications/MAMP/tmp/mysql/mysql.sock");
The above property seems specific to Os X. Are you running the server on os X?
上述属性似乎特定于 OS X。您是否在 os X 上运行服务器?
回答by MetroidFan2002
Your URL has localhost in it. Are you trying to connect to this server on the network? Your URL may be incorrect.
您的 URL 中包含 localhost。您是否正在尝试连接到网络上的此服务器?您的网址可能不正确。
回答by Uri
Also, make sure that your MySQL server's user account for the database user allows connections from other addresses but localhost. For security reasons in smaller servers it is possible (and possibly default?) to limit connections to localhost, so that only the local web application can access the database using the magic-numbered user/password combination.
此外,请确保您的 MySQL 服务器的数据库用户用户帐户允许来自除 localhost 之外的其他地址的连接。出于安全原因,在较小的服务器中,可以(并且可能是默认设置?)限制与本地主机的连接,以便只有本地 Web 应用程序可以使用幻数用户/密码组合访问数据库。
回答by Yoni Roit
There's only one reason for "connection refused" message, never mind if it's mysql or any other service. The server isn't listening. Try "netstat -an" and grep for port 3306 to validate it.
“连接被拒绝”消息只有一个原因,不管是 mysql 还是任何其他服务。服务器不听。尝试“netstat -an”和 grep 端口 3306 以验证它。
回答by Milhous
Here is my code that works on mysql on os X
这是我在 os X 上运行 mysql 的代码
public SQL(String host, String port, String database, String userid, String password)
{
queryType = QUERYTYPE.Single;
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://" + host;
if (port != null)
{
url += ":" + port;
}
else
{
url += ":" + defaultPort;
}
url += "/" + database;
try
{
Class.forName(driver);
connection = DriverManager.getConnection(url, userid, password);
}
catch (Exception e)
{
e.printStackTrace();
}
}
回答by Patrick Smith
Just a helpful note on this subject. I spent at least 30 minutes trying to debug the same error and this was my mistake.
只是关于这个主题的有用说明。我花了至少 30 分钟试图调试相同的错误,这是我的错误。
In my MySQL conf file I happened to set the BIND ADDRto the IP of the machine. I did not set it to 127.0.0.1
, or 0.0.0.0
. So MySQL would accept JDBC connections from outside, but not internally. I was getting Connection Refused only when connecting from the local machine.
在我的 MySQL conf 文件中,我碰巧将BIND ADDR设置为机器的 IP。我没有将其设置为127.0.0.1
, 或0.0.0.0
。所以 MySQL 会接受来自外部的 JDBC 连接,而不是内部。仅当从本地计算机连接时,我才收到连接被拒绝。
Changing that address to 0.0.0.0
allowed it to listen for requests to any IP. This allowed for both internal and external IPs.
更改该地址以0.0.0.0
允许它侦听对任何 IP 的请求。这允许内部和外部 IP。
Hopefully this helps somebody who made the same dumb mistake I did. Check it with the above comment about netstat -an | grep 3306
.
希望这能帮助那些犯了和我一样愚蠢错误的人。检查上面关于 的评论netstat -an | grep 3306
。
回答by asad raza
/* answer date is Expire but my answer is for Future */
/* 回答日期是过期但我的回答是针对未来 */
I have the same problem.
我也有同样的问题。
How can i solve it?
我该如何解决?
Answer:
回答:
Open your Xammp or other server, and just run MYSQL... . If SQL is not running that Exception will throw.
打开您的 Xammp 或其他服务器,然后运行 MYSQL... 。如果 SQL 未运行,则该异常将抛出。