Linux总结之CentOS Stream 9安装mysql8.0实操安装成功记录
由于网上很多的mysql8.0安装教程都是老版本或者安装过程记录有问题,导致经常安装到一半需要删除重新安装。所以将成功的实操安装过程记录一下,方面后面查阅,大家还有问题的可以在此讨论。
一、安装之前先做删除操作,检查一下之前有没有安装过
不重复写了,参考教程如下:删除linux的mysql
二、首先添加MySQL官方仓库
- 下载MySQL仓库的rpm包
dnf install -y https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm
- 安装MySQL GPG密钥
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
三、安装MySQL服务器
dnf install -y --nogpgcheck mysql-community-server
四、启动MySQL服务并设置开机自启
- 启动mysql服务
systemctl start mysqld
- 设置开机自启
systemctl enable mysqld
- 检查服务状态
systemctl status mysqld
五、获取初始密码并启动
- 查看初始随机密码
grep 'temporary password' /var/log/mysqld.log
- 使用初始密码登录MySQL
- 复制上密码是看不见的,复制完直接回车登陆就可以
mysql -u root -p
六、修改root的密码
- 在MySQL命令行中执行
- 新密码必须包含大小写字母、数字和特殊字符,长度至少8位。
ALTER USER 'root'@'localhost' IDENTIFIED BY '你的新密码';
- 创建可以远程访问的账户,注意替换账号和密码
CREATE USER 'username'@'%' IDENTIFIED BY '密码';
- 配置远程访问
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%';
- 刷新用户
FLUSH PRIVILEGES;
七、常用命令
# 启动MySQL服务
systemctl start mysqld# 停止MySQL服务
systemctl stop mysqld# 重启MySQL服务
systemctl restart mysqld# 查看MySQL服务状态
systemctl status mysqld# 开放MySQL默认端口3306
firewall-cmd --permanent --add-port=3306/tcp# 重载防火墙配置
firewall-cmd --reload
八、过程问题记录
1. 服务启动失败
Job for mysqld.service failed because the control process exited with error code.
See “systemctl status mysqld.service” and “journalctl -xeu mysqld.service” for details.
可能问题原因
数据目录权限问题
内存不足
配置文件错误
端口被占用
依次排查下吧,不写过程了
2.配置文件 /etc/my.cnf 的格式错误,这个是通过status命令查看的启动状态
× mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Sat 2024-12-28 14:44:33 CST; 21s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 2264526 (code=exited, status=1/FAILURE)
Status: “Server startup in progress”
CPU: 68ms
Dec 28 14:44:33 VM-16-6-centos mysqld_pre_systemd[2264407]: my_print_defaults: [ERROR] Fatal error in defaults handling. Program aborted!
Dec 28 14:44:33 VM-16-6-centos mysqld_pre_systemd[2264417]: my_print_defaults: [ERROR] Found option without preceding group in config file /etc/my.cnf at line 1.
Dec 28 14:44:33 VM-16-6-centos mysqld_pre_systemd[2264417]: my_print_defaults: [ERROR] Fatal error in defaults handling. Program aborted!
Dec 28 14:44:33 VM-16-6-centos mysqld_pre_systemd[2264496]: mysqld: [ERROR] Found option without preceding group in config file /etc/my.cnf at line 1.
Dec 28 14:44:33 VM-16-6-centos mysqld_pre_systemd[2264496]: mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
Dec 28 14:44:33 VM-16-6-centos mysqld[2264526]: mysqld: [ERROR] Found option without preceding group in config file /etc/my.cnf at line 1.
Dec 28 14:44:33 VM-16-6-centos mysqld[2264526]: mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
Dec 28 14:44:33 VM-16-6-centos systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE
Dec 28 14:44:33 VM-16-6-centos systemd[1]: mysqld.service: Failed with result ‘exit-code’.
Dec 28 14:44:33 VM-16-6-centos systemd[1]: Failed to start MySQL Server.
修改配置文件内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid[client]
socket=/var/lib/mysql/mysql.sock
保存文件后重启
systemctl restart mysqld
以上,如果还有其他问题可以评论讨论或者私信。