How to Install MySQL 8 on Amazon Linux 2 Instance

How to Install MySQL 8 on Amazon Linux 2 Instance

How To Install MySQL 8 on Amazon Linux 2 Instance

Dear Reader, I hope you are doing well today. In my last few posts, I have shared with you how to install GitAnsibleNginxApacheDockerJavaPHP 8.1 etc. on Amazon Linux 2 or AL2 EC2 Instance.

In this article, we’ll discuss how to install MySQL 8 on Amazon Linux 2 EC2 instance in a step-by-step manner.

So are you ready?

Alright, let’s start.

Don’t want to miss any posts from us? join us on our Facebook group, and follow us on Facebook, Twitter, LinkedIn, and Instagram. You can also subscribe to our newsletter below to not miss any updates from us.

A bit on MySQL and Amazon Linux 2(AL2)

MySQL: MySQL is the world’s most popular open-source relational database management system. It is based on Structured Query Language (SQL)

  • It is used to store data for various types of applications such as web applications, e-commerce applications, social media sites etc.
  • MySQL is easy to use and manage.
  • It is a superbly scalable and reliable RDBMS.
  • Is popular among small projects to big enterprise applications.
  • Supported by a large community of developers etc.

Amazon Linux 2: Amazon Linux 2 or AL2 is the Linux operating system provided by AWS. It is based on Red Hat Enterprise Linux (RHEL) and optimized for performance.

  • You can use Yum package manager to install software such as MySQL 8 on Amazon Linux 2
  • It is free to use
  • Designed to be secure, reliable and scalable.

Prerequisite

An Amazon Linux 2 EC2 instance that you can connect to and install MySQL server on.

You can follow my tutorial to launch an Amazon Linux 2 Instance within minutes.

Steps to Install MySQL 8 on Amazon Linux 2 Instance

  1. Connect to your Amazon Linux 2 Instance
  2. Run System Update
  3. Add MySQL MySQL Yum Repository
  4. Install MySQL 8 on Amazon Linux 2 Instance
  5. Verify MySQL Installation
  6. Start and Enable MySQL Service
  7. Find the Initial root Password
  8. Secure MySQL Installation
  9. Connect to MySQL using the root user
  10. Change root Password on need(Optional)

Step 1: Connect to your Amazon Linux 2 Instance

Shall I assume that you already have an Amazon Linux 2 instance up and running? And you ready to install MySQL on your Amazon Linux 2 Instance? However, If that’s not the case, feel free to launch one using one of my previous tutorials.

Once the instance is up and running, select your instance and click on Connect as shown in the below screenshot.

Connect to EC2 Instance

Verify that ec2-user is showing in the user name field and click Connect again.

In a matter of seconds, you should be connected to your instance.

Important Note: If you prefer connecting from your local, feel free to use the command- ssh -i /path/my-key-pair.pem ec2-user@instance-public-ip

Step 2: Run System Update

Before we install any software in your Amazon Linux 2 instance, it’s recommended to update your system packages to the latest.

You can run the system update using the below command-

sudo yum update

Type sudo yum update and hit enter.

Install Update on Amazon Linux 2

Step 3: Add MySQL Yum Repository

AWS recommends Maria DB and hence MySQL is not available in the Amazon Linux 2 core repository.

The MySQL team provides the RPM package to install MYSQL on your Linux System. Let’s add the MySQL Yum repository to our Amazon Linux 2 system’s repository list.

Navigate to the downloads page and choose a package for your system. As Amazon Linux 2 is el7-based we’ll be using below highlighted package which is mysql80-community-release-el7-10.noarch.rpm.

How to Install MySQL 8 on Amazon Linux 2 Instance 1

The command to add this repo to our repo list is-

sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-10.noarch.rpm -y
How to Install MySQL 8 on Amazon Linux 2 Instance 2 updated

Repo is added as you can see, you can also confirm the same by –

yum repolist enabled
How to Install MySQL 8 on Amazon Linux 2 Instance 4

Can you see the MySQL community package, Yeah we are going to install MySQL server on our Amazon Linux 2 from it.

Step 4: Install MySQL 8 on Amazon Linux 2 Instance

As we saw MySQL repo is added and enabled. Let’s install MySQL from it

sudo yum -y install mysql-community-server
How to Install MySQL 8 on Amazon Linux 2 Instance 5

As soon as you hit enter, MySQL along with all it’s dependencies gets installed on your Amazon Linux 2.

Step 5: Verify MySQL Installation

To check the version your MySQL is running use-

mysql -V
mysql 8 installed

And as expected the version is 8.0.34

Step 6: Start and Enable MySQL Service

We have just installed MySQL but at the moment it’s not running, You need to manually start it. Additionally, you also need to enable it so that next time it can start automatically on system reboots.

Here are the commands for the same-

systemctl start mysqld //Starts MySQL service
systemctl enable mysqld //Enabled mySQL service to restart on bot
systemctl status mysqld //Check MySQL service running status
How to Install MySQL 8 on Amazon Linux 2 Instance 6

Congratulation !!!

You have successfully installed MySQL 8 on your Amazon Linux 2 EC2 Instance. As per above, MySQL service is running fine.

Step 7: Find the Initial root Password

On the initial start-up of the MySQL server, A superuser account 'root'@'localhost is created. A password for the superuser is set and stored in the error log file. To reveal it, use the following command:

sudo grep 'temporary password' /var/log/mysqld.log
How to Install MySQL on Amazon Linux 2 Instance 7

Keep this password handy, as we’ll need it in the next step to secure MySQL installation.

Step 8: Secure MySQL Installation

It’s very important to secure the MySQL installation to improve the security of the MySQL server.

You can do so using-

sudo mysql_secure_installation -p

Once you enter the above command, it will ask you for a password. Enter the temporary password we got in the last step. the cursor does move while you type the password, so just enter the password and hit enter.

It will ask for a new password as we used a temporary password. Enter a new password

And all the other settings as per your need. Here is what I choose-

[ec2-user@ip-172-31-8-242 ~]$ sudo mysql_secure_installation -p
Enter password: 

Securing the MySQL server deployment.


The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) :

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 
[ec2-user@ip-172-31-8-242 ~]$ 

Step 9: Connect to MySQL using the root user

Let’s verify the MySQL connection. Use the below command to connect as root user.

mysql -u root -p 

Enter your password and you should be connected. Once you see the MySQL prompt, create a simple database and list the databases available on our Amazon Linux 2.

[ec2-user@ip-172-31-8-242 ~]$ mysql -u root -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 8.0.34 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE IF NOT EXISTS cloudkatha;
Query OK, 1 row affected (0.00 sec)

mysql> use cloudkatha
Database changed
mysql> SHOW DATABASES; 
+--------------------+
| Database           |
+--------------------+
| cloudkatha         |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

Did you notice cloudkatha database was created?

It’s all good and working. Woohoo !!!

Step 10: Change root Password on need(Optional)

Connect to your MySQL and use the below command-

ALTER USER 'root'@'localhost' IDENTIFIED BY 'Demo@Password1';
Set Password for MySQL

And you should be good to go.

Conclusion

In this how-to guide, we talked about How to Install MySQL 8 on Amazon Linux 2 Instance.

MySQL is not available on the core repository as AWS recommends Maria DB. We have added the MySQL yum repository from the official site and installed it from there.

After the installation, we found the initial password and secured our Mysql installation using that password.

We finally connected to MYSQL and created a database of our choice.

Were you able to install MySQL 8 on Amazon Linux 2 using this tutorial? Let me know in the comment section.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate us-

Suggested Read:

Leave a Reply

Your email address will not be published. Required fields are marked *