安装MySQL源

1
2
3
4
5
6

# 下载
bash > wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
# 安装 MySQL 源
bash > yum localinstall mysql57-community-release-el7-11.noarch.rpm

检查源是否安装成功

1
2
3

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

页面输出信息表示源安装成功

1
2
3
4
5

mysql-connectors-community/x86_64 MySQL Connectors Community 165
mysql-tools-community/x86_64 MySQL Tools Community 115
mysql57-community/x86_64 MySQL 5.7 Community Server 444

使用yum install 命令安装MySQL

1
2
3

bash > yum install -y mysql-community-server

发现安装太慢,去国内镜像站下载对应版本的client和server rpm包

  1. 推荐:http://uni.mirrors.163.com/mysql/Downloads/MySQL-5.7/

  2. cd到目录 /var/cache/yum/x86_64/7/mysql57-community/packages/

  3. 把下载好的rpm文件上传到此目录
    mysql-community-client-5.7.34-1.el7.x86_64.rpm
    mysql-community-server-5.7.34-1.el7.x86_64.rpm

  4. 再次执行安装命令

1
2
3

bash > yum install -y mysql-community-server

启动MySQL服务

在CentOs7 下 启动/关闭/重启服务的命令是 systemctl start|stop|restart

1
2
3

bash > systemctl start mysqld

用systemctl status 命令查看服务运行状态

1
2
3

bash > systemctl status mysqld

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2020-07-28 21:09:44 CST; 57s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 25677 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 25628 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 25681 (mysqld)
CGroup: /system.slice/mysqld.service
└─25681 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

7月 28 21:09:38 iZm5ebq5g3dxxmygidjc5cZ systemd[1]: Starting MySQL Server...
7月 28 21:09:44 iZm5ebq5g3dxxmygidjc5cZ systemd[1]: Started MySQL Server.

这里可看到MySQL 运行的状态 active (running)

设置开机启动MySQL 服务

1
2
3
4
5

bash > systemctl enable mysqld
# 重载所有修改过的配置文件
bash > systemctl daemon-reload

重启ContOs 服务器测试.(没有问题)

查看MySQL默认密码

MySQL 安装完成之后,生成的默认密码在 /var/log/mysqld.log 文件中。使用 grep 命令找到日志中的密码。

1
2
3

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

avatar

首次通过初始密码登录后,使用以下命令修改密码

1
2
3
4
5
6

bash > mysql -u root -p
#输入密码
#执行修改密码语句.
bash > ALTER USER 'root'@'localhost' IDENTIFIED BY 'Vine@yyy1';

添加一个用于远程连接的账号,如Navicat

1
2
3
bash > GRANT ALL PRIVILEGES ON *.* TO 'xianglin'@'%' IDENTIFIED BY 'Xiang@lin!1996' WITH GRANT OPTION;
#Query OK, 0 rows affected, 1 warning (0.00 sec)

添加一个指定访问数据库的账号,如Navicat

1
2
3
4

bash > GRANT ALL PRIVILEGES ON test.* TO 'xianglin'@'%' IDENTIFIED BY 'Xiang@lin!1996' WITH GRANT OPTION;
#Query OK, 0 rows affected, 1 warning (0.00 sec)

设置默认编码为 utf8mb4

现在查看编码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 配置文件,在相关节点(没有则自行添加)下添加编码配置,如下:

1
2
3
4
5
6
7
[mysqld]
character-set-server=utf8mb4
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4

或者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[mysqld]
port=3306
default-storage-engine=INNODB
character_set_server=utf8mb4
log-bin=mysql-bin
server-id=1
max_connections=5000
innodb_log_file_size=256M
innodb_buffer_pool_size=8g
join_buffer_size = 64M
sort_buffer_size = 2M
log_timestamps=system
log_slave_updates=on
max_connections=5000
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=256M
binlog_format=row
group_concat_max_len = 102400
explicit_defaults_for_timestamp=false
sql_mode = TRADITIONAL
expire_logs_days=3
long_query_time =1
slow_query_log = 1

[client]
default-character-set=utf8mb4

保存之后使用systemctl restart mysqld 查看编码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

修改密码验证规则

1
2
3
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;

设置简单密码

1
2
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

默认配置文件路径

  1. 配置文件:/etc/my.cnf

  2. 日志文件:/var/log/mysqld.log

  3. 服务启动脚本:/usr/lib/systemd/system/mysqld.service

  4. socket文件:/var/run/mysqld/mysqld.pid

作者简介

青山淼淼