如何在CentOS 7/6和Fedora 31/30上安装MySQL 8.0

时间:2019-05-19 01:26:21  来源:igfitidea点击:

MySQL 8是最新版本的安装版本。MySQL是Linux系统中最流行的数据库服务器,它还支持大量的平台。本教程将使用包管理器在CentOS/RHEL 7/6、Fedora 31/30/29/28上安装MySQL Server 8.0社区版。

步骤1–设置Yum存储库

首先,需要在MySQL提供的系统中启用MySQL-yum存储库。根据操作系统版本执行以下命令之一。

### 在 CentOS/RHEL 7  ###
rpm -Uvh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm

### 在 CentOS/RHEL 6  ###
rpm -Uvh https://repo.mysql.com/mysql80-community-release-el6-3.noarch.rpm

### 在 Fedora 31  ###
rpm -Uvh https://repo.mysql.com/mysql80-community-release-fc31-1.noarch.rpm

### 在 Fedora 30  ###
rpm -Uvh https://repo.mysql.com/mysql80-community-release-fc30-1.noarch.rpm

### 在 Fedora 29  ###
rpm -Uvh https://repo.mysql.com/mysql80-community-release-fc29-2.noarch.rpm

### 在 Fedora 28  ###
rpm -Uvh https://repo.mysql.com/mysql80-community-release-fc28-2.noarch.rpm

步骤2-安装MySQL Community Server

MySQL-yum存储库包含多个MySQL版本的多个存储库配置。因此,首先禁用mysql repo文件中的所有存储库。

sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/mysql-community.repo

然后根据操作系统执行以下命令之一来安装MySQL。

yum --enablerepo=mysql80-community install mysql-community-server  ## CentOS & RedHat 
dnf --enablerepo=mysql80-community install mysql-community-server  ## Fedora Systems

步骤3–启动MySQL服务

从Linux终端使用以下命令启动MySQL服务器。

使用Sysvini的系统

service mysqld start

使用Systemd的系统

systemctl start mysqld.service

步骤4–查找默认root密码

安装MySQL 8.0后,将为MySQL根用户创建一个临时密码。我们可以在日志文件中找到生成的临时密码。

grep "A temporary password" /var/log/mysqld.log

输出:

[Note] [MY-010454] [Server] A temporary password is generated for Hyman@theitroad: KWa>vo9xFUrx

步骤5–MySQL安装后设置

在第一次安装MySQL之后,执行mysql_secure_installation命令来保护MySQL服务器。它会提示一些问题,我们建议对每个问题都说“是”( y)。

mysql_secure_installation

Enter password for user root:

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

New password:
Re-enter new password:

Change the password for root ? ((Press y|Y for Yes, any other key for No) :   **n**  

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安装已成功完成。现在重新启动服务并在系统启动时设置autostart。

### Using SysVinit
service mysqld restart
chkconfig mysqld on

### Using Systemd
systemctl restart mysqld.service
systemctl enable mysqld.service

第7步-使用MySQL

现在使用下面的命令 连接mysql 数据库服务器。它将提示输入验证密码。成功登录后,我们将得到MySQL命令提示符。

mysql -h localhost -u root -p

登录后,可以使用以下命令创建新数据库、创建用户并为该用户分配数据库权限。

### CREATE DATABASE
mysql> CREATE DATABASE mydb;

### CREATE USER ACCOUNT
mysql> CREATE USER 'dbuser'@'192.168.10.101' IDENTIFIED BY 'secret';

### GRANT PERMISSIONS ON DATABASE
mysql> GRANT ALL ON mydb.* TO 'dbuser'@'192.168.10.101';

###  RELOAD PRIVILEGES
mysql> FLUSH PRIVILEGES;

我们已经成功地在系统上安装了MySQL服务器。