How to Install MySQL 5.7 on Amazon Linux 2

In this article, We are going to perform, How to Install MySQL 5.7 on Amazon Linux 2, Download MySQL 5.7 Yum Repository using wget, Install MySQL 5.7 Yum Repository on Amazon Linux 2, Install MySQL 5.7 on Amazon Linux 2.

Get MySQL 5.7 default password on Amazon Linux 2, Secure MySQL Installation on Amazon Linux 2, Login to MySQL on Amazon Linux 2, Create MySQL Remote User, How to Enable MySQL Remote Access, Start/Restart/Stop MySQL 5.7 on Amazon Linux 2

Introduction

MySQL is the world’s most popular open source relational database management system. It is widely used with web server like apache2,Nginx,IIS,etc.

Prerequisite

  • Amazon Linux 2 SSH Access with root privilege

Login to Amazon Linux 2 using IP, key pair and ec2-user

update the system packages

sudo yum update

#1: Download MySQL 5.7 Yum Repository using wget

First Download the MySQL 5.7 yum repository on Amazon Linux 2 using wget

sudo wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

Output:

https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm [following]
--2020-09-26 06:14:25--  https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 104.120.171.24
Connecting to repo.mysql.com (repo.mysql.com)|104.120.171.24|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25680 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-11.noarch.rpm’

100%[===========================================================================================================================>] 25,680      --.-K/s   in 0.01s

‘mysql57-community-release-el7-11.noarch.rpm’ saved [25680/25680]

#2: Install MySQL 5.7 Yum Repository on Amazon Linux 2

Install MySQL 5.7 Yum Repository on Amazon Linux 2 using below command

sudo yum localinstall mysql57-community-release-el7-11.noarch.rpm

Output:

Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Examining mysql57-community-release-el7-11.noarch.rpm: mysql57-community-release-el7-11.noarch
Marking mysql57-community-release-el7-11.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql57-community-release.noarch 0:el7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================================================
 Package                                      Arch                      Version                    Repository                                                   Size
=====================================================================================================================================================================
Installing:
 mysql57-community-release                    noarch                    el7-11                     /mysql57-community-release-el7-11.noarch                     31 k

Transaction Summary
=====================================================================================================================================================================
Install  1 Package

Total size: 31 k
Installed size: 31 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql57-community-release-el7-11.noarch                                                                                                           1/1
  Verifying  : mysql57-community-release-el7-11.noarch                                                                                                           1/1

Installed:
  mysql57-community-release.noarch 0:el7-11

Complete!

#3: Install MySQL 5.7 on Amazon Linux 2

Install MySQL 5.7 on Amazon Linux 2 using below commands

sudo yum install mysql-community-server

Output:

Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
 Userid     : "MySQL Release Engineering <[email protected]>"
 Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 Package    : mysql57-community-release-el7-11.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-common-5.7.31-1.el7.x86_64                                                                                                        1/7
  Installing : mysql-community-libs-5.7.31-1.el7.x86_64                                                                                                          2/7
  Installing : ncurses-compat-libs-6.0-8.20170212.amzn2.1.3.x86_64                                                                                               3/7
  Installing : mysql-community-client-5.7.31-1.el7.x86_64                                                                                                        4/7
  Installing : mysql-community-server-5.7.31-1.el7.x86_64                                                                                                        5/7
  Installing : mysql-community-libs-compat-5.7.31-1.el7.x86_64                                                                                                   6/7
  Erasing    : 1:mariadb-libs-5.5.64-1.amzn2.x86_64                                                                                                              7/7
  Verifying  : mysql-community-libs-compat-5.7.31-1.el7.x86_64                                                                                                   1/7
  Verifying  : ncurses-compat-libs-6.0-8.20170212.amzn2.1.3.x86_64                                                                                               2/7
  Verifying  : mysql-community-common-5.7.31-1.el7.x86_64                                                                                                        3/7
  Verifying  : mysql-community-server-5.7.31-1.el7.x86_64                                                                                                        4/7
  Verifying  : mysql-community-client-5.7.31-1.el7.x86_64                                                                                                        5/7
  Verifying  : mysql-community-libs-5.7.31-1.el7.x86_64                                                                                                          6/7
  Verifying  : 1:mariadb-libs-5.5.64-1.amzn2.x86_64                                                                                                              7/7

