I have a MySQL database running inside an amazon t1.micro ec2 Linux instance.
I use a data source so I can do connection pooling, the code works fine on my home pc, but I get a communications link error when running the same exact code on the instance.
DataSource.java looks like this:
package resources;
import org.apache.commons.dbcp2.BasicDataSource;
public class DataSource {
private static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com:3306/database";
private static final String DB_USER = "ec2-user";
private static final String DB_PASSWORD = "root";
private static final int CONN_POOL_SIZE = 50;
private BasicDataSource bds = new BasicDataSource();
private DataSource() {
bds.setDriverClassName(DRIVER_CLASS_NAME);
bds.setUrl(DB_URL);
bds.setUsername(DB_USER);
bds.setPassword(DB_PASSWORD);
bds.setInitialSize(CONN_POOL_SIZE);
}
Then I have a jersey rest address which draws a connection like so:
source = DataSource.getInstance().getBds();
connection = source.getConnection();
When I run the project on my own pc, there's no errors and it fetches the data correctly. I can connect to the database remotely both via command line and from code with JDBC. But when I run/deploy the exact same code onto the amazon instance, I get this error trace:
java.sql.SQLException: Cannot create PoolableConnectionFactory (Communications link failure
Last packet sent to the server was 0 ms ago.)
[lower down]
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
So apparently when ec2-user @ localhost tries to connect, the DB is not available.
the contents of my.cnf are: my.cnf
I have tried messing around with the bind address, setting it to 127.0.0.1 and commenting it out.
I am able to open mysql from linux command line with "mysql -u ec2-user -p root", and "mysql -h localhost -u ec2-user -p root". I confirmed that the server is in fact running on port 3306.
On my amazon instance security group i have the following inbound rules: https://i.stack.imgur.com/la0q2.png
I went through the steps in this post: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
IP address or hostname in JDBC URL is wrong.
Hostname in JDBC URL is not recognized by local DNS server.
Port number is missing or wrong in JDBC URL.
DB server is down.
DB server doesn't accept TCP/IP connections.
DB server has run out of connections. -||-
Something in between Java and DB is blocking connections, eg a firewall or proxy.
The user is "ec2-user"@"%" with all privileges granted, this user should work for both local and remote?
I am all out of things to try, most scenarios I came across are where people have it working locally but NOT remotely.
I am able to connect to localhost 3306 via telnet from the instance linux command line:
telnet localhost 3306 - Trying 127.0.0.1... Connected to localhost. Escape character is '^]'.
Issue avoided by simply running the mysql database on a separate ec2- instance. I guess there's no answer to the original setup but... this works right now.
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.