Many developers face confusion when trying to install MySQL on Amazon Linux 2 AMI. The default package repositories don't include MySQL packages anymore, and the traditional yum install mysql-server
approach fails. Here's how to properly set it up.
First, we need to add the official MySQL repository:
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm
sudo yum update
This enables access to MySQL packages not available in default Amazon Linux 2 repos.
With the repository configured, install MySQL Server 8.0 (current stable version):
sudo yum install mysql-community-server
For specific versions, enable the right sub-repository first:
sudo yum-config-manager --disable mysql80-community
sudo yum-config-manager --enable mysql57-community
sudo yum install mysql-community-server
After installation, start the service and secure your installation:
sudo systemctl start mysqld
sudo systemctl enable mysqld
sudo grep 'temporary password' /var/log/mysqld.log
mysql_secure_installation
If you encounter GPG key errors during installation:
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
For memory-constrained EC2 instances, you may need to adjust MySQL configuration in /etc/my.cnf
before starting the service.
Test your connection with:
mysql -u root -p
Remember to configure appropriate security groups if accessing from external applications.
Many developers face difficulties when trying to install MySQL on Amazon Linux 2, as the default package repositories don't include MySQL packages. When you run sudo yum install mysql56-server
, you'll get package not found errors because:
- Amazon Linux 2 doesn't include MySQL in its default repos
- The
amazon-linux-extras
command doesn't list MySQL as an available option - Amazon prefers MariaDB as the default MySQL-compatible alternative
Here's how to properly install MySQL 8.0 (the current stable version) on Amazon Linux 2:
# Download the MySQL Yum repository
sudo wget https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm
# Install the repository package
sudo yum localinstall mysql80-community-release-el7-5.noarch.rpm
# Verify the repository is enabled
sudo yum repolist enabled | grep "mysql.*-community.*"
# Install MySQL server
sudo yum install mysql-community-server
# Start MySQL service
sudo systemctl start mysqld
# Enable auto-start on boot
sudo systemctl enable mysqld
After installation, you'll need to secure your MySQL instance:
# Get temporary root password
sudo grep 'temporary password' /var/log/mysqld.log
# Run security script
sudo mysql_secure_installation
# Basic configuration example:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourStrongPassword123!';
CREATE DATABASE app_database;
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'UserPassword456!';
GRANT ALL PRIVILEGES ON app_database.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
If you encounter GPG key errors during installation, run:
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
For compatibility issues with older clients:
# Edit MySQL configuration
sudo vi /etc/my.cnf
# Add these lines under [mysqld]
default_authentication_plugin=mysql_native_password
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
For isolated deployments, consider Docker:
sudo yum install docker -y
sudo systemctl start docker
sudo docker run --name=mysql8 -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=securepass mysql:8.0
Remember to configure proper security groups if using AWS EC2 to allow MySQL connections.