简体   繁体   中英

Communications links failure

I'm using a MacBook Pro with MAMP installed on it. Everything on the MAMP side is working fine. Also JDBC connector is in it's place.

The problem is that when I try to connect to MySQL through my java code it gives me the following error message:

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):

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.

So what is wrong on my MacBook Pro that's not letting me to connect to MySQL through Java?

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.

I think by default MAMP version of mysql has the TCP/IP port turned off. 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.

You can try getting network access for MAMP mysql working with the following steps. Launch "MAMP Pro" (none of these functions are available in the free version)":

a. Go into menu "File"->"Edit Template"->"MySQL my.cnf" and editing the following 2 lines. Becareful not to change anything else.

bind-address = 0.0.0.0
#MAMP_skip-networking_MAMP    

b. Uncheck the "Allow local access only" on the "Server" tab.

After you take these steps run to confirm TCP/IP access:

netstat -tln | grep mysql

You'll have to do some diagnostics on the MacBook first.
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'. as simple as that :D

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