Connect to the MySQL server from another PC (from java application) - java

I realized an java application that connects to MySQL.
when i make my ip adress of my pc in the navigator of another pc in same local network ,I access to phpMyadmin and Wamp.
When i make 127.0.0.1 on the same pc (Wamp install) I connect to 127.0.0.1 from my java application and it works .
but the problem when I put my ip address in my java application either from my pc or from another pc in same local network , I can not manage to connect .
this is my file of configuration in the java application:
db.user=root
db.password=
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://192.168.1.59:3306/testfournisseur
Is there a manipulation to do?
This is the errors appear when i connect to the database:
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 1 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:666)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1069)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
... 72 more
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2431)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:590)
... 74 more

I'm unfamiliar with MySQL on Windows but the most common reason I see for being unable to connect is that the bind-address in my.cnf is set to 127.0.0.1, if you change this to 0.0.0.0 or another accessible address then it should work fine.
I also noticed you specify port 80 in your properties example, MySQL runs on 3306 by default so changing the port to that (or omitting it) may work.
Edit: Windows' built-in firewall may be blocking the connection so it's definitely worth checking out.

Related

Java exception connecting to MySQL

SOLVED
The latest version of MySQL JDBC driver evidently attempts to create an SSL connection. The app which WORKED did NOT define javax.net.ssl.keystore (and other) values, so (I assume) it reverts to a non-SSL connection. The app which FAILED did define all of the keystore/certificate parameters and then the connection attempt failed - still not sure of exactly why, if someone could enlighten would appreciate. The exception thrown could also be more informative.
Anyway, adding "&useSSL=false&allowPublicKeyRetrieval=true" to the connection parameters works for both applications. (Although not a very good setup for any production app, can use for this POC!)
============= Original Posting Follows =========
This has had me stumped for a few days! AWS server (86_64), Fedora 29, Java 8 (1.8.0_201-b09), MySQL - all upgraded to the latest release. MySQL is running on the same server. Driver: com.mysql.cj.jdbc.Driver
Connection params: jdbc:mysql://172.31.10.222:3306?user=xxxx&password=xxxx. (IP is the AWS private IP for this server.)
Have 2 different utilities - separate 'main' routines, but all in the same JAR file. The first one works perfectly, the second one aborts with the stack pasted below. This is consistent over dozens of trials with minor variations.
The different main routines both use the 'connectToDB' method shown in the stack trace. Instrumented this to log all parameters and the class name of the driver in both cases and verified that they are identical.
Have already tried/verified:
Multiple Google searches of the same error
MySql is running and OK (connect thru another terminal window w no problem),
Same JAR used with all local code
Classpath is identical
Running under the same Linux user, using the same MySQL user/password
Verified the IP above (172.31.10.222)
Also changed the IP to 'localhost', 127.0.0.1, and a symbolic name from /etc/hosts
Added -Xmx2G to make sure not an OOM issue
These are 2 different 'main' methods, but don't do much more than load some properties (from the same property files) before trying to connect to the DB. Then they execute the same connectToDB routine.
Something in the context must be different! Hoping that someone here can at least provide some hints on where to look!
Thanks
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
at spc.tools.spcConnection.connectToDB(spcConnection.java:692)
at spc.tools.spcConnection.connectToLocalDB(spcConnection.java:1418)
at spc.tools.spcConnection.loadAllConnectionsOK(spcConnection.java:1112)
at spc.tools.spcConnection.initOneTime(spcConnection.java:1012)
at spc.tools.spcConnection.init(spcConnection.java:194)
at spc.ops.spcServerBase.environmentOK(spcServerBase.java:367)
at spc.ops.spcServerBase.environmentOK(spcServerBase.java:342)
at spc.run.daemon.spcDaemon.main(spcDaemon.java:1082)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
at com.mysql.cj.protocol.a.NativeProtocol.negotiateSSLConnection(NativeProtocol.java:351)
at com.mysql.cj.protocol.a.NativeAuthenticationProvider.negotiateSSLConnection(NativeAuthenticationProvider.java:777)
at com.mysql.cj.protocol.a.NativeAuthenticationProvider.proceedHandshakeWithPluggableAuthentication(NativeAuthenticationProvider.java:486)
at com.mysql.cj.protocol.a.NativeAuthenticationProvider.connect(NativeAuthenticationProvider.java:202)
at com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1442)
at com.mysql.cj.NativeSession.connect(NativeSession.java:165)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
... 11 more
Caused by: java.net.SocketException: Broken pipe (Write failed)
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
at sun.security.ssl.OutputRecord.writeBuffer(OutputRecord.java:431)
at sun.security.ssl.OutputRecord.write(OutputRecord.java:417)
at sun.security.ssl.SSLSocketImpl.writeRecordInternal(SSLSocketImpl.java:879)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:850)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:720)
at sun.security.ssl.Handshaker.sendChangeCipherSpec(Handshaker.java:1144)
at sun.security.ssl.ClientHandshaker.sendChangeCipherAndFinish(ClientHandshaker.java:1280)
at sun.security.ssl.ClientHandshaker.serverHelloDone(ClientHandshaker.java:1190)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:369)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1037)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:965)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1064)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)
at com.mysql.cj.protocol.ExportControlled.performTlsHandshake(ExportControlled.java:315)
====================================================== New Information =====
Here are the key lines from the connectToDB method (responding to comment below):
Driver dvr = (Driver)Class.forName(info.sDBDriver).newInstance();
if(dvr!=null) conn = dvr.connect(accessString(), null);
verified the expected driver and that accessString() returns the 'jdbc:mysql:...' string as above. The exception is thrown executing the 2nd line above.
Since the original post, put in code to run a 'netstat -ln' immediately before these lines and post the result to the log. This shows an active listener for port 3306.
It would be great if you can post connectToDB() method.
According to this sentence " The first one works perfectly, the second one aborts with the stack pasted below.", you must have closed the db connection in the first connectToDB() call.
So that second call is broken as the DB connection was terminated by first call.
The good way to setup is to use dependency injection for database object.
Hope this helps!

