第一步 — 安装 MySQL
在CentOS 8默认的软件源中提供了MySQL 8,所以我们可以直接安装。
运行以下命令,安装mysql-server
软件包及其依赖项:
sudo dnf install mysql-server
出现确认提示时,输入 y
,然后ENTER
确认以继续:
Output. . .
Install 49 Packages
Total download size: 46 M
Installed size: 252 M
Is this ok [y/N]: y
完成之后,MySQL就已成功安装在我们的服务器上,但是仍处于未运行状态。我们刚安装的MySQL软件包,系统将其配置成名为mysqld.service
的systemd服务。使用以下systemctl
命令启动MySQL:
sudo systemctl start mysqld.service
要检查MySQL服务是否正常运行,运行以下命令查看mysqld服务的状态。虽然MySQL的服务名称是“mysqld.service”,但是我们在使用systemctl
命令的时候,并不需要在mysqld后面带上.service
:
sudo systemctl status mysqld
如果MySQL成功启动,则输出内容更将会显示MySQL服务处于“active(running)”状态:
Output
● mysqld.service - MySQL 8.0 database server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2020-03-12 14:07:41 UTC; 1min 7s ago
Main PID: 15723 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 5056)
Memory: 474.2M
CGroup: /system.slice/mysqld.service
└─15723 /usr/libexec/mysqld --basedir=/usr
Mar 12 14:07:32 cent-mysql-3 systemd[1]: Starting MySQL 8.0 database server...
Mar 12 14:07:32 cent-mysql-3 mysql-prepare-db-dir[15639]: Initializing MySQL database
Mar 12 14:07:41 cent-mysql-3 systemd[1]: Started MySQL 8.0 database server.
接下来,使用以下命令将MySQL设置为随服务器启动而启动:
sudo systemctl enable mysqld
注意:如果不想设置MySQL开机启动,可以通过运行以下命令来设置:
sudo systemctl disable mysqld
现在,服务器上已经安装了MySQL,并且其已处于运行状态。接下来,我们来介绍如何为MySQL设置密码。
第二步 — 增强 MySQL 安全性
MySQL包含一个安全脚本,该脚本允许我们为MySQL设置密码。
运行以下命令:
sudo mysql_secure_installation
这将引导我们完成一系列操作,系统将询问是否要对MySQL安装的安全性选项进行更改。第一步将询问我们是否要设置验证密码。
按“Y”确认我们要设置密码,然后系统将要求我们选择密码验证级别。输入最高级级别2
,该级别要求密码大于8个字符,并包括大写,小写,数字和特殊字符的组合:
Output
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT 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 component?
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: 2
下一步,提示为MySQL 的root用户设置密码。 根据刚才选择的级别2的要求,输入相应的密码:
Output
Please set the password for root here.
New password:
Re-enter new password:
如果我们输入的密码不符合级别2要求的等级,系统会提示重新按要求输入密码,密码应该大于8个字符,并包括大写,小写,数字和特殊字符的组合:
Output
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
然后,按 Y
继续。
系统将询问是否删除匿名用户和测试数据库,是否禁用root远程登录。
现在,我们就已经在CentOS 8服务器上安装了MySQL,并已经为root用户设置了密码。接下来,我们将测试MySQL数据库是否正常工作。
第三步 — 测试 MySQL
使用以下命令以root(-u root
)身份连接到MySQL ,并返回MySQL版本信息:
mysqladmin -u root -p version
我们将看到类似下面的内容输出:
Output
mysqladmin Ver 8.0.17 for Linux on x86_64 (Source distribution)
Copyright (c) 2000, 2019, 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.
Server version 8.0.17
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/lib/mysql/mysql.sock
Uptime: 2 hours 52 min 37 sec
Threads: 2 Questions: 20 Slow queries: 0 Opens: 131 Flush tables: 3 Open tables: 48 Queries per second avg: 0.001
这说明我们的MySQL已经安装成功。
If you’d like to connect to MySQL and begin adding data to it, run the following:
如果想连接到MySQL,并向MySQL中添加数据,执行如下命令:
mysql -u root -p
与mysqladmin
命令类似,此命令也包括 -u
参数,该参数可让我们指定要连接的用户,在本示例中指定要连接的用户为root。后面的 -p
参数,该参数作用是要输入为用户设置的密码。
输入了root 用户密码后,我们将进入MySQL提示符状态:
mysql >
总结
综上所述,我们在Centos 8 系统上成功安装了MySQL,并为 root 用户设置了密码。接下来,我们就可以继续创建数据库等操作。
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/linux/how-to-install-mysql-on-centos8/