Search code examples
javaamazon-web-servicesamazon-ec2amazon-rds

Deploy Web App on EC2 instance AWS - Database Confusion


Apologize in advance if this is a stupid question but I've been trying to search up this question without being able to find anything relevant.

So I'm trying to deploy a simple registration/login web app project that inserts/retrieves information of users into/from MySQL database onto an EC2 instance. I think I narrowed down the issue in problem down to these lines of code

    String url = "jdbc:mysql://{RDS Database Instance endpoint}:3306/";
    String userName = "root";
    String password = "password";
    String dbName = "LOGIN"; 
    Connection con = DriverManager.getConnection(url + dbName, userName, password);

as the page ran fine except for login/registration which uses these lines. Originally, these were to connect to my local MySQL database. The SQL script on my local machine is

    use LOGIN; 
    drop table if exists users;
    create table users (
    id int primary key auto_increment,
    user_name varchar(50),
    pass varchar(50),
    email varchar(50),
    mobile varchar(20)
);

The error that I got on EC2 instance is about con being null which seems to be that there is connection error to the database on EC2.

Here is where I got confused. I did create an Amazon RDS instance (db.t2.micro instance type) with DB name LOGIN for MySQL and connect that to an EC2 instance (t2.micro instance). However, I also installed MySQL server using EC2 instance terminal, ran mysql -u root -p, and created the table users this way. The MySQL Server on EC2 instance has databases mysql (?) with seemingly different host name.

I'm not sure what is the difference between the two and what should I use in the jdbc connection code above?

EDIT: The exact error is here enter image description here


Solution

  • Ok, I figured out my problems now. I'm going to leave a comment here in case someone else has the same issues:

    1. First thing was adding inbound rules for the database so that there is no restriction.

    2. Second thing was adding mysql-connector.jar to the bin folder inside where I stored tomcat server. This was added locally but somehow not included when deploying onto EC2.