Java RMI 'java.rmi.ConnectException: Connection refused to host: 127.0.1.1;' [duplicate]

java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is:
java.net.ConnectException: Connection refused
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:128)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
at com.sun.proxy.$Proxy0.notifyMe(Unknown Source)
at CallbackServerImpl.doCallback(CallbackServerImpl.java:149)
at CallbackServerImpl.registerForCallback(CallbackServerImpl.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 23 more
I get this exception when I try to connect a remote client to my server. In both, server and client the hostName for the registryUrl of rmi is the public IP address of the server. I also tried to put localhost in server but the error doesn't change.
My java.policy is set to grant all connections to all ports and I have no firewalls enabled in the server or the client.
Any suggestions what could be?
This is item A.1 in the RMI FAQ. You need to either fix your /etc/hosts file or set the java.rmi.server.hostname property at the server.
PROBLEM SOLVED
I had exactly the same error. When the remote object got binded to the rmiregistry it was attached with the loopback IP Address which will obviously fail if you try to invoke a method from a remote address. In order to fix this we need to set the java.rmi.server.hostname property to the IP address where other devices can reach your rmiregistry over the network. It doesn't work when you try to set the parameter through the JVM. It worked for me just by adding the following line to my code just before binding the object to the rmiregistry:
System.setProperty("java.rmi.server.hostname","192.168.1.2");
In this case the IP address on the local network of the PC binding the remote object on the RMI Registry is 192.168.1.2.
you can use LocateRegistry such as:
Registry rgsty = LocateRegistry.createRegistry(1888);
rgsty.rebind("hello", hello);
I found many of the Q&A on this topic, not nothing was helping me - that's because my issue was more basic ( what can I say I am not a networking guru :) ). My ip address in /etc/hosts was incorrect. What I had tried included the following for CATALINA_OPTS:
CATALINA_OPTS="$CATALINA_OPTS -Djava.awt.headless=true -Xmx128M -server
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=7091
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=A.B.C.D" #howeverI put the wrong ip here!
export CATALINA_OPTS
My problem was that I had changed my ip address many months ago, but never updated my /etc/hosts file. it seems that by default the jconsole uses the hostname -i ip address in some fashion even though I was viewing local processes. The best solution was to simply change the /etc/hosts file.
The other solution which can work is to get your correct ip address from /sbin/ifconfig and use that ip address when specifying the ip address in, for example, a catalina.sh script:
-Djava.rmi.server.hostname=A.B.C.D
If you're running in a Linux environment, open the file /etc/hosts.allow
add the following line
ALL
Wildcards
Also check the /etc/hostname and /etc/host to see if there might be something wrong there.
I had to change my / etc / host from
127.0.0.1 localhost
127.0.1.1 AMK
to
127.0.0.1 localhost
127.0.0.1 AMK
also wrote in ALL in the file /etc/hosts.allow which was previously completely empty
Now everything works
do not know how safe it is. you have to read more about possible options for /etc/hosts.allow to do something that requires a touch of security.
Maybe your rmiregistry not be created before client trying connect to your server and it would lead to this exception.In Linux, you can use "netstat" to check your rmiregistry be bond on the right port you assigned in java code.
If you've tried modifying etc/hosts and adding java.rmi.server.hostname property as well but still registry is being bind to 127.0.0.1
the issue for me was resolved after explicitly setting System property through code though the same property wasn't picked from jvm args
It works for me after getting rid of "::1" in /etc/hosts.
I had the same exact problem and my issue was that I had 2 IP addresses from 2 different networks configured in the etc/hosts as below.
10.xxx.x.xxx localhost
192.xxx.x.xxx localhost
This should be because there was a conflict as to which IP to be used for the other devices to reach the rmiregistry over the network.
Once I removed the extra-record that is not required, I was able to solve the issue.
So my etc/hosts file had only the following record.
10.xxx.x.xxx localhost
In my case I was unable to edit the hosts file because using a pc from the university.
I fixed the problem running rmiregistry in another port (instead of 1099) with:
rmiregistry <port>
and then running the server on that port.
It was basically an error caused by occupied port.
when you want to connect to remote server with RMI you must add a system property same as:
System.setProperty("java.rmi.server.hostname","Ip or DNS of the server");
or add environment variable.
For me I got Connection Refused and solve it by adding this line of code in server side:
java -jar -Djava.rmi.server.hostname="ip or dns of the server" packageName.jar
Thank to other guy for guide me to solve it.
On Windows make sure your Windows firewall is correctly configure / disabled. I had to disable the Windows firewall (because I didn't bother with configuring it) to get things to work even when I was testing with localhost.
When I got the same error on my machine ("connection is refused"), the reason was that I had defined the following on the server side:
Naming.rebind("rmi://localhost:8080/AddService"
,addService);
Thus the server binds both the IP = 127.0.0.1 and the port 8080.
But on the client side I had used:
AddServerInterface st = (AddServerInterface)Naming.lookup("rmi://localhost"
+"/AddService");
Thus I forgot to add the port number after the localhost, so I rewrote the above command and added the port number 8080 as follows:
AddServerInterface st = (AddServerInterface)Naming.lookup("rmi://localhost:8080"
+"/AddService");
and everything worked fine.
You can simply use:
on server side:
Registry <objectName1> = LocateRegistry.createRegistry(1099);
Registry <objectName2> = LocateRegistry.getRegistry();
on Client Side:
Registry <object name you want> = LocateRegistry.getRegistry();

failure to connect to mysql from java locally while remote connection works

I have the following situation:
I can
connect to my mysql database via the command line tool mysql, both from the local machine and a remote host
connect from a small java test program from the remote host
I can not
connect from that same small java test program from the local machine
The test code is basically the same as in this question and I followed all the suggestions given there to solve my problem with no avail. I have tried the following connection strings:
jdbc:mysql://localhost/database
jdbc:mysql://localhost:3306/database
jdbc:mysql://127.0.0.1/database
jdbc:mysql://hostname/database
all resulting in the same error message. On the remote machine I could use the fourth alternative with no problem.
I am running a mysql-5.5 server. The remote uses java version "1.7.0_101" and the local machine has java version "1.6.0_38".
Here is the error message when I run my java program on the local machine:
Connecting to database...
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:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:534)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:688)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1094)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2337)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2370)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:534)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(DriverManager.java:615)
at java.sql.DriverManager.getConnection(DriverManager.java:195)
at FirstExample.main(FirstExample.java:22)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2540)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:612)
... 16 more
Goodbye!
I just realized that mysql -u user database would work, but not mysql -u user -h 127.0.0.1 database, so it turned out I had an error in my /etc/hosts.allow file. Problem solved!

