如何在Ubuntu 20.04上安装MySQL 8.0

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

今日教程介绍了在Ubuntu 20.04上安装MySQL 8.0的过程。 MySQL是最常用的数据库管理系统之一。它使用关系数据库的概念,并具有客户端/服务器体系结构。它可以安装在各种操作系统上,包括Windows,CentOS和Debian。

在Ubuntu 20.04上安装MySQL 8.0

以下步骤描述了如何在Ubuntu 20.04上安装和配置MySQL 8.0。

在Ubuntu中添加MySQL APT存储库

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

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 8.0服务器并单击OK。

默认情况下,下一个提示选择MySQL8. 选择最后一个选项确定,然后单击确定。

在Ubuntu 20.04上更新MySQL系统信息库

运行以下命令以更新系统软件包索引列表。

sudo apt-get update

现在使用apt缓存搜索MySQL 8.0,如下所示:

$sudo apt-cache policy mysql-server
mysql-server: 
 Candidate: 8.0.21-1ubuntu18.04 
 Version table: 
    8.0.21-1ubuntu18.04 500 
       500 http://repo.mysql.com/apt/ubuntu bionic/mysql-8.0 amd64 Packages 
    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 100 
       100 /var/lib/dpkg/status

在Ubuntu 20上安装MySQL 8.0

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

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

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

安全的MySQL安装

运行以下命令以保护MySQL

$sudo mysql_secure_installation

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

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                  file 

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版本

连接到MySQL以检查MySQL的安装版本。要连接到MySQL,请运行以下命令:

mysql-u root -p

提供上面设置的root密码,一旦连接,执行以下命令以显示MySQL版本。

SELECT VERSION()
| VERSION() | 
+-----------+ 
| 8.0.21    | 
+-----------+ 
1 row in set (0.00 sec)

创建MySQL用户(可选)

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

CREATE USER 'lorna'@'%' IDENTIFIED BY 'MyStrongPass.';
GRANT ALL PRIVILEGES ON * . * TO 'lorna'@'%'; 
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