简体   繁体   English

通讯链接故障

[英]Communications links failure

I'm using a MacBook Pro with MAMP installed on it. 我使用的是装有MAMP的MacBook Pro。 Everything on the MAMP side is working fine. MAMP方面的所有功能均正常运行。 Also JDBC connector is in it's place. JDBC连接器也在其中。

The problem is that when I try to connect to MySQL through my java code it gives me the following error message: 问题是,当我尝试通过Java代码连接到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 com.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 DatabaseConnection.<init>(DatabaseConnection.java:22)
at Main.main(Main.java:10)
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:432)
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)
... 16 more

And here is my connection statement in my Java code(I checked the credentials as well): 这是我的Java代码中的连接语句(我也检查了凭据):

connection = DriverManager.getConnection("jdbc:mysql:///"+databaseName, userName, password);

This totally drives me crazy because with the same installation and code on my iMac everything works fine. 这完全让我发疯,因为在iMac上安装相同的代码和相同的程序,一切正常。

So what is wrong on my MacBook Pro that's not letting me to connect to MySQL through Java? 那我的MacBook Pro上有什么问题让我无法通过Java连接到MySQL?

The solution to the MAMP, mysql J Connector, Java and "Communications link failure" exception was best resolved by installing a brand new copy of mysql Server. 通过安装全新的mysql服务器副本,可以最好地解决MAMP,mysql J连接器,Java和“通信链接失败”异常的解决方案。

I think by default MAMP version of mysql has the TCP/IP port turned off. 我认为默认情况下,MAMP版本的mysql的TCP / IP端口已关闭。 You can turn it on by running the steps below. 您可以通过执行以下步骤将其打开。 I found however that none of these steps worked for me and that it was much easier installing a brand new copy of mysql server which through the default install has network connections working. 但是我发现这些步骤都不适合我,并且安装全新的mysql服务器副本要容易得多,它通过默认安装即可正常工作。

You can try getting network access for MAMP mysql working with the following steps. 您可以按照以下步骤尝试获取MAMP mysql的网络访问权限。 Launch "MAMP Pro" (none of these functions are available in the free version)": 启动“ MAMP Pro”(免费版本中没有这些功能):

a. 一种。 Go into menu "File"->"Edit Template"->"MySQL my.cnf" and editing the following 2 lines. 进入菜单“文件”->“编辑模板”->“ MySQL my.cnf”,然后编辑以下两行。 Becareful not to change anything else. 注意不要更改其他任何内容。

bind-address = 0.0.0.0
#MAMP_skip-networking_MAMP    

b. b。 Uncheck the "Allow local access only" on the "Server" tab. 取消选中“服务器”选项卡上的“仅允许本地访问”。

After you take these steps run to confirm TCP/IP access: 在执行这些步骤之后,请运行以确认TCP / IP访问:

netstat -tln | netstat -tln | grep mysql grep MySQL的

You'll have to do some diagnostics on the MacBook first. 您必须首先在MacBook上进行一些诊断。
Because the driver is not receiving any packets... 由于驱动程序未收到任何数据包...
That means there's no connectivity. 这意味着没有连接。

Open up a terminal window and do: 打开终端窗口并执行以下操作:

su
--> enter password
mysql
--> does mysql start?
telnet localhost 3306
--> do you get a response?
ps -ef |grep mysql
--> do you see a mysql instance?
less /etc/my.cnf
--> check out the settings, what port number is used?

Just open the MAMP and uncheck 'allow local access only'. 只需打开MAMP,然后取消选中“仅允许本地访问”即可。 as simple as that :D 就这么简单:D

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

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