RabbitMQ new connection refused due to SocketException

while trying to create a new connection to rabbitmq running on a different server, I got the following error:
java.io.IOException
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106)
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124)
at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:406)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:516)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:533)
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; reason: java.net.SocketException: Connection reset
at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67)
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:343)
at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.readUnsignedByte(Unknown Source)
at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:95)
at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:131)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:533)
Steps taken :
rabbitmq is running on the server.
server is specified
default port is specified
lsof -i tcp:5672
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
beam.smp 3084 rabbitmq 15u IPv6 18611 0t0 TCP *:amqp (LISTEN)
rabbitmqctl list_connections
Listing connections ...
guest client_server 55765 running
...done.
netstat -tapnl | grep 5672
tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 3084/beam.smp
tcp 0 0 0.0.0.0:55672 0.0.0.0:* LISTEN 3084/beam.smp
tcp 0 0 :::5672 :::* LISTEN 3084/beam.smp
One of the possible reasons is that user you are connecting with to RabbitMQ has no rights to access to virtual hosts.
You can check this using Management Plugin (Admin tab).
Do not specify the default port as you have mentioned in your steps.
If you have not created virtual host on the actual server, where you are trying to connect, Do create a virtual host and give it admin permision.
Set the virtual host on the factory before creating the new connection, like factory.setVirtualHost("VIRTUAL_HOST_NAME_ON_SERVER");
Make sure username on the server on which you are trying to connect is Admin and have access to the Virtual Host you just created.
Specify your username and password along with virtual host, while getting the connection.
Start your application in Debug Mode, and check if it now passes, factory.newConection();
This should make your things work.
Got the same exception, and it worked for me.
If it still does not work paste your code snippet.
Check the host and port value
In application.properties
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
See RabbitMQ site is running on port 15672 whereas in code using amqp protocol.
You can check if the SSL/TLS support is enabled. Then use the instruction useSslProtocol :
ConnectionFactory factory = new ConnectionFactory();
factory.useSslProtocol();
I got Unable to connect to AMQP server error when create a new connection. It was confirmed that it was a problem with the firewall. I solved it by command systemctl stop firewalld.

