# MYSQL

#### 设置yum源

在`/etc/yum.repos/`目录创建文件mysql.repo，内容如下：

```
[mysql]
name=mysql
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=0
```

#### 安装mysql

执行下面的命令安装，然后进行启动

```
$ yum -y install mysql-community-server

$ systemctl daemon-reload

$ systemctl start mysqld
```

启动成功后，默认允许localhost:3306的访问

#### 修改配置

首先，修改密码。mysql在安装后会生成一个临时root密码，保存在日志文件/var/log/mysqld.log中，通过以下命令可以看到

```
$ grep 'temporary password' /var/log/mysqld.log
```

然后执行命令`mysql -uroot -p`连接到mysql，执行以下命令修改root密码

```
mysql> set password for 'root'@'localhost'=password('Mypass123456!');
```

退出mysql。

接着，我们继续配置，允许可以远程root登录。

从本地连接到mysql，然后执行以下命令

```
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Mypass123456!' WITH GRANT OPTION;
```

其中"*.*"代表所有资源所有权限， “'root'@%”其中root代表账户名，%代表所有的访问地址，也可以使用一个唯一的地址进行替换，只有一个地址能够访问。如果是某个网段的可以使用地址与%结合的方式，如10.0.42.%。IDENTIFIED BY 'Mypass1234561'，这个是指访问密码。WITH GRANT OPTION允许级联授权。

然后删新访问列表（个人实验中并没有执行这一行）

```
mysql> FLUSH PRIVILEGES;
```

然后，查看访问列表，发现HOST有了%这一行

```
mysql> select HOST, USER from user;
+-----------+---------------+
| HOST      | USER          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
4 rows in set (0.00 sec)
```

####

#### 参考

{% embed url="<https://blog.csdn.net/kabolee/article/details/82528913>" %}

{% embed url="<https://blog.csdn.net/zhazhagu/article/details/81064406>" %}


---

# 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://pshizhsysu.gitbook.io/linux/chang-yong-ruan-jian-an-zhuang-pian/mysql.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.
