How to Install MySQL 5.7 on Ubuntu 20.04 LTS

In this article, We are going to Perform How to install MySQL 5.7 on Ubuntu 20.04 LTS using command line, creating new user and enabled remote access.

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.

MySQL has client/Server architecture , supports InnoDB storage engine,can be installed on various operating system like Ubuntu,Debian,CentOS,Windows.,etc.

Step #1: Add MySQL APT Repository in Ubuntu

Ubuntu comes with default package repositories. So,if we want to add/install latest repositories then we have to add/install package repositories.

Update the System Packages

sudo apt update

Install wget on ubuntu if not installed

sudo apt install wget -y

Below are commands to add/download latest MySQL APT repository using command line,

wget https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb

then, below command is to install above downloaded apt repository,

sudo dpkg -i mysql-apt-config_0.8.12-1_all.deb

Select Ubuntu Bionic option and click on Ok.

select ubuntu version

By default it shows MySQL 8.0, Click on First option .

select mysql 5.7 option

Select MySQL 5.7 server and click on OK.

select mysql 5.7

Confirm that showing MySQL 5.7 on First option and Click on OK.

click on ok

Step #2: Update MySQL Repository

Update apt repository

sudo apt-get update

Search MySQL 5.7 package using MySQL apt cache and select 5.7.30-1ubuntu18.04 to install

sudo apt-cache policy mysql-server

Output:

mysql-server:
Installed: (none)
Candidate: 8.0.25-0ubuntu0.20.04.1
Version table:
8.0.25-0ubuntu0.20.04.1 500
500 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
500 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages
8.0.19-0ubuntu5 500
500 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu focal/main amd64 Packages
5.7.35-1ubuntu18.04 500
500 http://repo.mysql.com/apt/ubuntu bionic/mysql-5.7 amd64 Packages

Now install MySQL client 5.7.33 as output shown above

sudo apt install -f mysql-community-client=5.7.35-1ubuntu18.04 

Step #3: How to Install MySQL 5.7 on Ubuntu 20.04 LTS

Error:

The following packages have unmet dependencies:
mysql-community-server : Depends: mysql-client (= 5.7.35-1ubuntu18.04) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

Solution:

sudo apt install -f mysql-client=5.7.35-1ubuntu18.04

Install MySQL 5.7 on Ubuntu 20.04 LTS using below command,

sudo apt install -f mysql-community-server=5.7.35-1ubuntu18.04

Installation process will prompt default password for root user and again same password.

enter root password for mysql 5.7

confirm password for mysql 5.7

Install mysql-server=5.7.35 package also

sudo apt install -f mysql-server=5.7.35-1ubuntu18.04

Step #4: Secure MySQL Installation

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

Run the below script on command prompt, asks below options.

$ mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Using existing password for root.

Estimated strength of the password: 50
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!

Step #5: Login to MySQL

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

mysql -u root -p

Output:

mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.30 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.

Now, We have successfully covered How to Install MySQL 5.7 on Ubuntu 20.04 LTS.

Step #6: 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 “fosstechnix“.

mysql> CREATE USER 'fosstechnix'@'%' IDENTIFIED BY 'FOSSTechNix@123';

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 'fosstechnix'@'%';

OR

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

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

To take effect reload the privileges using below command,

mysql> FLUSH PRIVILEGES;

Exit from MySQL prompt

mysql> exit

Step #7: How to Enable MySQL Remote Access in Ubuntu 20.04

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/mysql/mysql.conf.d/mysqld.cnf file

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Below the [mysqld] section find the Line,

[mysqld]
bind-address  = 127.0.0.1

And replace it to

 bind-address = 0.0.0.0

Output:

/etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
bind-address = 0.0.0.0
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

Restart the MySQL Server to take effect.

 sudo systemctl restart mysql

Step #8: Start/Restart/Stop MySQL Server Using Command Line

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

sudo systemctl start mysql
sudo systemctl restart mysql
sudo systemctl stop mysql

To check the status of mysql service on ubuntu

sudo systemctl status mysql

Conclusion:

In this article, We have performed ,How to Install MySQL 5.7 on Ubuntu 20.04 LTS System, creating new user and enabled remote access.

Related Articles:

Reference:

MySQL Official Documentation

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

31 thoughts on “How to Install MySQL 5.7 on Ubuntu 20.04 LTS”

  1. I tried to run
    sudo apt install -f mysql-client=5.7.30-1ubuntu18.04
    and it gives me following error:
    Version ‘5.7.30-1ubuntu18.04’ for ‘mysql-client’ was not found.

    You need to run:
    sudo apt install -f mysql-client=5.7.31-1ubuntu18.04

    Reply
  2. Thanks for the article.

    As David suggested,

    Also “sudo apt install -f mysql-community-server=5.7.30-1ubuntu18.04” should be changed to “sudo apt install -f mysql-community-server=5.7.31-1ubuntu18.04”

    Reply
  3. Hello,

    I was runing it to according your article, but I make a mistake. At screen to choose the OS version, I choose “xenial” rather than “bionic”. Now, when I try to repeat the process, I get this message: “Version ‘5.7.30-1ubuntu18.04’ for ‘mysql-client’ was not found”.

    Pease, you’d can help me? Thank you!

    Reply
  4. On the screen to choose OS, I pressed escape key. Now when I try to do it again, I get:
    Preparing to unpack mysql-apt-config_0.8.12-1_all.deb …
    Unpacking mysql-apt-config (0.8.12-1) over (0.8.12-1) …
    Setting up mysql-apt-config (0.8.12-1) …
    Warning: apt-key should not be used in scripts (called from postinst maintainerscript of the package mysql-apt-config)
    OK

    Reply
  5. I am getting error for below install

    sudo apt install -f mysql-client=5.7.31-1ubuntu18.04

    sudo apt install -f mysql-community-server=5.7.31-1ubuntu18.04

    error:
    The following packages have unmet dependencies:
    mysql-server : Depends: mysql-community-server (= 5.7.31-1ubuntu18.04) but it is not going to be installed
    E: Unable to correct problems, you have held broken packages.

    Reply
  6. Hi!
    Great article, we needed this a few times already 🙂

    are you sure that mysql-apt-config_0.8.16-1_all.deb does still work? when I tried it, it does not ask for the Ubuntu version so I had to download mysql-apt-config_0.8.12-1_all.deb (the previous version, I guess)

    Reply
  7. an update && upgrade deinstalled mysql 5.7.33 instead of upgrading to mysql 5.7.34 and I don’t know why…
    anyone else?

    Reply
  8. Hi! Very usefull info. I just did it on Pop!_OS 22.04 LTS. I had some issues with the repository and got the error:

    W: GPG error: http://repo.mysql.com/apt/ubuntu bionic InRelease: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY 467B942D3A79BD29

    So I had to use the most recent version:

    wget https://dev.mysql.com/get/mysql-apt-config_0.8.25-1_all.deb

    And the recent 5.7 version is:

    5.7.42-1ubuntu18.04 500

    With all that I have it up and running. Thanks!

    Reply

Leave a Comment

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

Share via
Copy link
Powered by Social Snap