Tomcat unable to connect to mysql through JDBC connector

I have an web application that I was trying deployed on a remote windows 2003 server with java6, tomcat6 and mysql5.5. After deploying application when I hit the application url I am getting following error. I did some google and found some link which says to change localhost in connection string to 127.0.0.1 but that did not worked, some link says to check connection string so I created a simple java class on same windows machine to test the connection string, usename and password. When I ran that class it worked fine without any error but when I am running web application which uses same connection string and username and password it is throwing the error. There are also some links which advice to modify my.ini of mysql but I did not find any my.ini in mysql installation directory.
I am not getting any clue to deal with this error so could you please guide me on this. Full stack trace is as below
INFO: Server startup in 2832 ms
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link fai
lure
The last packet sent successfully to the server was 0 milliseconds ago. The driv
er has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
orAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
onstructorAccessorImpl.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:1
117)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:350)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2408)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2
445)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2230)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
orAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
onstructorAccessorImpl.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:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
:334)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at com.adxchange.utils.DBConnectionSingleton.getConnection(DBConnectionS
ingleton.java:28)
at com.adxchange.utils.ADXchange.getStates(ADXchange.java:402)
at org.apache.jsp.homepage_jsp._jspService(homepage_jsp.java:252)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:3
13)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatc
her.java:416)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcesso
r.java:877)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.pr
ocess(Http11AprProtocol.java:594)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:16
75)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.net.ConnectException: Connection timed out: connect
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.ja
va:259)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
... 38 more
getStates: Error in database connection
could it be some permission issue?
EDIT:
I also created CLASSPATH variable in system variables and added java\lib and tomcat\lib path in it but still no result.
Sorry friends to put you in effort of looking and answering my question but I found the solution. Actually it is my silly mistake that I have used .properties file for all db and other changeable things and the problem was that I was modifying wrong properties file. It is so embarrassing that I created a mess for simple thing. Anyway thanks a lot that you guys helped.
I think you may need to take a look at the tomcat config, a usual error on these modern setups is that either mysql or tomcat is trying to connect using IPV6 so you need to make sure you only listen on IPV4 for it to work.
I'd also see if there's a firewall between your Tomcat and MySQL. You might need a firewall rule to open up the port for MySQL.
You can tell by pinging the MySQL IP address from the Tomcat server. If you can't ping, your app can't either.
The host should be the name of the MySQL server, not localhost.
Is the database running on the same box as Tomcat? That's not a good design.
Are you sure MySQL is running? Can you log into the admin console and access the database that your Java app needs to get to? If yes, does the Java app use appropriate credentials? (Please don't tell me that you gave it the admin root access.)

Categories