前提:可以输入命令检测是否安装了mysql
在Linux中,可以通过以下命令检查是否安装了MySQL:
代码语言:bash复制mysql --version
如果MySQL已安装,将显示版本信息。如果未安装,则会提示命令未找到。你也可以使用以下命令:
对于Debian/Ubuntu系统
代码语言:bash复制dpkg -l | grep mysql
对于Red Hat/CentOS系统
代码语言:bash复制rpm -qa | grep mysql
这些命令可以列出已安装的MySQL相关包。如果没有任何输出,说明系统中未安装MySQL。
如果系统上已安装有maraidb数据库,需要卸载mariadb
下面开始安装MySQL8.0
代码语言:bash复制yum install mysql
代码语言:bash复制[root@VM-7-7-tencentos ~]# yum install mysql
Last metadata expiration check: 0:35:51 ago on Tue 17 Sep 2024 05:38:17 PM CST.
Dependencies resolved.
======================================================================================================================================================================================
Package Architecture Version Repository Size
======================================================================================================================================================================================
Installing:
mysql x86_64 8.0.36-1.module el8.8.0 599 dab32cb8 AppStream 14 M
Installing dependencies:
mysql-common x86_64 8.0.36-1.module el8.8.0 599 dab32cb8 AppStream 136 k
Enabling module streams:
mysql 8.0
Transaction Summary
======================================================================================================================================================================================
Install 2 Packages
Total download size: 15 M
Installed size: 73 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): mysql-common-8.0.36-1.module el8.8.0 599 dab32cb8.x86_64.rpm 2.6 MB/s | 136 kB 00:00
(2/2): mysql-8.0.36-1.module el8.8.0 599 dab32cb8.x86_64.rpm 32 MB/s | 14 MB 00:00
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 32 MB/s | 15 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : mysql-common-8.0.36-1.module el8.8.0 599 dab32cb8.x86_64 1/2
Installing : mysql-8.0.36-1.module el8.8.0 599 dab32cb8.x86_64 2/2
Running scriptlet: mysql-8.0.36-1.module el8.8.0 599 dab32cb8.x86_64 2/2
Verifying : mysql-8.0.36-1.module el8.8.0 599 dab32cb8.x86_64 1/2
Verifying : mysql-common-8.0.36-1.module el8.8.0 599 dab32cb8.x86_64 2/2
Installed:
mysql-8.0.36-1.module el8.8.0 599 dab32cb8.x86_64 mysql-common-8.0.36-1.module el8.8.0 599 dab32cb8.x86_64
Complete!
dnf module list mysql
# 查看可用的 MySQL 模块版本
dnf module enable mysql:latest # 或者指定一个具体的版本,如 mysql:8.0
# 启用 MySQL 模块
# 安装 MySQL 服务器
代码语言:bash复制dnf install mysql-server
如果不确定服务名,可以尝试更通用的搜索 找到
代码语言:bash复制 journalctl | grep mysql
COMMAND后面为MySQL服务名
代码语言:bash复制COMMAND=/bin/systemctl start mysqld
代码语言:bash复制[root@VM-7-7-tencentos ~]# journalctl | grep mysql
Sep 17 18:01:08 VM-7-7-tencentos dbus-daemon[722]: [system] Activating via systemd: service name='org.freedesktop.PackageKit' unit='packagekit.service' requested by ':1.83' (uid=0 pid=40161 comm="/usr/libexec/pk-command-not-found mysql --version ")
Sep 17 18:22:03 VM-7-7-tencentos sudo[49211]: root : TTY=pts/2 ; PWD=/root ; USER=root ; COMMAND=/bin/systemctl start mysqld
Sep 17 18:25:33 VM-7-7-tencentos groupadd[50523]: group added to /etc/group: name=mysql, GID=27
Sep 17 18:25:33 VM-7-7-tencentos groupadd[50523]: group added to /etc/gshadow: name=mysql
Sep 17 18:25:33 VM-7-7-tencentos groupadd[50523]: new group: name=mysql, GID=27
Sep 17 18:25:33 VM-7-7-tencentos useradd[50529]: new user: name=mysql, UID=27, GID=27, home=/var/lib/mysql, shell=/sbin/nologin
获取到服务名后启动服务
代码语言:txt复制systemctl start mysqld
查看服务启动状态如下
代码语言:bash复制systemctl status mysqld
代码语言:bash复制[root@VM-7-7-tencentos ~]# 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 2024-09-17 18:35:59 CST; 55s ago
Process: 55317 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
Process: 55222 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
Process: 55183 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
Main PID: 55261 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 10570)
Memory: 376.5M
CGroup: /system.slice/mysqld.service
└─55261 /usr/libexec/mysqld --basedir=/usr
Sep 17 18:35:58 VM-7-7-tencentos systemd[1]: Starting MySQL 8.0 database server...
Sep 17 18:35:59 VM-7-7-tencentos systemd[1]: Started MySQL 8.0 database server.
获取临时密码
安装完成后,MySQL 会生成一个临时密码。你可以通过查看 MySQL 的日志来获取这个密码:
PS:因每个人安装环境不同,可能路径会有不一致 提示No such file or directory,可以手动进入/var/log下目录查找mysqld.log文件
代码语言:bash复制sudo grep 'password' /var/log/mysql/mysqld.log
出现如下提示在初始化MySQL数据库时使用了--initialize-insecure
选项,这个选项会导致root用户被创建时没有任何密码(即密码为空)。
[root@VM-7-7-tencentos mysql]# sudo grep ' password' /var/log/mysql/mysqld.log
2024-09-17T10:27:07.048379Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
尝试连接到MySQL服务器
最直接的方法是尝试连接到MySQL服务器。你可以使用mysql
命令行客户端:
因数据库创建时没有任何密,直接回车登录
代码语言:bash复制mysql -u root -p
代码语言:bash复制[root@VM-7-7-tencentos 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.36 Source distribution
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.
修改root账号密码, 'new_password' 为用户设置的密码
代码语言:bash复制ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
修改成功后刷新权限并退出(exit)。
代码语言:bash复制FLUSH PRIVILEGES;
修改任何IP地址的客户端都可以用root用户连接到服务器,修改后需要刷新
代码语言:bash复制GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;