AWS

4 Mins Read

Detailed Guide to Access Database hosted on EC2 Remotely

Voiced by Amazon Polly

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

Prerequisites

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

Step1

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

Step2

Step 3 – SSH inside the machine

Step3

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 I’m giving all privileges to test

Step01

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 0.0.0.0. If your machine is set for IPv6, use: instead of 0.0.0.0
  • 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.

Step02

Step 3 – Restart MySQL Server

Step03

Step 4 – Check Connection

Check connection of database using PHP

Step04a

Step04b

Conclusion

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 also the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

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 Consultancy page and Managed Services Package that is CloudThat’s offerings.

FAQs

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

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

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

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

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

ANS: – 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

4. Can we secure the manually hosted database?

ANS: – Yes, you can secure the manual hosted database from three different places- (i) From AWS Network Access Control List (NACL)
(ii)From EC2 Security Group
(iii)From MySQL config file bind-address
(iv)By giving required access to users created in the database

WRITTEN BY Rishi Raj

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!