Detailed Guide to Access Database hosted on EC2 Remotely

September 28, 2022 | Comments(0) |


1. Overview of Hosted and Traditional Database
2. Real-time Scenario Example
3. Prerequisites
4. Steps to install MySQL in AWS EC2
5. Real-time Use Cases
6. Conclusion
7. About CloudThat
8. FAQs


Overview of Hosted and Traditional Database

Traditional databases provide for the management of underlying business resources and infrastructure on corporate premises. Fortunately, cloud databases have transformed that industry by making the same resources available on a pay-as-you-go basis.

Provisioning firm infrastructure and resources in data centers to administer and on-site a database is costly and time-consuming. Traditional databases need extensive preparation in terms of purchase orders for both hardware and database software.

Cloud databases have a crucial advantage over traditional databases in that they allow organizations with critical data-resource requirements to grow on demand without worrying about availability or security—the cloud provides multiple database replication across several geographical locations.

Real-time Scenario Example

Let us suppose that you want to use a database in your application, but you don’t have any data center to host your database, so you have chosen the cloud to host the database. Instead of a managed database, you have opted for a custom installation of a database in the server

Here, I have used the AWS EC2 server to host the database. The database engine is MySQL 8.0.29


AWS Account

  1. EC2 Instance
  2. User with privileges to create an instance

Steps to install MySQL in AWS EC2

Step 1- Launch an EC2 Server with Ubuntu 18.04


Step 2 Check the security group of EC2 Server, which has MySQL/Aurora Port open only from specified IP Address and SSH port from your IP Address


Step 3 SSH Inside the machine


Step-4 SSH Inside the machine and Update and install mysql-server

Step-5 Go inside your MySQL

Step-6 Alter root user credentials for the database and flush all the privileges

Step 7 Check whether the password is applied for the root user or not


Steps to Connect to EC2 database remotely

Step 1 Create a new remote user in MySQL, create a new user, and give permission as required. Here is am giving all privileges to test


Note: Don’t use any reserved keyword to create a remote user like (root, Admin, Admin)

Step 2 Change the data-bind address to the IP where you want to access the database

  • Set the MySQL server to listen to all IP addresses on the system if you wish to connect to it via a public network. To do so, modify the MySQL configuration file and add or update the bind-address option’s value. You can configure a single IP address as well as IP ranges. The MySQL server accepts connections on all host IPv4 interfaces if the address is If your machine is set for IPv6, use: instead of
  • Depending on the distribution, the location of the MySQL configuration file varies. The file is placed at /etc/mysql/mysql.conf.d/mysqld.cnf in Ubuntu and Debian, and at/etc/my.cnf in Red Hat-based distributions such as CentOS.


Step 3 Restart MySQL Server


Step 4 Check Connection

Check connection of database using PHP




If you need an automated solution, go for the managed database; for more control and flexibility, go for manual database hosting.

For some cases like automated backups, security, and updating versions, this will be taken care of by managed database. If you want to have control of the OS, manage your database by looking after clustering and replication, then go for the manually hosted database.

The choice ultimately comes down to whether you prefer manual or automated operations, how much time it will cost, and your management abilities.

About CloudThat

CloudThat is an official Microsoft Gold Partner, AWS Advanced Consulting Partner, and Training Partner, assisting people in gaining cloud knowledge and assisting organizations in achieving best-in-industry cloud computing expertise and benefits. We help many organizations build cloud ecosystems and will continue this quest to make this ecosystem more robust and more resilient.

Drop a query if you have any questions regarding the hosted database, traditional database, or any other cloud services, and I will get back to you quickly. To get started, go through our Expertise Page and CloudThat’s offerings.


  1. Should the subnet associated with the server be public or private?

The subnet should be public. Otherwise, we would not be able to connect the database to the application code

  1. Can we access the hosted database on private IP?

Yes, if our hosted database and Application code lie in the same VPC

  1. Are manual hosted databases best as compared to the database which Cloud Providers offer?

If you want to have complete control over the database users and privileges including patching and updating, then you can opt for hosting the database manually otherwise hosted DB which is offered by cloud providers are good option to pick up

  1. Can we secure the manually hosted database?

Yes, you can secure the manual hosted database from three different places-

  • From AWS Network Access Control List (NACL)
  • From EC2 Security Group
  • From MySQL config file bind-address
  • By giving required access to users created in the database

Leave a Reply