# CentOS7下rpm安装MySQL8
# CentOS环境
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@localhost ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
1
2
3
4
2
3
4
# 准备安装包
可以从MySQL官网 (opens new window)下载最新MySQL8。
# 手动下载后上传
mysql-community-common-8.0.25-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
mysql-community-libs-8.0.25-1.el7.x86_64.rpm
mysql-community-client-8.0.25-1.el7.x86_64.rpm
mysql-community-server-8.0.25-1.el7.x86_64.rpm
1
2
3
4
5
2
3
4
5
将安装包上传至/opt/software
中
[root@localhost software]# ll
总用量 489744
-rw-r--r--. 1 root root 47810444 1月 12 10:21 mysql-community-client-8.0.25-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 193616 1月 12 10:21 mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 628904 1月 12 10:21 mysql-community-common-8.0.25-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 4240320 1月 12 10:21 mysql-community-libs-8.0.25-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 448614076 1月 12 10:25 mysql-community-server-8.0.25-1.el7.x86_64.rpm
1
2
3
4
5
6
7
2
3
4
5
6
7
# 使用wget
下载
需要联网且安装wget
# 安装wget
yum install -y wget
1
2
2
下载
[root@localhost software]# wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.25-1.el7.x86_64.rpm-bundle.tar
[root@localhost software]# ll
总用量 784772
-rw-r--r--. 1 root root 803604480 6月 17 14:53 mysql-8.0.25-1.el7.x86_64.rpm-bundle.tar
drwxr-xr-x. 2 root root 282 6月 17 14:53 old
# 解压
[root@localhost software]# tar -xvf mysql-8.0.28-1.el8.x86_64.rpm-bundle.tar
-rw-r--r--. 1 root root 47810444 1月 12 10:21 mysql-community-client-8.0.25-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 193616 1月 12 10:21 mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 628904 1月 12 10:21 mysql-community-common-8.0.25-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 4240320 1月 12 10:21 mysql-community-libs-8.0.25-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 448614076 1月 12 10:25 mysql-community-server-8.0.25-1.el7.x86_64.rpm
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 安装前检查
# 检查是否存在mariadb
CentOS中自带 mariadb
,为了避免与MySQL冲突,故先卸载 mariadb
:
# 检查是否安装mariadb
[root@localhost ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
# 存在mariadb则卸载
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
1
2
3
4
5
2
3
4
5
# 检查是否存在MySQL
[root@localhost ~]# rpm -qa | grep mysql
[root@localhost ~]#
1
2
2
# 检查是否存在mysql用户
# 检查组文件
[root@localhost ~]# cat /etc/group | grep mysql
# 检查密码文件
[root@localhost ~]# cat /etc/passwd | grep mysql
# 没有则创建
# 创建mysql组
[root@localhost ~]# groupadd mysql
# 创建mysql账号,并指定mysql组
[root@localhost ~]# useradd -r -g mysql mysql
# 修改mysql密码
[root@localhost software]# passwd mysql
更改用户 mysql 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新
# 检查组和密码文件
[root@localhost ~]# cat /etc/group | grep mysql
mysql:x:1000:
[root@localhost ~]# cat /etc/passwd | grep mysql
mysql:x:997:1000::/home/mysql:/bin/bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 检查是否开启MySQL端口
根据需求修改端口号,这里使用默认端口3306。
# 查看想开的端口是否已开,若此提示FirewallD is not running, 表示为不可知的防火墙 需要查看状态并开启防火墙, 如果是云服务器还需要去控制台配置安全组访问
[root@localhost ~]# firewall-cmd --query-port=3306/tcp
no
# 开启端口, success代表成功
[root@localhost ~]# firewall-cmd --add-port=3306/tcp --permanent
success
# 刷新配置, 使配置生效
[root@localhost ~]# firewall-cmd --reload
success
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 安装及配置
注意:需按照如下步骤顺序安装MySQL包。
第1步:安装mysql-community-common-8.0.25-1.el7.x86_64.rpm
[root@localhost software]# rpm -ivh mysql-community-common-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-common-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-common-8.0.25-1.e################################# [100%]
1
2
3
4
5
2
3
4
5
第2步:mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
[root@localhost software]# rpm -ivh mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-plugins-8.################################# [100%]
1
2
3
4
5
2
3
4
5
第3步:安装mysql-community-libs-8.0.25-1.el7.x86_64.rpm
[root@localhost software]# rpm -ivh mysql-community-libs-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-libs-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-8.0.25-1.el7################################# [100%]
1
2
3
4
5
2
3
4
5
第4步:安装mysql-community-client-8.0.25-1.el7.x86_64.rpm
[root@localhost software]# rpm -ivh mysql-community-client-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-client-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-8.0.25-1.e################################# [100%]
1
2
3
4
5
2
3
4
5
第5步:安装mysql-community-server-8.0.25-1.el7.x86_64.rpm
[root@localhost software]# rpm -ivh mysql-community-server-8.0.25-1.el7.x86_64.rpm
警告:mysql-community-server-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-server-8.0.25-1.e################################# [100%]
1
2
3
4
5
2
3
4
5
第5步报错:
警告:mysql-community-server-8.0.25-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
错误:依赖检测失败:
/usr/bin/perl 被 mysql-community-server-8.0.25-1.el7.x86_64 需要
mysql-community-common(x86-64) = 8.0.25-1.el7 被 mysql-community-server-8.0.25-1.el7.x86_64 需要
net-tools 被 mysql-community-server-8.0.25-1.el7.x86_64 需要
perl(Getopt::Long) 被 mysql-community-server-8.0.25-1.el7.x86_64 需要
perl(strict) 被 mysql-community-server-8.0.25-1.el7.x86_64 需要
1
2
3
4
5
6
7
2
3
4
5
6
7
需要安装net-tools
依赖:
[root@localhost software]# yum install -y net-tools
[root@localhost software]# yum install -y perl-Module-Install.noarch
1
2
2
查看MySQL安装情况:
[root@localhost software]# rpm -qa|grep mysql
mysql-community-client-plugins-8.0.25-1.el7.x86_64
mysql-community-common-8.0.25-1.el7.x86_64
mysql-community-libs-8.0.25-1.el7.x86_64
mysql-community-server-8.0.25-1.el7.x86_64
mysql-community-client-8.0.25-1.el7.x86_64
1
2
3
4
5
6
2
3
4
5
6
# MySQL默认目录
Centos 通过yum安装(RPM分发进行安装)MySQL的几个默认目录如下:
目录 | 说明 |
---|---|
/usr/bin | 客户端程序和脚本 |
/usr/sbin | mysqld服务器 |
/var/lib/mysql | 数据库目录 |
/var/lib/mysql-files | 数据文件目录 |
/var/lib/mysql-keyring | 加密文件目录 |
/var/log/mysqld.log | 日志文件 |
/usr/share/mysql-8.0/ | 错误消息和字符集文件 |
/etc/my.cnf | 配置文件 |
# 配置MySQL用户和组
chown -R mysql:mysql /var/lib/mysql
1
# 启动MySQL
[root@localhost ~]# systemctl start mysqld
1
# 查看MySQL状态
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2022-06-17 16:58:58 CST; 11s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 11847 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 11918 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─11918 /usr/sbin/mysqld
6月 17 16:58:53 localhost.localdomain systemd[1]: Starting MySQL Server...
6月 17 16:58:58 localhost.localdomain systemd[1]: Started MySQL Server.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查看初始化密码
启动成功后,可以在/var/log/mysqld.log
文件中查看密码
[root@localhost ~]# cat /var/log/mysqld.log | grep password
2022-06-17T08:58:55.406319Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: I;-C<tB>u4*A
1
2
2
# 修改密码
# 登录
[root@localhost ~]# mysql -h localhost -u root -p'I;-C<tB>u4*A'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.25
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 WITH mysql_native_password BY 'Pwd@1234';
Query OK, 0 rows affected (0.00 sec)
# 退出
mysql> exit;
Bye
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 授权其他用户远程登录
为了安全,不允许远程用户使用root用户登录,这里我们创建一个用户 aotuxx
供远程用户登录使用。
mysql> CREATE USER 'aotuxx'@'%' IDENTIFIED BY 'Aotu@123';
Query OK, 0 rows affected (0.01 sec)
# aotuxx.*代表赋予aotuxx数据库所有操作权限, 如需要赋予所有数据库权限, 可设置为*.*
# 'aotuxx'@'%'代表允许aotuxx用户在任何ip登录, 也可以指定具体ip, 如'用户名称'@'ip地址'
mysql> GRANT ALL PRIVILEGES ON *.* TO 'aotuxx'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
# 刷新权限
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 其他命令
# 启动mysql服务
systemctl start mysqld.service
# 停止mysql服务
systemctl stop mysqld.service
# 重启mysql服务
systemctl restart mysqld.service
# 查看mysql服务当前状态
systemctl status mysqld.service
# 设置mysql服务开机自启动
systemctl enable mysqld.service
# 停止mysql服务开机自启动
systemctl disable mysqld.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17