摘自 CentOS 7 下 Yum 安装 MySQL 5.7 | Zhanming’s blog

1. 配置 yum 源

下载并安装 MySQL 源安装包:

> sudo yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

检查 yum 源是否安装成功:

> sudo yum repolist enabled | grep "mysql.*-community.*"

mysql-connectors-community/x86_64 MySQL Connectors Community                 95
mysql-tools-community/x86_64      MySQL Tools Community                      84
mysql57-community/x86_64          MySQL 5.7 Community Server                327

2. yum 安装 MySQL

> sudo yum install mysql-community-server

3. 启动 MySQL

> sudo systemctl enable mysqld
> sudo systemctl start mysqld
> sudo systemctl status mysqld

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-02-16 00:41:07 CST; 2 days ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 29143 (mysqld)
    Tasks: 31
   Memory: 193.2M
   CGroup: /system.slice/mysqld.service
           └─29143 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Feb 16 00:41:06 localhost.localdomain systemd[1]: Starting MySQL Server...
Feb 16 00:41:07 localhost.localdomain systemd[1]: Started MySQL Server.

4. 修改 root 初始密码

MySQL 5.7 启动后,会在/var/log/mysqld.log文件中为用户root生成一个随机的初始密码。使用以下命令查看初始密码:

> grep 'temporary password' /var/log/mysqld.log

2019-02-15T16:29:45.738097Z 1 [Note] A temporary password is generated for root@localhost: jHsg<YlYu5id

登录 MySQL 并修改密码

> mysql -u root -p
Enter password:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

MySQL 5.7 默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不少于 8 位

通过 MySQL 环境变量可以查看密码策略的相关信息

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+
7 rows in set (0.01 sec)

指定密码校验策略

> sudo vi /etc/my.cnf

[mysqld]
# 添加如下键值对, 0=LOW, 1=MEDIUM, 2=STRONG
validate_password_policy=0

禁用密码策略

> sudo vi /etc/my.cnf

[mysqld]
# 禁用密码校验策略
validate_password = off

重启 MySQL 服务,使配置生效:

sudo systemctl restart mysqld

5. 添加远程登录用户

MySQL 默认只允许root用户在本地登录。如果要从其他机器上连接 MySQL,必须允许root用户从远程登录,或者添加一个允许远程连接的账户。以下命令将添加一个新用户admin,并允许其从远程登录

mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY 'secret' WITH GRANT OPTION;

如果需要修改权限允许root远程登录,则先root用户登录 MySQL,再进行如下操作:

mysql> USE mysql;
Database changed

mysql> SELECT User, Host
    -> FROM user;
+-----------+---------------+
| Host      | User          |
+-----------+---------------+
| %         | admin         |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
4 rows in set (0.02 sec)

mysql> UPDATE user
    -> SET Host = '%'
    -> WHERE User = 'root';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT User, Host
    -> FROM user;
+-----------+---------------+
| Host      | User          |
+-----------+---------------+
| %         | admin         |
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
+-----------+---------------+
4 rows in set (0.00 sec)

6. 配置默认编码为 UTF-8

MySQL 默认编码latin1,查看字符集:

mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

在配置文件/etc/my.cnf中将其修改为utf8

> vim /etc/my.cnf

[mysqld]
# 在myslqd下添加如下键值对
character_set_server=utf8
init_connect='SET NAMES utf8'

重启 MySQL,使配置生效:

> sudo systemctl restart mysqld

再次查看字符集:

mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

7. 开启防火墙端口

> sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent
> sudo firewall-cmd --reload

参考文章

  1. CentOS 7 下 Yum 安装 MySQL 5.7 | Zhanming’s blog
  2. A Quick Guide to Using the MySQL Yum Repository | MySQL Documentation
  3. MySQL 5.7 安装与配置(YUM)| CSDN
  4. Centos 7 安装 MySQL | 简书