安装环境

系统:CentOS Linux release 8.1.1911 (Core) | MySQL版本:8.0.17 | 博客记录时间:2020-4-21 15:21:36

安装MySQL

查看MySQL 官网 提供的 MySQL Yum Repository ,这里我们选择 Red Hat Enterprise Linux 8 / Oracle Linux 8 (Architecture Independent), RPM Package

根据自己的 Linux 版本挑选 RPM Package 版本,这里演示的环境是 Linux 8

检查系统版本

1
2
3
4
5
# 查看系统版本
[unshell@localhost /]$ cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
# 选择合适版本的 Yum 源
mysql80-community-release-el8-1.noarch.rpm

开始之前

1
2
3
4
5
6
7
# 直接切换成root账号,避免权限不足造成其他问题
[unshell@localhost /]$ su root
Password:
[root@localhost /]#
# 当然你如果不想切换账号,那在需要权限的命令前加上sudo
[unshell@localhost /]$ sudo [需要权限的命令]
Password:

安装YUM仓库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 下载到当前目录下
wget https://repo.mysql.com//mysql80-community-release-el8-1.noarch.rpm
# 使用 yum 本地安装 【方法一】
yum localinstall mysql80-community-release-el8-1.noarch.rpm
# 或者【方法二】
rpm -ivh mysql80-community-release-el8-1.noarch.rpm
# 查看包源是否安装成功【方法一】
yum repolist enabled | grep "mysql.*-community.*"
# 输出内容如下
mysql-connectors-community MySQL Connectors Community 42
mysql-tools-community MySQL Tools Community 19
mysql80-community MySQL 8.0 Community Server 31
# 或者【方法二】
yum list installed | grep mysql
# 输出内容如下
mysql80-community-release.noarch

配置MySQL服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 安装 MySQL 服务【方法一】
yum install mysql-server
# 或者【方法二】
yum install mysql-community-server
# 启动 MySQL 服务
systemctl start mysqld
# 查看 MySQL 服务状态
systemctl status mysqld
# 输出如下则表示开启
● mysqld.service - MySQL 8.0 database server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2020-04-21 16:22:47 CST; 10s ago
Process: 11914 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
Process: 11787 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
Process: 11763 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
Main PID: 11871 (mysqld)
Status: "Server is operational"
Tasks: 39 (limit: 4888)
Memory: 328.3M
CGroup: /system.slice/mysqld.service
└─11871 /usr/libexec/mysqld --basedir=/usr

卸载Mysql服务

1
2
3
4
5
6
#找到已安装的 rpm 包名
rpm -qa | grep mysql
# 卸载 rpm 包
rpm -e [包名]
# 清理缓存
yum clean all

配置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
# 查看MySQL服务日志与password相关的内容
grep 'password' /var/log/mysql/mysqld.log
# 演示时输出如下
2020-04-21T08:22:41.675426Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
# 这里提示我们Server为root@localhost账号创建了个空的密码,我们这里直接登入MySQL
mysql -u root -p
Enter password:
# 直接回车,输出如下及登入成功
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.17 Source distribution

Copyright (c) 2000, 2019, 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.
# 修改root@localhost的密码(这里演示修改密码为:qwe123)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'qwe123';
Query OK, 0 rows affected (0.08 sec)
# 为外网访问添加账户(%代表所有地址都可能访问,使用时根据实际情况)
CREATE USER 'unshell'@'%' IDENTIFIED BY 'qwe123';
# 授予权限(这里演示提供所有权限,使用时根据实际情况)
GRANT ALL ON *.* TO 'unshell'@'%' WITH GRANT OPTION;

开启防火墙对应端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 开放默认3306端口
firewall-cmd --add-port=3306/tcp --permanent
# 输出success说明成功
success
# 更新防火墙规则
firewall-cmd --reload
# 查看防火墙状况
firewall-cmd --list-all
# 输出内容如下说明成功添加3306端口
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
services: cockpit dhcpv6-client ssh
ports: 3306/tcp
# 如果未成功,可以尝试重启防火墙
firewall-cmd --complete-reload

卸载MySQL

1
2
3
4
5
6
7
8
9
10
11
12
# 关闭MySQL服务
systemctl stop mysqld
# 查找已安装的MySQl相关的包
rpm -qa|grep -i mysql
# 移除输出的包
yum remove [package]
# 查询相关的文件夹
find / -name mysql
# 删除输出的文件夹
rm -rf [directory]
# 有需要的话也可以3306端口
firewall-cmd --permanent --remove-port=3306/tcp

相关文档