如何在Ubuntu 20.04上安装MySQL 5.7

时间:2020-02-23 14:40:58  来源:igfitidea点击:

在今天的教程中,我们正在研究如何在Ubuntu 20.04(Focal Fossa)服务器上安装MySQL 5.7. MySQL是最常用的数据库管理系统之一。它使用关系数据库的概念,并具有客户端/服务器体系结构。它可以安装在各种操作系统上,包括Windows,CentOS和Debian。

在Ubuntu 20.04(Focal Fossa)上安装MySQL 5.7

以下步骤描述了如何在Ubuntu 20.04上安装和配置MySQL 5.7. 首先从为MySQL软件包添加APT存储库开始,然后深入到实际的软件包安装和配置。

在Ubuntu中添加MySQL APT存储库

Ubuntu已经带有默认的MySQL软件包存储库。为了添加或者安装最新的存储库,我们将安装软件包存储库。使用以下命令下载存储库:

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

下载后,通过运行以下命令来安装存储库:

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

在提示中,选择Ubuntu Bionic,然后单击"确定"。

下一个提示显示默认情况下选择的MySQL 8.0。选择第一个选项,然后单击"确定"。

在下一个提示中,选择" MySQL 5.7服务器",然后单击"确定"。

下一个提示默认选择MySQL5.7. 选择最后一个ot,然后单击"确定"。

在Ubuntu上更新MySQL存储库

运行以下命令以更新系统软件包

sudo apt-get update

现在使用apt-cache搜索MySQL 5,7,如下所示:

$sudo apt-cache policy mysql-server
mysql-server: 
 Installed: (none) 
 Candidate: 8.0.21-0ubuntu0.20.04.4 
 Version table: 
    8.0.21-0ubuntu0.20.04.4 500 
       500 http://ke.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages 
       500 http://ke.archive.ubuntu.com/ubuntu focal-security/main amd64 Packages 
    8.0.19-0ubuntu5 500 
       500 http://ke.archive.ubuntu.com/ubuntu focal/main amd64 Packages 
    5.7.31-1ubuntu18.04 500 
       500 http://repo.mysql.com/apt/ubuntu bionic/mysql-5.7 amd64 Packages

如我们所见,MySQl5.7.31-1ubuntu18.04出现在列表中。

在Ubuntu 20.04 Linux机器上安装MySQL 5.7

在我们的系统中找到MySQL 5.7之后,我们将使用以下命令安装MySQL 5.7客户端,MySQL 5.7服务器:

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

按y键开始在Ubuntu 20.04 Linux上安装MySQL 5.7.

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libgdbm-compat4 libmecab2 libperl5.30 libtinfo5 mysql-common mysql-community-client perl perl-modules-5.30
Suggested packages:
  perl-doc libterm-readline-gnu-perl | libterm-readline-perl-perl make libb-debug-perl liblocale-codes-perl
The following NEW packages will be installed:
  libgdbm-compat4 libmecab2 libperl5.30 libtinfo5 mysql-client mysql-common mysql-community-client mysql-community-server mysql-server perl perl-modules-5.30
0 upgraded, 11 newly installed, 0 to remove and 35 not upgraded.
Need to get 58.1 MB of archives.
After this operation, 361 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

出现提示时输入并重新输入root密码

在Ubuntu 20.04上安全地安装MySQL 5.7

运行以下命令以保护MySQL

$sudo mysql_secure_installation

按Enter键。当提示我们输入密码时,请提供上面设置的root密码。

回答如下提示:

Enter current password for root (enter for none): <Enter password>
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                 

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1 
Using existing password for root. 
Estimated strength of the password: 25  
Change the password for root ? ((Press y|Y for Yes, any other key for No) : d
Remove anonymous users? [Y/n] Y 
Disallow root login remotely? [Y/n] Y 
Remove test database and access to it? [Y/n] Y 
Reload privilege tables now? [Y/n] Y 
Thanks for using MariaDB!

检查MySQL版本。

要确认安装的版本,首先,使用设置的root密码连接到MySQL。

$mysql -u root -p

运行以下命令以显示版本

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

创建MySQL用户(可选,仅测试)

在仍然连接到MySQL的同时,运行以下命令来创建用户:

CREATE USER 'user'@'%' IDENTIFIED BY 'MyStrongPass.';
GRANT ALL PRIVILEGES ON * . * TO 'user'@'%'; 
FLUSH PRIVILEGES;
exit

启用MySQL远程访问(可选)

默认情况下,禁用MySQL远程访问。要启用它,我们需要编辑mysqld.cnf文件,如下所示:

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

查找行bind_address并进行如下更改:

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

保存文件并重启mysql

$sudo systemctl restart mysql

允许通过防火墙的远程连接

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

要从远程计算机访问数据库,请运行以下命令:

$mysql -u user -h database_server_ip -p

我们已在Ubuntu 20.04上成功安装MySQL 5.7.