How to Install MySQL 8 on Amazon Linux 2023

How to Install MySQL 8 on Amazon Linux 2023

How to Install MySQL 8 on Amazon Linux 2023

Dear Reader, I hope you are doing well today. In my last few posts, I have shared with you how to install GitAnsibleNginxApache Web ServerDockerJavaPythonPHP 8.1 etc. on Amazon Linux 2023 or AL2023 EC2 Instance.

Today, you’ll learn how to install MySQL 8 on Amazon Linux 2023 EC2 instance in a step-by-step manner. I will also share the bash script to install MySQL on Amazon Linux 2023 by the end of this tutorial.

Shall we start?

Alright, let’s go !!!

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 2023(AL2023)

MySQL: MySQL is an open-source relational database management system or RDBMS. It is the most popular RDBMS in the world. As you might already know, It is based on Structured Query Language (SQL).

  • You can store data for different types of apps for example web applications, e-commerce applications, social media sites etc.
  • It is very easy to use and manage and most of us are familiar with it.
  • 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 2023: Amazon Linux 2023 or AL2023 is the next-generation Amazon Linux AMI provided by AWS. It is rpm-based and optimized for the cloud. It is designed with the latest Linux innovations to deliver high-performance and stable Linux systems.

Prerequisite

Before you can run MySQL or to be specific MySQL 8 on an Amazon Linux 2023 EC2 instance, you need one up and running.

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

Steps to Install MySQL 8 on Amazon Linux 2023 Instance

  1. SSH into your Amazon Linux 2023 EC2 Instance
  2. Search for MySQL
  3. Update Package Repository to Latest
  4. Add MySQL Repository to AL2023
  5. Install MySQL 8 on Amazon Linux 2023 Instance
  6. Verify MySQL Installation
  7. Start and Enable MySQL Service
  8. Post Installation Configuration

Step 1: SSH into your Amazon Linux 2023 EC2 Instance

We already have an Amazon Linux 2023 instance up and running. Therefore we are all set to install MySQL on it. However, If that’s not the case, feel free to launch one using one of my previous tutorials.

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

How to Install MySQL 8 on Amazon Linux 2023 0

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: Search for MySQL on Amazon Linux 2023

If you search for MySQL using the below command-

dnf search mysql

It resolves to MariaDB- a community-developed, commercially supported fork of MySQL as you can see below-

How to Install MySQL 8 on Amazon Linux 2023 1

The reason is that AWS recommends using MariaDB and hence doesn’t provide MySQL in the official core repository.

However, not everyone wants to go ahead with MariaDB. So If you are also here to install MySQL instead of Maria DB on your Amazon Linux 2023, you are at the right place.

Step 3: Update the Package Repository to the Latest

Before you install any software for example MySQL in your Amazon Linux 2023 instance, it’s recommended to update your system packages to the latest.

You can run the system update using the below command-

sudo dnf update

Type sudo dnf update and hit enter.

Your system gets updated with the latest packages.

Step 4: Add MySQL Repository to AL2023

As we just said AWS doesn’t provide mysql in the core repo. However, MySQL provides a yum repository that you can use to install MySQL 8 on your Amazon Linux 2023.

sudo wget https://dev.mysql.com/get/mysql80-community-release-el9-4.noarch.rpm
sudo dnf install mysql80-community-release-el9-4.noarch.rpm
Installed MySQL Repo

Let’s install it now-

How to Install MySQL 8 on Amazon Linux 2023 4

Once, installed you can verify it by

dnf repolist enabled
How to Install MySQL 8 on Amazon Linux 2023 5

Step 5: Install MySQL 8 on Amazon Linux 2023

All set to install MySQL 8 on Amazon Linux 2023-

sudo dnf install mysql-community-server

Type the above command and hit enter.

Type y when the prompt asks for it.

A prompt will also ask you to import a key so that authenticity can be checked. Say yes again as shown below-

[ec2-user@ip-172-31-5-48 ~]$ sudo dnf install mysql-community-server
MySQL 8.0 Community Server                                                                                           3.0 MB/s | 1.0 MB     00:00    
MySQL Connectors Community                                                                                            86 kB/s |  27 kB     00:00    
MySQL Tools Community                                                                                                865 kB/s | 366 kB     00:00    
Dependencies resolved.
=====================================================================================================================================================
 Package                                          Architecture             Version                         Repository                           Size
=====================================================================================================================================================
Installing:
 mysql-community-server                           x86_64                   8.0.34-1.el9                    mysql80-community                    49 M
Installing dependencies:
 mysql-community-client                           x86_64                   8.0.34-1.el9                    mysql80-community                   3.5 M
 mysql-community-client-plugins                   x86_64                   8.0.34-1.el9                    mysql80-community                   1.4 M
 mysql-community-common                           x86_64                   8.0.34-1.el9                    mysql80-community                   554 k
 mysql-community-icu-data-files                   x86_64                   8.0.34-1.el9                    mysql80-community                   2.3 M
 mysql-community-libs                             x86_64                   8.0.34-1.el9                    mysql80-community                   1.4 M

Transaction Summary
=====================================================================================================================================================
Install  6 Packages

Total download size: 58 M
Installed size: 334 M
Is this ok [y/N]: y
Downloading Packages:
(1/6): mysql-community-common-8.0.34-1.el9.x86_64.rpm                                                                6.3 MB/s | 554 kB     00:00    
(2/6): mysql-community-client-plugins-8.0.34-1.el9.x86_64.rpm                                                         12 MB/s | 1.4 MB     00:00    
(3/6): mysql-community-client-8.0.34-1.el9.x86_64.rpm                                                                 24 MB/s | 3.5 MB     00:00    
(4/6): mysql-community-icu-data-files-8.0.34-1.el9.x86_64.rpm                                                         30 MB/s | 2.3 MB     00:00    
(5/6): mysql-community-libs-8.0.34-1.el9.x86_64.rpm                                                                   24 MB/s | 1.4 MB     00:00    
(6/6): mysql-community-server-8.0.34-1.el9.x86_64.rpm                                                                 72 MB/s |  49 MB     00:00    
-----------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                 70 MB/s |  58 MB     00:00     
MySQL 8.0 Community Server                                                                                           3.0 MB/s | 3.1 kB     00:00    
Importing GPG key 0x3A79BD29:
 Userid     : "MySQL Release Engineering <[email protected]>"
 Fingerprint: 859B E8D7 C586 F538 430B 19C2 467B 942D 3A79 BD29
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                             1/1 
  Installing       : mysql-community-common-8.0.34-1.el9.x86_64                                                                                  1/6 
  Installing       : mysql-community-client-plugins-8.0.34-1.el9.x86_64                                                                          2/6 
  Installing       : mysql-community-libs-8.0.34-1.el9.x86_64                                                                                    3/6 
  Running scriptlet: mysql-community-libs-8.0.34-1.el9.x86_64                                                                                    3/6 
  Installing       : mysql-community-client-8.0.34-1.el9.x86_64                                                                                  4/6 
  Installing       : mysql-community-icu-data-files-8.0.34-1.el9.x86_64                                                                          5/6 
  Running scriptlet: mysql-community-server-8.0.34-1.el9.x86_64                                                                                  6/6 
  Installing       : mysql-community-server-8.0.34-1.el9.x86_64                                                                                  6/6 
  Running scriptlet: mysql-community-server-8.0.34-1.el9.x86_64                                                                                  6/6 
  Verifying        : mysql-community-client-8.0.34-1.el9.x86_64                                                                                  1/6 
  Verifying        : mysql-community-client-plugins-8.0.34-1.el9.x86_64                                                                          2/6 
  Verifying        : mysql-community-common-8.0.34-1.el9.x86_64                                                                                  3/6 
  Verifying        : mysql-community-icu-data-files-8.0.34-1.el9.x86_64                                                                          4/6 
  Verifying        : mysql-community-libs-8.0.34-1.el9.x86_64                                                                                    5/6 
  Verifying        : mysql-community-server-8.0.34-1.el9.x86_64                                                                                  6/6 
