How to Install MySQL on Amazon EC2 Linux

In this tutorial, you will learn how to install MySQL on Amazon EC2 Linux machine. If you’re new to this or have encountered issues with previous installations, you’re in the right place.

If you want to learn how to run MySQL in a Docker container instead, then read this tutorial: How to start MySQL in a Docker container.


  • An Amazon EC2 instance running Amazon Linux.
  • Sudo privileges on the instance.

Also, before following the below instructions, please connect to you EC2 instance. Because the following commands, need to be executed on EC2 instance. Not on your local computer.

Step 1: Download MySQL Server

First, you’ll need to download MySQL Community Server. To do this, execute the following command on AWS EC2 instance:

sudo yum install

This command installs the MySQL repository from the provided URL. It’s important because this repository contains the latest version of MySQL specifically packaged for Amazon Linux.

Step 2: Install MySQL Community Server

Now that you have downloaded MySQL server, it is time to install it. To install MySQL Server run the following command:

sudo yum install mysql-community-server

This command should install the MySQL Community Server on you Amazon EC2 Linux machine. But we are not done it. We still need to enable MySQL server to make it start automatically.

Step 3: Enable MySQL Server

After installation, it’s essential to ensure that MySQL starts automatically when your EC2 instance boots up. Use this command to enable it:

sudo systemctl enable --now mysqld

This command not only enables MySQL to start at boot but also starts the MySQL service immediately.

Step 4: Locate the Temporary MySQL Root Password

Now that MySQL is up and running, you will want to login to it. To do that you will need to use a temporary root password.

MySQL generates a temporary password for the root user upon installation. To find this password, run:

sudo mysql_secure_installation

This utility helps you set a new root password, remove anonymous users, restrict root user access to the local machine, and remove test databases. It’s a crucial step in securing your MySQL server.

When prompted, enter the temporary password you retrieved earlier, and then follow the instructions to set up your new root password and other security settings.

Step 5: Create a New MySQL User

Now that you are logged in as a root user, it is good to create a new user or additional user for working with a database. Let’s create a new user named “sergey” with a secure alphanumeric password.

Run the following command, replacing your_password with a secure alphanumeric password of your choice:

CREATE USER 'sergey'@'localhost' IDENTIFIED BY 'your_password';

This command creates a new user named ‘sergey’ who will be able to connect to MySQL from the local machine. The @'localhost' part specifies that this user can only connect from the localhost. If you want ‘sergey’ to connect from any host, you can replace 'localhost' with '%'.

Grant Privileges

After creating the user, you need to grant them the necessary privileges. For instance, if you want ‘sergey’ to have full access to a database named ‘exampledb’, use this command:

GRANT ALL PRIVILEGES ON exampledb.* TO 'sergey'@'localhost';

This command grants ‘sergey’ all privileges on all tables within the ‘exampledb’ database. Adjust the database name and privileges according to your needs.

Apply Changes

To ensure the changes take effect, run:


This command reloads the grant tables, making all your changes effective immediately.

Step 6. Exit MySQL

Once you are done working with MySQL, you will want to exit it. To properly exit MySQL type the follow command and hit enter:



I hope this tutorial was helpful to you in setting up MySQL on your Amazon EC2 Linux instance.

If you are using MySQL for Spring Boot applications and Microservices, check out my other MySQL-related tutorials for more in-depth guidance and best practices.

Happy coding!