How to Connect MySQL to EC2 Instance From Ubuntu?

MySQL is a relational DBMS deployed by Oracle with a client center model which is used to store data in tables. Elastic Compute Cloud (EC2) is used to create virtual machines on the cloud called instances. More specifically, AWS offers to connect MySQL to the EC2 instances on the cloud.

This guide will explain how to connect MySQL to an EC2 instance from Ubuntu.

How to Connect MySQL to EC2 Instance From Ubuntu?

For connecting MySQL on AWS EC2 from Ubuntu, check out the following demonstration.

Step 1: Connect to the EC2 Instance

It is required to have an EC2 instance created to connect to it. Select the instance and click on the “Connect” button:

Select the “SSH client” section and copy the command provided by the platform:

Open the terminal in the Ubuntu machine and paste the command on it. Before executing the command on the terminal, change the path of the private key pair file from the system:

Type this command to provide access to the root user:

sudo su

Update the apt packages:

sudo apt update

Step 2: Install MySQL to the Instance

Use the following command to install the MySQL server:

apt-get install mysql-server -y

After the MySQL server installation, check its version:

mysql --version

It can be observed that the MySQL version “8.0.32” has been installed successfully:

Step 3: Configure MySQL

Change the directory and head into the “mysql.conf.d” file:

cd /etc/mysql/mysql.conf.d/

Type “ls” to get the list of files available in the directory:


Edit the “mysqld.cnf” file:

sudo nano mysqld.cnf

Change the “bind-address” to “” and save the file:

Step 4: Connect MySQL to the Instance

Restart the MySQL server:

service mysql restart

Syntax to connect to MySQL is mentioned below:

mysql -u <username> -p

Type the following command to connect to MySQL server:

mysql -u root -p

Executing the above command will prompt the user to enter password:

As a result, MySQL will be connected to the EC2 instance from Ubuntu.


To connect MySQL to the EC2 instance from Ubuntu, create and connect to the EC2 instance from Ubuntu. After that, update apt packages and then install MySQL server on the instance. Edit the “mysqld.cnf” file and change the bind address to “” within the file. Restart the MySQL services and connect to it. This guide has explained how to connect MySQL to an EC2 instance from Ubuntu.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.