Ubuntu 20.04 安装 MySQL 5.7

Installing MySQL 5.7 on Ubuntu 20.04 (Focal Fossa)

The below steps describe how to install and configure MySQL 5.7 on Ubuntu 20.04. It start with adding APT repository with packages for MySQL then dives to the actual package installations and configurations.

Step 1: Add MySQL APT repository in Ubuntu

Install the required packages.

1 apt-get update
2 apt-get install wget

Download and install the MySQL repository package.

1 mkdir /downloads
2 cd /downloads
3 wget wget https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb
4 dpkg -i mysql-apt-config_0.8.12-1_all.deb

Select the option UBUNTU BIONIC.

Ubuntu 20.04 安装 MySQL 5.7

On the MySQL Server and Cluster screen, select the option MYSQL-5.7.

Ubuntu 20.04 安装 MySQL 5.7

In the next prompt, select MySQL 5.7 server and click OK.

Ubuntu 20.04 安装 MySQL 5.7

The next prompt selects MySQL5.7 by default. Choose the last otpion Ok and click OK

Ubuntu 20.04 安装 MySQL 5.7

Step 2: Update MySQL Repository on Ubuntu

Run the below command to update your system packages

1 sudo apt-get update

Verify the Ubuntu policy for the MySQL package installation.

1 apt-cache policy mysql-server

Here is the command output.

 1 mysql-server:
 2   Installed: (none)
 3   Candidate: 8.0.22-0ubuntu0.20.04.2
 4   Version table:
 5      8.0.22-0ubuntu0.20.04.2 500
 6         500 http://br.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
 7         500 http://br.archive.ubuntu.com/ubuntu focal-security/main amd64 Packages
 8      8.0.19-0ubuntu5 500
 9         500 http://br.archive.ubuntu.com/ubuntu focal/main amd64 Packages
10      5.7.32-1ubuntu18.04 500
11         500 http://repo.mysql.com/apt/ubuntu bionic/mysql-5.7 amd64 Packages

In our example, the system offers the option to install the MySQL version 5.7.32.

Take note of the MySQL version you want to install.

As a test, simulate the installation of the MySQL 5.7 client package.

1 apt-get install -s mysql-client=5.7.32-1ubuntu18.04

If not errors were detected, perform the installation of the MySQL 5.7 client package.

apt-get install mysql-client=5.7.32-1ubuntu18.04

Optionally, simulate the installation of the remaining MySQL packages.

1 apt-get install -s mysql-community-server=5.7.32-1ubuntu18.04
2 apt-get install -s mysql-server=5.7.32-1ubuntu18.04

Install the MySQL server version 5.7.

1 apt-get install mysql-community-server=5.7.32-1ubuntu18.04
2 apt-get install mysql-server=5.7.32-1ubuntu18.04

Configure the password to the MySQL account named ROOT.

Ubuntu 20.04 安装 MySQL 5.7

Step 3: Secure MySQL 5.7 Installation on Ubuntu 20.04

Run the command below to secure MySQL
1 sudo mysql_secure_installation

Answer the prompts as below:

 1 Securing the MySQL server deployment.
 2 
 3 Enter password for user root: 
 4 
 5 VALIDATE PASSWORD PLUGIN can be used to test passwords
 6 and improve security. It checks the strength of password
 7 and allows the users to set only those passwords which are
 8 secure enough. Would you like to setup VALIDATE PASSWORD plugin?
 9 
10 Press y|Y for Yes, any other key for No: Y
11 
12 There are three levels of password validation policy:
13 
14 LOW    Length >= 8
15 MEDIUM Length >= 8, numeric, mixed case, and special characters
16 STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file
17 
18 Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
19 Using existing password for root.
20 
21 Estimated strength of the password: 50 
22 Change the password for root ? ((Press y|Y for Yes, any other key for No) : s
23 
24  ... skipping.
25 By default, a MySQL installation has an anonymous user,
26 allowing anyone to log into MySQL without having to have
27 a user account created for them. This is intended only for
28 testing, and to make the installation go a bit smoother.
29 You should remove them before moving into a production
30 environment.
31 
32 Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
33 Success.
34 
35 
36 Normally, root should only be allowed to connect from
37 'localhost'. This ensures that someone cannot guess at
38 the root password from the network.
39 
40 Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
41 Success.
42 
43 By default, MySQL comes with a database named 'test' that
44 anyone can access. This is also intended only for testing,
45 and should be removed before moving into a production
46 environment.
47 
48 
49 Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
50  - Dropping test database...
51 Success.
52 
53  - Removing privileges on test database...
54 Success.
55 
56 Reloading the privilege tables will ensure that all changes
57 made so far will take effect immediately.
58 
59 Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
60 Success.
61 
62 All done! 

Check MySQL version.

To confirm the installed version, first, connect to MySQL with the set root password.

mysql -u root -p 

Run the below command to display version

$ SELECT VERSION();
+-----------+ 
| VERSION() | 
+-----------+ 
| 5.7.31    | 
+-----------+ 
1 row in set (0.00 sec)

Step 4: Create MySQL User (Optional, testing only)

Step 5: Enable MySQL remote access (Optional)

By default, MySQL remote access is disabled. To enable it we need to edit mysqld.cnf file as below:

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

Look for the line ‘bind_address’ and change as below:

# By default we only accept connections from localhost 
#bind-address   = 127.0.0.1 
bind-address   = 0.0.0.0

Save the file and restart mysql

sudo systemctl restart mysql

Allow remote connections through the firewall

1 sudo ufw allow from <remote_IP_address> to any port 3306
2 sudo ufw enable

To access the database from a remote machine, run the following command:

1 mysql -u user -h database_server_ip -p

You have successfully installed MySQL 5.7 on Ubuntu 20.04.

Check more interesting Linux guides below: