MySQL - CentOS

系统配置

  • CentOS Linux release 7.9.2009 (Core)
  • MySQL 8.0.24-1

MySQL 资源地址

安装 MySQL Yum Repository

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
root@ox [01:24:00 AM] [~/Downloads] 
-> # wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
--2021-04-28 01:24:05-- https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.201.152.227
Connecting to repo.mysql.com (repo.mysql.com)|23.201.152.227|: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 39.1KB/s in 0.6s

2021-04-28 01:24:10 (39.1 KB/s) - ‘mysql80-community-release-el7-3.noarch.rpm’ saved [26024/26024]

root@ox [01:24:12 AM] [~/Downloads]
-> # ll
total 21M
-rw-r--r--. 1 root root 26K Apr 25 2019 mysql80-community-release-el7-3.noarch.rpm
-rwxrw-rw-. 1 root root 21M Apr 25 11:32 node-v14.16.1-linux-x64.tar.xz
root@ox [01:24:13 AM] [~/Downloads]
-> # yum localinstall 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

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
Is this ok [y/d/N]: y
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!
root@ox [01:59:04 AM] [~/Downloads]
-> # yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 194
mysql-tools-community/x86_64 MySQL Tools Community 126
mysql80-community/x86_64 MySQL 8.0 Community Server 247
root@ox [01:59:22 AM] [~/Downloads]
-> # cd /etc/yum.repos.d
root@ox [01:59:45 AM] [/etc/yum.repos.d]
-> # ll
total 56K
-rw-r--r--. 1 root root 1.9K Jul 4 2019 CentOS-Base.repo
-rw-r--r--. 1 root root 1.7K Oct 23 2020 CentOS-Base.repo.backup
-rw-r--r--. 1 root root 1.3K Oct 23 2020 CentOS-CR.repo.backup
-rw-r--r--. 1 root root 649 Oct 23 2020 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 Oct 23 2020 CentOS-fasttrack.repo.backup
-rw-r--r--. 1 root root 630 Oct 23 2020 CentOS-Media.repo
-rw-r--r--. 1 root root 1.3K Oct 23 2020 CentOS-Sources.repo
-rw-r--r--. 1 root root 8.4K Oct 23 2020 CentOS-Vault.repo
-rw-r--r--. 1 root root 616 Oct 23 2020 CentOS-x86_64-kernel.repo
-rw-r--r--. 1 root root 2.2K Apr 28 01:13 docker-ce.repo
-rw-r--r--. 1 root root 2.1K Apr 25 2019 mysql-community.repo
-rw-r--r--. 1 root root 2.1K Apr 25 2019 mysql-community-source.repo

安装 MySQL Community Server

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
root@ox [02:13:09 AM] [/etc/yum.repos.d] 
-> # yum install mysql-community-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:8.0.24-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 8.0.24-1.el7 for package: mysql-community-server-8.0.24-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 8.0.11 for package: mysql-community-server-8.0.24-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:8.0.24-1.el7 will be installed
--> Processing Dependency: mysql-community-client-plugins = 8.0.24-1.el7 for package: mysql-community-client-8.0.24-1.el7.x86_64
--> Processing Dependency: mysql-community-libs(x86-64) >= 8.0.11 for package: mysql-community-client-8.0.24-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:8.0.24-1.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.68-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.24-1.el7 will be installed
---> Package mysql-community-libs.x86_64 0:8.0.24-1.el7 will be obsoleting
--> Running transaction check
---> Package mysql-community-libs-compat.x86_64 0:8.0.24-1.el7 will be obsoleting
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================
Installing:
mysql-community-libs x86_64 8.0.24-1.el7 mysql80-community 4.0 M
replacing mariadb-libs.x86_64 1:5.5.68-1.el7
mysql-community-libs-compat x86_64 8.0.24-1.el7 mysql80-community 1.2 M
replacing mariadb-libs.x86_64 1:5.5.68-1.el7
mysql-community-server x86_64 8.0.24-1.el7 mysql80-community 428 M
Installing for dependencies:
mysql-community-client x86_64 8.0.24-1.el7 mysql80-community 45 M
mysql-community-client-plugins x86_64 8.0.24-1.el7 mysql80-community 189 k
mysql-community-common x86_64 8.0.24-1.el7 mysql80-community 614 k

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