=====================================================================================================================================================
WARNING:
  A newer release of "Amazon Linux" is available.

  Available Versions:

  Version 2023.1.20230628:
    Run the following command to upgrade to 2023.1.20230628:

      dnf upgrade --releasever=2023.1.20230628

    Release notes:
     https://docs.aws.amazon.com/linux/al2023/release-notes/relnotes.html

  Version 2023.1.20230629:
    Run the following command to upgrade to 2023.1.20230629:

      dnf upgrade --releasever=2023.1.20230629

    Release notes:
     https://docs.aws.amazon.com/linux/al2023/release-notes/relnotes.html

  Version 2023.1.20230705:
    Run the following command to upgrade to 2023.1.20230705:

      dnf upgrade --releasever=2023.1.20230705

    Release notes:
     https://docs.aws.amazon.com/linux/al2023/release-notes/relnotes.html

  Version 2023.1.20230719:
    Run the following command to upgrade to 2023.1.20230719:

      dnf upgrade --releasever=2023.1.20230719

    Release notes:
     https://docs.aws.amazon.com/linux/al2023/release-notes/relnotes.html

  Version 2023.1.20230725:
    Run the following command to upgrade to 2023.1.20230725:

      dnf upgrade --releasever=2023.1.20230725

    Release notes:
     https://docs.aws.amazon.com/linux/al2023/release-notes/relnotes.html

  Version 2023.1.20230809:
    Run the following command to upgrade to 2023.1.20230809:

      dnf upgrade --releasever=2023.1.20230809

    Release notes:
     https://docs.aws.amazon.com/linux/al2023/release-notes/relnotes.html

  Version 2023.1.20230825:
    Run the following command to upgrade to 2023.1.20230825:

      dnf upgrade --releasever=2023.1.20230825

    Release notes:
     https://docs.aws.amazon.com/linux/al2023/release-notes/relnotes.html

=====================================================================================================================================================

Installed:
  mysql-community-client-8.0.34-1.el9.x86_64          mysql-community-client-plugins-8.0.34-1.el9.x86_64  mysql-community-common-8.0.34-1.el9.x86_64 
  mysql-community-icu-data-files-8.0.34-1.el9.x86_64  mysql-community-libs-8.0.34-1.el9.x86_64            mysql-community-server-8.0.34-1.el9.x86_64 

Complete!
[ec2-user@ip-172-31-5-48 ~]$ 

Step 6: Verify MySQL Installation

mysql -V

As you can see MySQL 8 is successfully installed on Amazon Linux 2023. We verified the version using mysql -V.

Step 7: Start and Enable MySQL

As of now, MySQL is just installed however it’s not yet running.

You can run the below set of commands to start and enable it to auto-start on boot.

sudo systemctl start mysqld //Starts MySQL service
sudo 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 2023 7

Congratulation !!!

You have successfully installed and started MySQL 8 on your Amazon Linux 2023 EC2 Instance.

Step 8: Post Installation Configuration of MySQL

Once installation is done, you should-

  1. Get Initial Password
  2. Secure MySQL Installation
  3. Connect to MySQL and run a simple query

1. Get Initial Password

sudo grep 'temporary password' /var/log/mysqld.log

2. Secure MySQL Installation

You must secure your MySQL to enhance the overall security of your database.

sudo mysql_secure_installation -p

Provide answers to questions as shown below or as per your need-

[ec2-user@ip-172-31-5-48 ~]$ 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) : n

 ... 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-5-48 ~]$ 

3. Connect to MySQL and run a simple query

mysql -u root -p 

Enter the password we set for the root user in the last step and hit enter. You see have the mysql prompt. Run the command of your choice. For example, I have created a database and then listed them using the below commands-

CREATE DATABASE IF NOT EXISTS demodb;
SHOW DATABASES;
[ec2-user@ip-172-31-5-48 ~]$ mysql -u root -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
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 demodb;
Query OK, 1 row affected, 1 warning (0.01 sec)

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

mysql> 

We are successfully able to run queries on MySQL DB running on Amazon Linux 2023.

Final Script to Install MySQL on Amazon Linux 2023-

#!/bin/bash  

echo "Installing mySQL on Amazon Linux 2023"  
sudo dnf update
sudo wget https://dev.mysql.com/get/mysql80-community-release-el9-4.noarch.rpm
sudo dnf install mysql80-community-release-el9-4.noarch.rpm
sudo dnf install mysql-community-server

Conclusion

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

We saw that MySQL 8 is not available in the core repository of Amazon Linux 2023. As Amazon recommends MariaDB. We added the Yum repository provided by MySQL official and installed MySQL after that.

Later, we did post-installation configuration and ran a few commands to create a database and list them.

Were you able to install MySQL 8 on Amazon Linux 2023 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 *