MySQL 是一款开源免费的关系型数据库管理系统。本文牛奇网向您介绍,如何在 RHEL/CentOS 8/7/6/ 和 Fedora 上安装和 MySQL 8.0 。
第 1 步:添加 MySQL 存储库
我们将使用 Yum 官方软件仓库来安装 MySQL,该仓库提供 RPM 包,可用于安装最新版本的 MySQL 服务器。
需要注意的是,本文介绍的是在服务器上全新安装 MySQL 的方法,如果已经使用第三方 RPM 包安装了 MySQL,那么建议您使用 Yum 升级或替换已安装的 MySQL ”。
在升级或替换旧的 MySQL 包之前,切记要先备份 MySQL 数据库。
现在下载 MySQL 的 Yum 安装源,以安装最新版本的 MySQL。
---------------在 RHEL/CentOS 8 上---------------
# wget https://repo.mysql.com/mysql80-community-release-el8-1.noarch.rpm
---------------在 RHEL/CentOS 7 上---------------
# wget https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
---------------在 RHEL/CentOS 6 上---------------
# wget https://dev.mysql.com/get/mysql80-community-release-el6-1.noarch.rpm
---------------在 Fedora 35 上---------------
# wget https://dev.mysql.com/get/mysql80-community-release-fc35-1.noarch.rpm
---------------在 Fedora 34 上---------------
# wget https://dev.mysql.com/get/mysql80-community-release-fc34-1.noarch.rpm
---------------在 Fedora 33 上---------------
# wget https://dev.mysql.com/get/mysql80-community-release-fc33-1.noarch.rpm
下载相应的软件包后,使用以下命令安装 MySQL 软件包。
---------------在 RHEL/CentOS 8 上---------------
# yum localinstall mysql80-community-release-el8-1.noarch.rpm
---------------在 RHEL/CentOS 7 上---------------
# yum localinstall mysql80-community-release-el7-1.noarch.rpm
---------------在 RHEL/CentOS 6 上---------------
# yum localinstall mysql80-community-release-el6-1.noarch.rpm
---------------在 Fedora 35 上---------------
# dnf localinstall mysql80-community-release-fc35-1.noarch.rpm
---------------在 Fedora 34 上---------------
# dnf localinstall mysql80-community-release-fc34-1.noarch.rpm
---------------在 Fedora 33 上---------------
# yum localinstall mysql80-community-release-fc33-1.noarch.rpm
通过上述安装命令,会将包含 MySQL 的 Yum 软件源添加到系统的软件仓库列表中,并下载 GnuPG 密钥以验证包的完整性。
可以使用以下命令验证 MySQL 的 Yum 软件源是否已成功添加。
yum repolist enabled | grep "mysql.-community."
dnf repolist enabled | grep "mysql.-community." [On Fedora versions]
第 2 步:安装最新的 MySQL 版本
使用以下命令安装最新版本的 MySQL 。
yum install mysql-community-server
dnf install mysql-community-server [On Fedora versions]
执行上面的命令后,将为 MySQL 服务器安装 mysql-community-server、mysql-community-client、mysql-community-common 和 mysql-community-libs 等所有需要的包。
第 3 步:安装特定的 MySQL 版本
可以使用 MySQL Community Server 安装不同的 MySQL 版本。最新的 MySQL 版本安装源默认激活,而旧版本的安装源默认禁用。
要启用或禁用特定版本的安装源,可以使用 yum -config-manager
或 dnf config-manager 命令,然后加上 --enable
或者 --disable
选项:
# yum-config-manager --disable mysql57-community
# yum-config-manager --enable mysql56-community
------------------ Fedora 版本------------------
# dnf config-manager --disable mysql57-community
# dnf config-manager --enable mysql56-community
第 4 步:启动 MySQL 服务器
安装 MySQL 后,可以使用以下命令启动 MySQL 服务器:
service mysqld start
systemctl enable mysqld.service
启动 MySQL 后,使用以下命令查看 MySQL 服务器的状态:
systemctl status mysqld.service
或者
service mysqld status
下面的是在 CentOS 7 服务器上运行 MySQL 的输出:
Redirecting to /bin/systemctl status mysqld.service
mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
Active: active (running) since Thu 2015-10-29 05:15:19 EDT; 4min 5s ago
Process: 5314 ExecStart=/usr/sbin/mysqld --daemonize $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 5298 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 5317 (mysqld)
CGroup: /system.slice/mysqld.service
└─5317 /usr/sbin/mysqld --daemonize
Oct 29 05:15:19 localhost.localdomain systemd[1]: Started MySQL Server.
使用以下命令查看安装的 MySQL 版本:
mysql --version
mysql Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)
第 5 步:MySQL 安全配置向导
执行 mysql_secure_installation 命令,并根据安全向导的指引进行安全设置,如设置 root 密码、删除匿名用户、删除 root 登录等手段,来提升 MySQL 的安全性。
注意:MySQL 8.0 或更高版本,在安装完成后,会在 /var/log/mysqld.log
生成一个临时的随机密码。
在运行 MySQL 安全配置命令之前,使用以下命令查看密码:
# grep 'temporary password' /var/log/mysqld.log
知道密码后,运行以下命令来对 MySQL 进行安全配置。
# mysql_secure_installation
注意:输入新的 Root 密码是指,输入从文件(/var/log/mysqld.log
)中获取的临时密码。
现在按照屏幕上的配置向导进行操作,可参考下面的输出样本:
输出样本
Securing the MySQL server deployment.
Enter password for user root: Enter New Root 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: 2
Using existing password for root.
Estimated strength of the password: 50
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y
New password: Set New MySQL Password
Re-enter new password: Re-enter New MySQL Password
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
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
Dropping test database…
Success.
Removing privileges on test database…
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
第 6 步:连接 MySQL 服务器
通过输入用户名和密码,连接 MySQL 服务器:
# mysql -u root -p
样本输出:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
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>
第 7 步:使用 Yum 更新 MySQL
这样,我们就已经成功安装了 MySQL 8.0 。还可以通过以下命令,对 MySQL 及组件进行升级。
yum update mysql-server
dnf update mysql-server [On Fedora versions]
当 MySQL 有新的更新可用时,它会自动安装更新;如果没有,将收到一条消息 “NO packagesmarked for updates”。
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/mysql/install-mysql-on-centos/