CentOS 7.6 安装 MySQL 8

CentOS7 安装 MySQL

从官网下载 rpm 包

​ 从 https://dev.mysql.com/downloads/repo/yum/ 下载对应系统版本的 MySQL 安装包,我的系统式 CentOS 7.6,安装 Oracle Linux 7 这条

​ 到下载页面找到名为No thanks,just start my download的链接,复制这个链接的地址,我这里下载到了/tmp/mysql文件夹下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@VM-8-9-centos mysql]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
--2020-12-16 13:09:42-- https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm [following]
--2020-12-16 13:09:46-- https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.205.96.240
Connecting to repo.mysql.com (repo.mysql.com)|23.205.96.240|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 26024 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql80-community-release-el7-3.noarch.rpm’

100%[======================================================================================================================================>] 26,024 152KB/s in 0.2s

2020-12-16 13:09:47 (152 KB/s) - ‘mysql80-community-release-el7-3.noarch.rpm’ saved [26024/26024]

安装

​ 使用yum install命令安装

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@VM-8-9-centos mysql]# yum -y install mysql80-community-release-el7-3.noarch.rpm 
Loaded plugins: fastestmirror, langpacks
Examining mysql80-community-release-el7-3.noarch.rpm: mysql80-community-release-el7-3.noarch
Marking mysql80-community-release-el7-3.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql80-community-release.noarch 0:el7-3 will be installed
--> Finished Dependency Resolution
epel/7/x86_64 | 4.7 kB 00:00:00
epel/7/x86_64/updateinfo | 1.0 MB 00:00:00
epel/7/x86_64/primary_db | 6.9 MB 00:00:01
extras/7/x86_64 | 2.9 kB 00:00:00
os/7/x86_64 | 3.6 kB 00:00:00
updates/7/x86_64 | 2.9 kB 00:00:00

Dependencies Resolved

================================================================================================================================================================================
Package Arch Version Repository Size
================================================================================================================================================================================
Installing:
mysql80-community-release noarch el7-3 /mysql80-community-release-el7-3.noarch 31 k

Transaction Summary
================================================================================================================================================================================
Install 1 Package

Total size: 31 k
Installed size: 31 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql80-community-release-el7-3.noarch 1/1
Verifying : mysql80-community-release-el7-3.noarch 1/1

Installed:
mysql80-community-release.noarch 0:el7-3

Complete!

​ 安装 MySQL 服务器

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
[root@VM-8-9-centos mysql]# yum -y install mysql-community-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
mysql-connectors-community | 2.6 kB 00:00:00
mysql-tools-community | 2.6 kB 00:00:00
mysql80-community | 2.6 kB 00:00:00
(1/3): mysql-tools-community/x86_64/primary_db | 83 kB 00:00:00
(2/3): mysql-connectors-community/x86_64/primary_db | 68 kB 00:00:00
(3/3): mysql80-community/x86_64/primary_db | 128 kB 00:00:01
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:8.0.22-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 8.0.22-1.el7 for package: mysql-community-server-8.0.22-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 8.0.11 for package: mysql-community-server-8.0.22-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:8.0.22-1.el7 will be installed
--> Processing Dependency: mysql-community-client-plugins = 8.0.22-1.el7 for package: mysql-community-client-8.0.22-1.el7.x86_64
--> Processing Dependency: mysql-community-libs(x86-64) >= 8.0.11 for package: mysql-community-client-8.0.22-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:8.0.22-1.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.65-1.el7 will be obsoleted
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64
---> Package mysql-community-client-plugins.x86_64 0:8.0.22-1.el7 will be installed
---> Package mysql-community-libs.x86_64 0:8.0.22-1.el7 will be obsoleting
--> Running transaction check
---> Package mysql-community-libs-compat.x86_64 0:8.0.22-1.el7 will be obsoleting
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================================================
Package Arch Version Repository Size
================================================================================================================================================================================
Installing:
mysql-community-libs x86_64 8.0.22-1.el7 mysql80-community 4.6 M
replacing mariadb-libs.x86_64 1:5.5.65-1.el7
mysql-community-libs-compat x86_64 8.0.22-1.el7 mysql80-community 1.2 M
replacing mariadb-libs.x86_64 1:5.5.65-1.el7
mysql-community-server x86_64 8.0.22-1.el7 mysql80-community 510 M
Installing for dependencies:
mysql-community-client x86_64 8.0.22-1.el7 mysql80-community 48 M
mysql-community-client-plugins x86_64 8.0.22-1.el7 mysql80-community 235 k
mysql-community-common x86_64 8.0.22-1.el7 mysql80-community 616 k

Transaction Summary
================================================================================================================================================================================
Install 3 Packages (+3 Dependent packages)