Total download size: 479 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql80-community/packages/mysql-community-client-plugins-8.0.24-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-client-plugins-8.0.24-1.el7.x86_64.rpm is not installed
(1/6): mysql-community-client-plugins-8.0.24-1.el7.x86_64.rpm | 189 kB 00:00:04
(2/6): mysql-community-common-8.0.24-1.el7.x86_64.rpm | 614 kB 00:00:03
(3/6): mysql-community-libs-8.0.24-1.el7.x86_64.rpm | 4.0 MB 00:00:14
(4/6): mysql-community-libs-compat-8.0.24-1.el7.x86_64.rpm | 1.2 MB 00:00:04
(5/6): mysql-community-client-8.0.24-1.el7.x86_64.rpm | 45 MB 00:00:55
(6/6): mysql-community-server-8.0.24-1.el7.x86_64.rpm | 428 MB 00:06:34
--------------------------------------------------------------------------------------------------------------------------------------------------
Total 1.1 MB/s | 479 MB 00:07:00
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
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql-community-client-plugins-8.0.24-1.el7.x86_64 1/7
Installing : mysql-community-common-8.0.24-1.el7.x86_64 2/7
Installing : mysql-community-libs-8.0.24-1.el7.x86_64 3/7
Installing : mysql-community-client-8.0.24-1.el7.x86_64 4/7
Installing : mysql-community-server-8.0.24-1.el7.x86_64 5/7
Installing : mysql-community-libs-compat-8.0.24-1.el7.x86_64 6/7
Erasing : 1:mariadb-libs-5.5.68-1.el7.x86_64 7/7
Verifying : mysql-community-server-8.0.24-1.el7.x86_64 1/7
Verifying : mysql-community-libs-8.0.24-1.el7.x86_64 2/7
Verifying : mysql-community-common-8.0.24-1.el7.x86_64 3/7
Verifying : mysql-community-libs-compat-8.0.24-1.el7.x86_64 4/7
Verifying : mysql-community-client-plugins-8.0.24-1.el7.x86_64 5/7
Verifying : mysql-community-client-8.0.24-1.el7.x86_64 6/7
Verifying : 1:mariadb-libs-5.5.68-1.el7.x86_64 7/7

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

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

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

Complete!

启动 MySQL 服务

1
2
root@ox [02:23:14 AM] [/etc/yum.repos.d] 
-> # systemctl start mysqld

查看 MySQL 服务状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@ox [02:28:44 AM] [/etc/yum.repos.d] 
-> # systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2021-04-28 02:28:44 CST; 5s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 4581 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 4659 (mysqld)
Status: "Server is operational"
Tasks: 38
Memory: 441.5M
CGroup: /system.slice/mysqld.service
└─4659 /usr/sbin/mysqld

Apr 28 02:28:38 ox.gg.com systemd[1]: Starting MySQL Server...
Apr 28 02:28:44 ox.gg.com systemd[1]: Started MySQL Server.

查看 root 用户初始密码

1
2
3
root@ox [02:28:52 AM] [/etc/yum.repos.d] 
-> # grep 'temporary password' /var/log/mysqld.log
2021-04-27T18:28:41.300527Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: >fgv>St_o5Wq

初次登陆修改 root 用户密码

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
root@ox [02:30:04 AM] [/etc/yum.repos.d] 
-> # mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.24

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456.MySQL';
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT user, authentication_string, plugin, host FROM mysql.user;
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
| mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
| mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
| mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
| root | $A$005$D Vc[JF\e!|es1FtZKb1dm.zqNB8T5TWFhoQTzuoQSI6kW5AVXgFUNeC2rG0 | caching_sha2_password | localhost |
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

创建远程登陆用户

1
2
3
4
5
6
7
8
mysql> CREATE USER 'gg'@'%' IDENTIFIED BY '123456.OX.gg';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'gg'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

忘记 root 密码

修改配置,跳过校验密码

1
[14:56:00] root :: sun  ➜  ~ » vim /etc/my.cnf

添加下面内容,跳过密码校验:

1
2
# MySQL
skip-grant-tables

登录 root 用户,清空 root 密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[14:59:37] root :: sun  ➜  ~ » mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.24 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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 authentication_string = '' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

恢复配置文件

1
[14:56:00] root :: sun  ➜  ~ » vim /etc/my.cnf

注释掉添加的内容:

1
2
# MySQL
skip-grant-tables

导出数据

导出数据库

1
2
3
4
5
[root@ClockworkMonkey MySQL]# mysqldump -u root -p anthology > database_anthology_dump_20220427.txt
Enter password:
[root@ClockworkMonkey MySQL]# ll
total 274356
-rw-r--r-- 1 root root 280940387 Apr 27 10:22 database_anthology_dump_20220427.txt

导入数据库

1
2
3
4
5
[root@ggimm MySQL]# ll
total 274356
-rw-r--r-- 1 root root 280940387 Apr 27 10:34 database_anthology_dump_20220427.txt
[root@ggimm MySQL]# mysql -u root -p anthology < database_anthology_dump_20220427.txt
Enter password: