connect amazon mysql with JDBC through remote server - java

I had a problem here, is it possible that JDBC connecting to the amazon MySQL server remotely? I searching on the Internet for solution but ended up receiving the error
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.
I am using jsch.jar to connect the remote server, then call up JDBC connector trying to connect MySQL DB, I had successfully connected to the remote server yet I cannot connect MySQL DB on amazon, here is my code
testPutty t = new testPutty();
t.connect("admin", "", "xx:xx:xx:xx", "openSSHPrivateKey", 22);
t.connect();
System.out.println("connected");
Class.forName("com.mysql.jdbc.Driver");
DriverManager.getConnection("jdbc:mysql://staging.ppyhcsxnlkji.ap-southwest-1.rds.amazonaws.com:3306/staging","superuser","password");
System.out.println("hello world");
t.close();

First set up the tunnel with SSH. You haven't shown what testPutty is so I'll assume it's a thin Java wrapper around a command line invocation of Putty. The command line option for setting up a tunnel is
-L[localport]:[host]:[remoteport]
For your example this would be
-L3306:staging.ppyhcsxnlkji.ap-southwest-1.rds.amazonaws.com:3306
This sets up a tunnel that forwards connections to local port 3306 to port 3306 on the given host (staging...amazonaws.com) on port 3306, routed through the host that is the target of the SSH command (in your sample xx:xx:xx:xx).
Then, your connection string needs to use localhost instead of the actual remote:
jdbc:mysql://localhost:3306/staging
If 3306 on your local host is not available because you are also running a local copy of MySQL, just choose a different unused port.

Related

Failure while connecting to MS SQL server

Trying to connect o MS SQL server that runs on the same Win 10 machine with my project in Eclipse:
String url = "jdbc:sqlserver://GOLD\\SQLEXPRESS;databaseName=PrintLog;";
Connection conn = DriverManager.getConnection(url ,"user","passwd");
But got exception:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host GOLD, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
Computer name is GOLD. I can connect to DB from C# without problem. Authentication is enabled both windows and SQL server. I'm expecting to get Windows authentication while connect
SQL server properties:
How to find the problem?
UPD
I got better result by adding port number to connect string:
String url = "jdbc:sqlserver://GOLD\\SQLEXPRESS:1433;databaseName=PrintLog;";
Does it means that default port number is different?

Failing to connect to MySQL Server from Java application

I am attempting to establish a connection to my local sqlserver instance on android with this line
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:3306;DatabaseName=test", "admin", "password");
But I am getting this error
The TCP/IP connection to the host localhost, port 3306 has failed.
Error: "Connection refused. Verify the connection properties. Make
sure that an instance of SQL Server is running on the host and
accepting TCP/IP connections at the port. Make sure that TCP
connections to the port are not blocked by a firewall
I know that using jdbc to create direct database connections on android is not recommended for very good reasons, but that is not my concern right now.
I have verified in sql server configuration manager that tcp/ip is enabled for LOCAL and that IPAll TCP Port is set to 3306. I restarted the server after making these changes.
I have created a firewall rule allowing tcp/ip connections on port 3306 for local/domain connections inbound and outbound.
I checked the port with telnet telnet localhost 3306 and it connected.
Any help is appreciated, it is beginning to get frustrating.

How to connect With CPanel MySql DB from localmachine

I am trying to test the connection from NetBeans IDE8.0 with New Connection Wizard to the Cpanel MySql Database .
I already added my machine ip address in the Remote MySql option of Cpanel but still i am not able to connect with database.
I am getting below error message
Cannot establish a connection to jdbc:mysql:// someIP:3306/hariom?zeroDateTimeBehavior=convertToNull using com.mysql.jdbc.Driver (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.)
If your server has ConfigServerFirewall installed you need to open the port there as well event though you turned on remote mysql on your cPanel. If you do not own the server you need to contact the host for this.

how to connect java with wamp mysql server

I am new java programmer, I try to setup jdbc connnectivity with wamp mysql server.
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection( "jdbc:mysql://182.73.151.36:3306/sbtsystem?user=sbtuser&password=Pm#dmin&useUnicode=true&characterEncoding=UTF-8");
182.73.151.36 this is server IP address. I can't understand how to connect java with wamp mysql server
Try This:
First run these commands
ping 182.73.151.36
telnet 182.73.151.36 3306
And make 182.73.151.36 mysql eligible for remote connection
After that change your connection line and run this program
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://182.73.151.36:3306/sbtsystem","sbtuser","Pm#dmin");
You need to first download the jdbc driver files for mysql
(download from here)
The code looks fine. You need to check the network configurations. Try the following commands to see if the server is accessible.
ping 182.73.151.36
telnet 182.73.151.36 3306
supposing your network configuration is correct,
I guess your mysql server on 182.73.151.36 is not configured for remote connection,

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure [duplicate]

This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
(51 answers)
Closed 6 years ago.
I Have developed an application to capture data, retrieve information,delete and also update info. The application is developed in java swing and hibernate and uses MySQL version 5.5 as a database. I tested it on my local server or local host and its working fine. So now i took it a step further trying to access the database on the remote server. Please note that also the MySQL version i use on my local server is the same as the MySQL in the remote server.
I get the following error:
Internal Exception: 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.
Error Code: 0
The problem is that your MySqlConnector/Driver can't establish a Connection to the remote server.
Check your jdbc URL and also the check if the wrong ports are used.
Maybe the Mysql DB is not reachable from outside, than you can use a port forwarding with ssh, for example :
port forwarding:
ssh -L3306:127.0.0.1:3306 user#remoteServer
than change your jdbc url to:
jdbc:mysql://localhost:3306/database
Explanation:
-L port:host:hostport
Specifies that the given port on the local (client) host is to be
forwarded to the given host and port on the remote side.
...
Whenever a
connection is made to this port, the connection is forwarded over
the secure channel, and a connection is made to host port
hostport from the remote machine.
Source: http://www.openbsd.org/cgi-bin/man.cgi?query=ssh&sektion=1
That means you request with your MySQL JDBC Driver(jdbc url) on the local 3306 port
will be forwarded to the remote server on his local 3306 port,
where the Mysql Database is listing.

Categories