Installed:
  mysql-community-libs.x86_64 0:5.7.31-1.el7         mysql-community-libs-compat.x86_64 0:5.7.31-1.el7         mysql-community-server.x86_64 0:5.7.31-1.el7

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.31-1.el7       mysql-community-common.x86_64 0:5.7.31-1.el7       ncurses-compat-libs.x86_64 0:6.0-8.20170212.amzn2.1.3

Replaced:
  mariadb-libs.x86_64 1:5.5.64-1.amzn2

Complete!

start the MySQL 5.7 service on Amazon Linux 2

sudo systemctl start mysqld.service

Check the MySQL 5.7 service status on Amazon Linux 2

sudo systemctl status mysqld.service

Output:

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-09-26 06:16:22 UTC; 5s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 3910 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 3861 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 3915 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─3915 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

#4: Get MySQL 5.7 default password on Amazon Linux 2

Run below command to get MySQL 5.7 default password on Amazon Linux 2

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

Output:

[Note] A temporary password is generated for [email protected]: *-g-w/daw4rH

#5: Secure MySQL Installation on Amazon Linux 2

MySQL Server comes with a script mysql_secure_installation this can do multiple security related operations.

You have to enter yes or No to below security features,

  • Change the password for root ? ((Press Y|Y for Yes, any other key for No)
  • Remove anonymous users? (Press Y|Y for Yes, any other key for No)
  • Disallow root login remotely? (Press Y|Y for Yes, any other key for No)
  • Remove test database and access to it? (Press Y|Y for Yes, any other key for No)
  • Reload privilege tables now? (Press Y|Y for Yes, any other key for No)
 mysql_secure_installation 

copy default password , Enter Password for root user and set new password.

Output:

mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:

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

New password:

Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
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) : 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!

#6: Login to MySQL on Amazon Linux 2

Now, Login to MySQL 5.7 Server using below command and use password to login entered during installation.

mysql -u root -p

Output:

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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.

#7: Create MySQL Remote User

First, Login to MySQL Server with root user using command line, Below is command is to create user , here i am creating user “devopshint“.

mysql> CREATE USER 'devopshint'@'%' IDENTIFIED BY '[email protected]';

Next, assign the privileges to database with below command , here i am assigning all databases privileges to user fosstechnix,

If you want to assign privileges to specific database replace ” .” with database name.

mysql> GRANT ALL PRIVILEGES ON * . * TO 'devopshint'@'%';

OR

if you want to allow “devopshint” user  to give privileges to other user.

 mysql> GRANT ALL PRIVILEGES ON *.* TO 'devopshint'@'%' WITH GRANT OPTION;

To take effect reload the privileges using below command,

mysql> FLUSH PRIVILEGES;

Exit from MySQL prompt

mysql> exit

#8: How to Enable MySQL Remote Access

By default, In MySQL database server remote access is disabled for security reason.

To enable remote connections of MySQL Server, we have to change bind-address in MySQL configuration file.

Open the /etc/my.cnf file

sudo nano /etc/my.cnf

Add the line at [mysqld] section

[mysqld]

bind-address  = 0.0.0.0

Restart the MySQL Server to take effect.

sudo systemctl restart mysqld.service

#9: Start/Restart/Stop MySQL 5.7 on Amazon Linux 2

Below are commands to start/restart/stop MySQL 5.7 on Ubuntu 20.04 LTS

sudo systemctl start mysqld.service
sudo systemctl restart mysqld.service
sudo systemctl stop mysqld.service

Conclusion:

In this article, We have performed ,Download MySQL 5.7 Yum Repository using wget, Install MySQL 5.7 Yum Repository on Amazon Linux 2, Install MySQL 5.7 on Amazon Linux 2.

Get MySQL 5.7 default password on Amazon Linux 2, Secure MySQL Installation on Amazon Linux 2, Login to MySQL on Amazon Linux 2, Create MySQL Remote User, How to Enable MySQL Remote Access, Start/Restart/Stop MySQL 5.7 on Amazon Linux 2

Related Articles:

How to connect to AWS EC2 Instance using MobaXTerm

How to Enable Password Authentication for AWS EC2

How to Connect EC2 Instance using Putty

How to Transfer files to AWS Instance using WinSCP [2 Steps]

How to Create AWS DocumentDB and Connect [3 steps]

20 AWS Interview Questions and Answers

push docker image to AWS ECR

Reference:

AWS Official Guide

About DevOps Hint

DevOps Hint Founded in 2020 Community Site where you can find about How to Guides, Articles and Troubleshooting Tips for DevOps, SRE, Sysadmins and Developers.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link