更新时间:2022-12-30 10:15:36浏览次数:509+次
本文介绍如何在Ubuntu 22.04操作系统上安装和配置MySQL 8.0版本的方法,类似的方法也适用在Ubuntu 20.04下。MySQL是最常用的数据库管理系统之一,它使用关系数据库的概念,具有客户机/服务器体系结构,当前,MySQL 8.0可以安装在各种操作系统上,包括Windows、CentOS和Debian等。
安装MySQL 8.0的方法
1、添加MySQL APT存储库
Ubuntu 22.04已经附带了MySQL 8.0包,您可以跳过Ubuntu 22.04上的存储库添加,即不需要操作本步骤,直接进入到更新MySQL存储库的操作。或者,如果要在Ubuntu 20.04下安装,那么,只在Ubuntu 20.04上执行以下操作即可。
使用以下命令下载存储库:
wget https://dev.mysql.com/get/mysql-apt-config_0.8.22-1_all.deb
下载后,通过运行以下命令安装存储库:
sudo dpkg -i mysql-apt-config_0.8.22-1_all.deb
如果未检测到操作系统,请选择MySQL ubuntu focal:
下一个提示显示默认选择的MySQL 8.0。选择第一个选项并单击确定:
在下一个提示中,选择MySQL 8.0服务器并单击OK:
默认情况下,下一个提示选择MySQL8。选择最后一个选项确定,然后单击确定。
2、更新MySQL存储库
运行以下命令以更新系统包索引列表:
sudo apt update
现在使用apt缓存搜索MySQL 8.0,如下所示:
### Ubuntu 22.04 ###
$ apt-cache policy mysql-server
mysql-server:
Installed: (none)
Candidate: 8.0.29-0ubuntu0.22.04.2
Version table:
8.0.29-0ubuntu0.22.04.2 500
500 http://ke.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages
500 http://ke.archive.ubuntu.com/ubuntu jammy-updates/main i386 Packages
500 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages
500 http://security.ubuntu.com/ubuntu jammy-security/main i386 Packages
8.0.28-0ubuntu4 500
500 http://ke.archive.ubuntu.com/ubuntu jammy/main amd64 Packages
500 http://ke.archive.ubuntu.com/ubuntu jammy/main i386 Packages
如果是Ubuntu 20.04版本,则如下:
$ sudo apt-cache policy mysql-server
mysql-server:
Installed: (none)
Candidate: 8.0.29-1ubuntu20.04
Version table:
8.0.29-1ubuntu20.04
500 http://repo.mysql.com/apt/ubuntu focal/mysql-8.0 amd64 Packages
3、在Ubuntu22.04上安装MySQL 8.0
Ubuntu 22.04:
sudo apt install mysql-client mysql-server
Ubuntu 20.04:
在我们的系统中找到MySQL 8.0后,我们将使用以下命令安装MySQL 8.0客户端和MySQL 8.0服务器:
sudo apt install mysql-client mysql-community-server mysql-server
按y键开始安装:
Need to get 35.3 MB of archives.
After this operation, 311 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
为MySQL根用户设置强密码:
再次键入root密码以确认:
设置默认身份验证插件。使用强密码加密机制:
配置MySQL 8.0的方法
1、安全安装MySQL
运行以下命令以保护安装在Ubuntu 22.04上的MySQL Server:
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) : n
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!
2、验证MySQL 8.0安装
连接到MySQL以检查MySQL安装的版本。要连接到MySQL,请运行以下命令:
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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.
mysql>
提供上面设置的根密码,一旦连接,执行下面的命令以显示MySQL版本:
mysql> SELECT VERSION();
3、创建MySQL数据库和用户(可选)
当仍然连接到MySQL时,您可以运行以下命令来创建数据库并授予用户权限:
CREATE DATABASE mydb;
CREATE USER 'mydbuser'@'%' IDENTIFIED BY 'DBUserStr0ngPassw0d.';
GRANT ALL PRIVILEGES ON mydb.* TO 'mydbuser'@'%';
FLUSH PRIVILEGES;
exit
4、启用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
到这里,初步配置MySQL 8.0完成。