Total download size: 564 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql80-community/packages/mysql-community-client-plugins-8.0.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY:-- ETA
Public key for mysql-community-client-plugins-8.0.22-1.el7.x86_64.rpm is not installed
(1/6): mysql-community-client-plugins-8.0.22-1.el7.x86_64.rpm | 235 kB 00:00:00
(2/6): mysql-community-common-8.0.22-1.el7.x86_64.rpm | 616 kB 00:00:01
(3/6): mysql-community-libs-8.0.22-1.el7.x86_64.rpm | 4.6 MB 00:00:07
(4/6): mysql-community-libs-compat-8.0.22-1.el7.x86_64.rpm | 1.2 MB 00:00:03
(5/6): mysql-community-client-8.0.22-1.el7.x86_64.rpm | 48 MB 00:03:28
(6/6): mysql-community-server-8.0.22-1.el7.x86_64.rpm 54% [==================================- ] 98 kB/s | 309 MB 00:44:09 ETA 54% [==================================- ] 10(6/6): mysql-community-server-8.0.22-1.el7.x86_64.rpm 54% [==================================- ] 10(6/6): mysql-community-server-8.0.22-1.el7.x86_64.rpm | 510 MB 00:44:40
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 214 kB/s | 564 MB 00:44:53
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
Userid : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
Package : mysql80-community-release-el7-3.noarch (@/mysql80-community-release-el7-3.noarch)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql-community-common-8.0.22-1.el7.x86_64 1/7
Installing : mysql-community-client-plugins-8.0.22-1.el7.x86_64 2/7
Installing : mysql-community-libs-8.0.22-1.el7.x86_64 3/7
Installing : mysql-community-client-8.0.22-1.el7.x86_64 4/7
Installing : mysql-community-server-8.0.22-1.el7.x86_64 5/7
Installing : mysql-community-libs-compat-8.0.22-1.el7.x86_64 6/7
Erasing : 1:mariadb-libs-5.5.65-1.el7.x86_64 7/7
Verifying : mysql-community-libs-8.0.22-1.el7.x86_64 1/7
Verifying : mysql-community-server-8.0.22-1.el7.x86_64 2/7
Verifying : mysql-community-client-plugins-8.0.22-1.el7.x86_64 3/7
Verifying : mysql-community-common-8.0.22-1.el7.x86_64 4/7
Verifying : mysql-community-client-8.0.22-1.el7.x86_64 5/7
Verifying : mysql-community-libs-compat-8.0.22-1.el7.x86_64 6/7
Verifying : 1:mariadb-libs-5.5.65-1.el7.x86_64 7/7

Installed:
mysql-community-libs.x86_64 0:8.0.22-1.el7 mysql-community-libs-compat.x86_64 0:8.0.22-1.el7 mysql-community-server.x86_64 0:8.0.22-1.el7

Dependency Installed:
mysql-community-client.x86_64 0:8.0.22-1.el7 mysql-community-client-plugins.x86_64 0:8.0.22-1.el7 mysql-community-common.x86_64 0:8.0.22-1.el7

Replaced:
mariadb-libs.x86_64 1:5.5.65-1.el7

Complete!

​ 启动并查看 MySQL 状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@VM-8-9-centos mysql]# systemctl start mysqld.service
[root@VM-8-9-centos mysql]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2020-12-16 14:59:43 CST; 14s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 17368 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 17452 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─17452 /usr/sbin/mysqld

Dec 16 14:59:33 VM-8-9-centos systemd[1]: Starting MySQL Server...
Dec 16 14:59:43 VM-8-9-centos systemd[1]: Started MySQL Server.

配置 MySQL

​ 查看初始密码

1
2
[root@VM-8-9-centos mysql]# grep "password" /var/log/mysqld.log 
2020-12-16T06:59:37.568001Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 6#bsu:lel2Tk

​ 登录 MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@VM-8-9-centos mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.22

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

​ 修改密码

1
2
alter user 'root'@'localhost' identified by '新的密码';
# MySQL 在这里要求修改的密码有一定的安全性,所以需要设置一个难度较大的密码

​ 修改密码规则

1
2
3
4
5
6
7
mysql> set global validate_password_policy=0;
ERROR 1193 (HY000): Unknown system variable 'validate_password_policy'
mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password.length=1;
Query OK, 0 rows affected (0.00 sec)

​ 随后可以重新设置密码

开启远程登录

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
[root@VM-8-9-centos mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> update user set host="%" where Host='localhost' and user = "root";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

​ 如果是云服务器,可能需要在对应的控制台开启3306端口的防火墙权限

-------------本文结束感谢您的阅读-------------