如何安装MySQL 5.7 CentOS/RHEL 7/6, Fedora 27/26/25

时间:2019-05-29 14:48:09  来源:igfitidea点击:

MySQL社区发布了MySQL 5.7版本。
在MySQL官方网站上可以找到。
在本文中,我们使用CentOS 7.2, 64位系统。
对于其他操作系统版本(如:windows),我们可以从
https://dev.mysql.com/downloads/mysql/5.7.html#downloads下载。

第1步-启用MySQL存储库

首先,我们需要在系统上启用MySQL 5.7社区发布yum存储库。
在MySQL官方网站上可以找到yum存储库配置的rpm包。
根据操作系统版本使用下面命令之一。

-- 在 CentOS and RHEL 7 -- 
# yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

-- 在 CentOS and RHEL 6 -- 
# yum localinstall https://dev.mysql.com/get/mysql57-community-release-el6-9.noarch.rpm

-- 在 Fedora 27 -- 
# dnf install https://dev.mysql.com/get/mysql57-community-release-fc27-9.noarch.rpm

-- 在 Fedora 26 -- 
# dnf install https://dev.mysql.com/get/mysql57-community-release-fc26-9.noarch.rpm

-- 在 Fedora 25 -- 
# dnf install https://dev.mysql.com/get/mysql57-community-release-fc25-9.noarch.rpm

步骤2 -安装MySQL 5.7服务器

因为我们已经在系统上成功启用了MySQL yum存储库。
现在,按照操作系统版本使用以下命令安装MySQL 5.7社区服务器。

适用于CentOS和RHEL 7/6

# yum install mysql-community-server

适用于Fedora 27/26/25:

# dnf install mysql-community-server

上面的命令安装将其他依赖包。

在包的安装过程中,创建一个临时密码并记录到MySQL日志文件中。
使用以下命令查找临时MySQL密码。
获取mysql临时root密码:

# grep 'A temporary password' /var/log/mysqld.log |tail -1
[Note] A temporary password is generated for Hyman@theitroad: Nm(!pKkkjo68e

启动MySQL服务

安装rpm后,使用以下命令启动MySQL服务。

# service mysqld start

初始设置MySQL

执行 mysql_secure_installation脚本并按照向导操作。

# /usr/bin/mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: **********

The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: ******************

Re-enter new 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!

步骤-登录到MySQL

我们已经成功安装了MySQL 5.7。
使用root访问登录到MySQL,并尝试创建一个虚拟数据库。

# mysql -h localhost -u root -p 

登录到MySQL服务器后,使用以下命令创建一个测试数据库。

/* CREATE NEW DATABASE */
mysql> CREATE DATABASE mydb;

/* CREATE MYSQL USER FOR DATABASE */
mysql> CREATE USER 'db_user'@'localhost' IDENTIFIED BY 'password';

/* GRANT Permission to User on Database */
mysql> GRANT ALL ON mydb.* TO 'db_user'@'localhost';

/* RELOAD PRIVILEGES */
mysql> FLUSH PRIVILEGES;

第6步-检查MySQL版本

查看MySQL版本。
下面的命令将显示已安装的MySQL版本。

#  mysql -V

mysql  Ver 14.14 Distrib 5.7.17, for Linux (x86_64) using  EditLine wrapper