CentOS安装MySQL数据库
本文中提到的CentOS版本为Stream9,MySQL版本为8.0
查询是否已经安装过MySQL
如果我们之前已经安装过MySQL,需要先将其卸载,以免造成冲突。
[root@bogon ~]# rpm -qa | grep mysql
一般来说,新部署的系统如果没有在安装系统时勾选直接部署MySQL,则一般不会安装相关组件。
通过wget
命令进行rpm的获取
[root@bogon ~]# wget https://dev.mysql.com/get/mysql80-community-release-el9-3.noarch.rpm
--2024-09-18 22:48:58-- https://dev.mysql.com/get/mysql80-community-release-el9-3.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 23.59.33.108, 2600:1406:4400:491::2e31, 2600:1406:4400:481::2e31
Connecting to dev.mysql.com (dev.mysql.com)|23.59.33.108|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://repo.mysql.com//mysql80-community-release-el9-3.noarch.rpm [following]
--2024-09-18 22:48:59-- https://repo.mysql.com//mysql80-community-release-el9-3.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.34.32.219, 2600:1417:4400:e8c::1d68, 2600:1417:4400:e84::1d68
Connecting to repo.mysql.com (repo.mysql.com)|23.34.32.219|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10715 (10K) [application/x-redhat-package-manager]
Saving to: ‘mysql80-community-release-el9-3.noarch.rpm’
mysql80-community-release-el9-3. 100%[=========================================================>] 10.46K --.-KB/s in 0s
2024-09-18 22:49:00 (130 MB/s) - ‘mysql80-community-release-el9-3.noarch.rpm’ saved [10715/10715]
在正确获取到rpm包后,我们就可以进行安装了。
安装MySQL
[root@bogon ~]# rpm -ivh mysql80-community-release-el9-3.noarch.rpm
warning: mysql80-community-release-el9-3.noarch.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:mysql80-community-release-el9-3 ################################# [100%]
[root@bogon ~]# dnf install mysql-community-server -y
CentOS Stream 9 - BaseOS 4.7 kB/s | 6.9 kB 00:01
CentOS Stream 9 - BaseOS 2.9 MB/s | 8.3 MB 00:02
CentOS Stream 9 - AppStream 9.9 kB/s | 7.7 kB 00:00
CentOS Stream 9 - AppStream 3.7 MB/s | 20 MB 00:05
CentOS Stream 9 - Extras packages 14 kB/s | 8.6 kB 00:00
gitlab-ce 29 kB/s | 3.0 kB 00:00
MySQL 8.0 Community Server 821 kB/s | 1.9 MB 00:02
MySQL Connectors Community 52 kB/s | 61 kB 00:01
MySQL Tools Community 320 kB/s | 721 kB 00:02
Dependencies resolved.
=================================================================================================================================== Package Architecture Version Repository Size
===================================================================================================================================Installing:
mysql-community-server x86_64 8.0.39-1.el9 mysql80-community 50 M
Installing dependencies:
mysql-community-client x86_64 8.0.39-1.el9 mysql80-community 3.4 M
mysql-community-client-plugins x86_64 8.0.39-1.el9 mysql80-community 1.4 M
mysql-community-common x86_64 8.0.39-1.el9 mysql80-community 556 k
mysql-community-icu-data-files x86_64 8.0.39-1.el9 mysql80-community 2.3 M
mysql-community-libs x86_64 8.0.39-1.el9 mysql80-community 1.5 M
Transaction Summary
===================================================================================================================================Install 6 Packages
Total download size: 59 M
Installed size: 335 M
Downloading Packages:
(1/6): mysql-community-client-plugins-8.0.39-1.el9.x86_64.rpm 914 kB/s | 1.4 MB 00:01
(2/6): mysql-community-common-8.0.39-1.el9.x86_64.rpm 245 kB/s | 556 kB 00:02
(3/6): mysql-community-icu-data-files-8.0.39-1.el9.x86_64.rpm 2.0 MB/s | 2.3 MB 00:01
(4/6): mysql-community-client-8.0.39-1.el9.x86_64.rpm 1.2 MB/s | 3.4 MB 00:02
(5/6): mysql-community-libs-8.0.39-1.el9.x86_64.rpm 861 kB/s | 1.5 MB 00:01
(6/6): mysql-community-server-8.0.39-1.el9.x86_64.rpm 6.8 MB/s | 50 MB 00:07
-----------------------------------------------------------------------------------------------------------------------------------Total 5.9 MB/s | 59 MB 00:09
MySQL 8.0 Community Server 3.0 MB/s | 3.1 kB 00:00
Importing GPG key 0x3A79BD29:
Userid : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
Fingerprint: 859B E8D7 C586 F538 430B 19C2 467B 942D 3A79 BD29
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Key imported successfully
Import of key(s) didn't help, wrong key(s)?
Public key for mysql-community-client-8.0.39-1.el9.x86_64.rpm is not installed. Failing package is: mysql-community-client-8.0.39-1.el9.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Public key for mysql-community-client-plugins-8.0.39-1.el9.x86_64.rpm is not installed. Failing package is: mysql-community-client-plugins-8.0.39-1.el9.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Public key for mysql-community-common-8.0.39-1.el9.x86_64.rpm is not installed. Failing package is: mysql-community-common-8.0.39-1.el9.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Public key for mysql-community-icu-data-files-8.0.39-1.el9.x86_64.rpm is not installed. Failing package is: mysql-community-icu-data-files-8.0.39-1.el9.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Public key for mysql-community-libs-8.0.39-1.el9.x86_64.rpm is not installed. Failing package is: mysql-community-libs-8.0.39-1.el9.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Public key for mysql-community-server-8.0.39-1.el9.x86_64.rpm is not installed. Failing package is: mysql-community-server-8.0.39-1.el9.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
The downloaded packages were saved in cache until the next successful transaction.
You can remove cached packages by executing 'dnf clean packages'.
Error: GPG check FAILED
可以看到,在安装的过程中出现了GPG校验失败的错误,我们可以通过--nogpgcheck
参数进行屏蔽。
[root@bogon ~]# dnf install mysql-community-server -y --nogpgcheck
Last metadata expiration check: 0:00:40 ago on Wed 18 Sep 2024 10:49:42 PM CST.
Dependencies resolved.
=================================================================================================================================== Package Architecture Version Repository Size
===================================================================================================================================Installing:
mysql-community-server x86_64 8.0.39-1.el9 mysql80-community 50 M
Installing dependencies:
mysql-community-client x86_64 8.0.39-1.el9 mysql80-community 3.4 M
mysql-community-client-plugins x86_64 8.0.39-1.el9 mysql80-community 1.4 M
mysql-community-common x86_64 8.0.39-1.el9 mysql80-community 556 k
mysql-community-icu-data-files x86_64 8.0.39-1.el9 mysql80-community 2.3 M
mysql-community-libs x86_64 8.0.39-1.el9 mysql80-community 1.5 M
Transaction Summary
===================================================================================================================================Install 6 Packages
Total size: 59 M
Installed size: 335 M
Downloading Packages:
[SKIPPED] mysql-community-client-8.0.39-1.el9.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-client-plugins-8.0.39-1.el9.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-common-8.0.39-1.el9.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-icu-data-files-8.0.39-1.el9.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-libs-8.0.39-1.el9.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-server-8.0.39-1.el9.x86_64.rpm: Already downloaded
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : mysql-community-common-8.0.39-1.el9.x86_64 1/6
Installing : mysql-community-client-plugins-8.0.39-1.el9.x86_64 2/6
Installing : mysql-community-libs-8.0.39-1.el9.x86_64 3/6
Running scriptlet: mysql-community-libs-8.0.39-1.el9.x86_64 3/6
Installing : mysql-community-client-8.0.39-1.el9.x86_64 4/6
Installing : mysql-community-icu-data-files-8.0.39-1.el9.x86_64 5/6
Running scriptlet: mysql-community-server-8.0.39-1.el9.x86_64 6/6
Installing : mysql-community-server-8.0.39-1.el9.x86_64 6/6
Running scriptlet: mysql-community-server-8.0.39-1.el9.x86_64 6/6
Verifying : mysql-community-client-8.0.39-1.el9.x86_64 1/6
Verifying : mysql-community-client-plugins-8.0.39-1.el9.x86_64 2/6
Verifying : mysql-community-common-8.0.39-1.el9.x86_64 3/6
Verifying : mysql-community-icu-data-files-8.0.39-1.el9.x86_64 4/6
Verifying : mysql-community-libs-8.0.39-1.el9.x86_64 5/6
Verifying : mysql-community-server-8.0.39-1.el9.x86_64 6/6
Installed:
mysql-community-client-8.0.39-1.el9.x86_64 mysql-community-client-plugins-8.0.39-1.el9.x86_64
mysql-community-common-8.0.39-1.el9.x86_64 mysql-community-icu-data-files-8.0.39-1.el9.x86_64
mysql-community-libs-8.0.39-1.el9.x86_64 mysql-community-server-8.0.39-1.el9.x86_64
Complete!
至此,我们的MySQL8.0就安装成功了,但是此时我们还没有启动服务,我们可以通过下列命令启动MySQL服务并将其添加到系统服务中。
启动MySQL服务
[root@bogon ~]# systemctl enable --now mysqld.service
[root@bogon ~]# systemctl status mysqld.service
之后,我们查看一下服务状态
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)
Active: active (running) since Wed 2024-09-18 22:50:47 CST; 2s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 20216 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 28994 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 48764)
Memory: 478.6M
CPU: 3.665s
CGroup: /system.slice/mysqld.service
└─28994 /usr/sbin/mysqld
Sep 18 22:50:40 bogon systemd[1]: Starting MySQL Server...
Sep 18 22:50:47 bogon systemd[1]: Started MySQL Server.
可以看到MySQL服务被顺利启动了。
到这里我们还不能够正常的使用MySQL,因为还需要知道root
用户的密码,才可以正常登录。
这里我们使用以下命令来查看root密码。
需要注意的是,这里面的root密码只会在一定时间周期内显示,过期将自动删除,所以我们需要尽快更改root密码
[root@bogon ~]# grep "password" /var/log/mysqld.log
2024-09-18T14:50:44.272448Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: dggfn4YAhd,i
我们可以看到此时我们的密码为dggfn4YAhd,i
修改MySQL中root用户密码
首先用上面给出的密码登录我们的MySQL
[root@bogon ~]# mysql -u root -p
出现以下提示证明已经成功登录MySQL数据库
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.39
Copyright (c) 2000, 2024, 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 "*****";
"*****"
替换成你的密码,这里面要符合密码复杂度要求。
我强烈不建议修改MySQL的密码复杂度校验,需要养成好习惯,由于修改成简单密码可能会带来的血与泪的教训
修改MySQL中允许root用户远程登录
完成上述任务后,我们还需要允许root用户远程登录以便我们更好的管理数据库
如何你没有远程登录数据库的需求,后面的内容可以忽略
mysql> use mysql;
mysql> update user set host="%" where user="root";
mysql> flush privileges;
上述命令干了三件事情
- 切换到mysql这个数据库
- 把root用户允许登录的机器名更改为任意
- 刷新权限
至此,我们就可以愉快的使用MySQL数据库了。