Install MySQL CentOS

摘要:在本教程中,您将逐步学习如何在 CentOS 7 上安装 MySQL 8。

要在 CentOS 7 上安装 MySQL 8,请按照以下步骤操作:

步骤 1. 设置 Yum 存储库

执行以下命令在 CentOS 上启用 MySQL yum 存储库:

rpm -Uvh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpmCode language: JavaScript (javascript)

步骤2.安装MySQL 8社区服务器

由于MySQL yum存储库有多个MySQL版本的多个存储库配置,因此您需要禁用mysql repo文件中的所有存储库:

sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/mysql-community.repoCode language: JavaScript (javascript)

并执行以下命令安装MySQL 8:

yum --enablerepo=mysql80-community install mysql-community-server

步骤3.启动MySQL服务

使用此命令启动 mysql 服务:

service mysqld start

步骤 4. 显示root用户的默认密码

安装 MySQL 8.0 时,root 用户帐户将被授予临时密码。要显示 root 用户帐户的密码,请使用以下命令:

grep "A temporary password" /var/log/mysqld.logCode language: JavaScript (javascript)

这是输出:

[Note] A temporary password is generated for root@localhost: hjkygMukj5+t783

请注意,您的临时密码将会有所不同。您将需要此密码来更改root用户帐户的密码。

步骤5.MySQL安全安装

执行命令 mysql_secure_installation 以保护 MySQL 服务器:

mysql_secure_installation

它将提示您输入 root 帐户的当前密码:

Enter password for user root:

输入上面的临时密码,然后按Enter 。将显示以下消息:

The existing password for the user account root has expired. Please set a new password.

New password:
Re-enter new password:Code language: PHP (php)

您需要输入root帐户的新密码两次。会提示一些问题,建议输入yes(y):

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

步骤6.重启并启用MySQL服务

使用以下命令重新启动mysql服务:

service mysqld restart

并在系统启动时自动启动 mysql 服务:

chkconfig mysqld on

步骤 7. 连接到 MySQL

使用此命令连接到 MySQL 服务器:

mysql -u root -p

它将提示您输入root用户的密码。输入密码并按Enter

Enter password:

它将显示mysql命令:

mysql>

使用SHOW DATABASES显示当前服务器中的所有数据库:

mysql> show databases;
Code language: SQL (Structured Query Language) (sql)

这是输出:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.05 sec)Code language: JavaScript (javascript)

在本教程中,您逐步学习了如何在 CentOS 7 上安装 MySQL 8。

本教程有帮助吗?