This document provides a 6 step guide to setup and connect MySQL to an EC2 instance running Ubuntu. It involves installing MySQL, configuring the installation with security settings and passwords, creating a database user, granting privileges to the user, connecting to MySQL with the user, and restarting the MySQL service. The goal is to have a working MySQL database installed and accessible from an EC2 Ubuntu server.
Setup and Connect MySQL Database to EC2 Instance on Ubuntu
1. How to Setup
and Connect
MySQL to
EC2 Instance
from Ubuntu
www.bacancytechnology.com
2. MYSQL is a famous open-source relational
database management system that provides
scalable and easy-to-use function sets. It
uses a relational DB and Structured Query
Language (SQL) for managing its data.
This tutorial describes a basic MySQL
installation and setup; later we will connect
MySQL to EC2 instance from Ubuntu. Once
done with the basics, we will create a user
for our database.
4. AWS EC2 – AWS Elastic Compute Cloud is
one of the most famous Amazon services
that provides the ability of running the app
on the public cloud. It offers safe, reliable,
and resizable cloud capacity with minimum
obstacles.
I assume that you are quite familiar with
Amazon EC2 and MySQL to proceed with
MySQL Setup in EC2 Ubuntu Instance.
6. Create EC2 Ubuntu Instance in AWS
Account
Connect EC2 Instance using SSH or
Putty
To get started with the tutorial for setting
up MySQL in EC2 Ubuntu Instance, please
make sure of the following prerequisites-
Now, let’s move towards how to set up and
connect MySQL to EC2 Instance Ubuntu.
8. Step 1: Update System
Packages and Install
MySQL
Run install update for your server system
packages
sudo apt update
Now, install MySQL using the below
command to install the latest MySQL.
sudo apt-get install mysql-server -y
After successful installation of mysql-
server, we can check the MySQL version by
using this command-
mysql --version
9. Step 2: Configure MySQL
Installation
To configure the installation of MySQL, run
the security script with sudo to run as the
root user- the super privileged user.
Run the below command-
sudo mysql_secure_installation
10. When we install MySQL for the first time, we
will be prompted to create a new password.
If you are getting an error while setting up
the password, check the temporary
password in the log file.
Use the below command to get a temporary
password.
12. Step 3: Login into MySQL
and Create a User
Use the below command to login into
MySQL:
sudo mysql
The command will log in to MySQL using
the root user. Once you are logged in, create
a user in the MySQL database using these
commands:
use mysql;
CREATE USER 'username'@'host'
IDENTIFIED WITH
mysql_native_password BY 'password';
13. Step 4: Grant Access
Privileges to User
Now, to grant access privileges to this
newly created user, use this command:
GRANT ALL PRIVILEGES ON *.* TO
'username'@'host';
Once this is done, run this command,
FLUSH PRIVILEGES;
14. Step 5: Connect User with
MySQL
Now you can use this user to connect with
MySQL like this:
mysql -u user -p
Once prompted for a password, provide the
password, and you will be connected.
Trick: If you want to store passwords for
that user, so prompting will not annoy us,
do these things.
Create .my.cnf file inside /home/Ubuntu
path
Write this command:
sudo nano .my.cnf
15. Write these lines in that file and save.
[mysql]
user=username
password=password
Now, whenever you want to connect, you
can use this .my.cnf file like this, and no
need to provide a password.
mysql --defaults-
file=/home/ubuntu/.my.cnf -u username
This file is generally needed when running
the CI/CD pipeline and connecting to
another server’s MySQL server.
16. Step 6: Restart MySQL
Service
To affect these changes, restart the MySQL
service by following commands:
to check the status of MySQL service:
systemctl status mysql.service
Now, if it is in “Running” mode, then restart
it using the below command:
sudo service mysql restart
17. This will restart the service and will reflect
our changes.
Now, your MySQL has been set up in your
Ubuntu Ec2 Instance.
Quick Read:
Cloud Cost Optimization: An Essential
Guide to AWS Cloud Migration
19. So, this was about how we can set up
MySQL in EC2 Ubuntu Instance. If you have
followed all the steps as discussed above,
then MySQL setup is successfully done in
EC2 Ubuntu Instance. Moreover, we
covered creating a new database and adding
a user to it. For more such tutorials, you can
visit the Cloud Tutorials page to explore
more about Cloud and DevOps services.
Are you looking for a certified architect for
managing AWS services? Then, without
wasting your time, connect us to hire AWS
experts. Bacancy has proficient and
qualified experts with AWS certifications
who have competence in providing
optimum solutions.