# CentOS  安装 Mysql 8.0

#### 博客作者：联系请[点击](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)，搬运不易，希望请作者喝咖啡，可以点击联系[博客作者](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)

{% hint style="info" %}
摘要： CentOS 8 安装MySQL 8.0 并配置远程登录
{% endhint %}

### 安装MySQL8.0 <a href="#id-2020851729" id="id-2020851729"></a>

使用最新的包管理器安装MySQL

```
dnf install @mysql
```

### 开启启动 <a href="#id-1058097243" id="id-1058097243"></a>

安装完成后，运行以下命令来启动MySQL服务并使它在启动时自动启动：

```
systemctl enable --now mysqld
```

要检查MySQL服务器是否正在运行，请输入：

```
systemctl status mysqld
```

### 添加密码及安全设置 <a href="#id-2905009988" id="id-2905009988"></a>

运行mysql\_secure\_installation脚本，该脚本执行一些与安全性相关的操作并设置MySQL根密码：

```
mysql_secure_installation
```

步骤如下：

1. 要求你配置VALIDATE PASSWORD component（验证密码组件）： 输入y ，回车进入该配置
   * 选择密码验证策略等级， 我这里选择0 （low），回车
   * 输入新密码两次
   * 确认是否继续使用提供的密码？输入y ，回车
   * 移除匿名用户？ 输入y ，回车
   * 不允许root远程登陆？ 我这里需要远程登陆，所以输入n ，回车\
     [![](https://img2018.cnblogs.com/blog/981325/201911/981325-20191126145030142-806823861.png)](https://img2018.cnblogs.com/blog/981325/201911/981325-20191126145030142-806823861.png)
2. 移除test数据库？ 输入y ，回车
3. 重新载入权限表？ 输入y ，回车\
   [![](https://img2018.cnblogs.com/blog/981325/201911/981325-20191126145056520-234675009.png)](https://img2018.cnblogs.com/blog/981325/201911/981325-20191126145056520-234675009.png)

### 配置远程登陆 <a href="#id-3907155004" id="id-3907155004"></a>

如果需要设置root账户远程登陆，上一步骤中，`不允许root远程登陆？`这一步需要设为n。\
接下来本机登录MySQL，将root用户的host字段设为'%'，意为接受root所有IP地址的登录请求：\
本机登录MySQL:

```
mysql -uroot -p<上面步骤中设置的密码>
```

回车后即可登录，接下来终端变成了`mysql>`开头:

接着继续执行mysql语句，将将root用户的host字段设为'%'：

```
use mysql;
update user set host='%' where user='root';
flush privileges;
```

#### 查询远程登录权限

```
select host,user from user;
```

&#x20;**修改连接权限执行：**

```
update user set host='%' where user ='root';
```

&#x20;**更改加密方式执行：**

```
update user set plugin='mysql_native_password' where user ='root';
```

&#x20;**授权远程连接：**

```
grant all on *.* to 'root'@'%';
```

设置完成后输入exit退出mysql，回到终端shell界面，接着开启系统防火墙的3306端口：

```
sudo firewall-cmd --add-port=3306/tcp --permanent
sudo firewall-cmd --reload
```

### 关闭MySQL主机查询dns <a href="#id-2596400489" id="id-2596400489"></a>

MySQL会反向解析远程连接地址的dns记录，如果MySQL主机无法连接外网，则dns可能无法解析成功，导致第一次连接MySQL速度很慢，所以在配置中可以关闭该功能。\
[参考文档](https://www.cnblogs.com/liruning/p/7111015.html)\
打开`/etc/my.cnf`文件，添加以下配置：

```
[mysqld]
skip-name-resolve
```

### 重启服务[#](https://www.cnblogs.com/kasnti/p/11929030.html#3532493344) <a href="#id-3532493344" id="id-3532493344"></a>

```
systemctl restart mysqld
```

本机测试安装后，MySQL8.0默认已经是utf8mb4字符集，所以字符集不再修改


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang.gitbook.io/about-the-author/yun-wei-huan-jing-da-jian/centos-8-an-zhuang-mysql-8-jiao-cheng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
