【MySQL】Linux(CentOS7)下安装MySQL8教程

一、下载安装包

华为镜像源:https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-8.0/

二、卸载系统自带的mariadb

查看系统中的mariadb

bash 复制代码
rpm -qa|grep mariadb

卸载mariadb

bash 复制代码
rpm -e mariadb-libs --nodeps

检查系统中是否存在mariadb

bash 复制代码
rpm -qa|grep mariadb

三、卸载系统自带的MySQL

关闭mysql服务

bash 复制代码
systemctl stop mysqld.service

查找安装mysql的rpm包

bash 复制代码
rpm -qa | grep -i mysql  

卸载mysql

找到了就卸载,没找到你卸载个锤子

下边的代码根据自己实际情况改

bash 复制代码
yum remove mysql-community-libs-5.7.29-1.el7.x86_64 mysql-community-common-5.7.29-1.el7.x86_64 mysql-community-client-5.7.29-1.el7.x86_64 mysql-community-server-5.7.29-1.el7.x86_64

查看是否卸载干净

bash 复制代码
rpm -qa | grep -i mysql

查找mysql相关目录 删除

bash 复制代码
find / -name mysql

该删的删,像Anacodna里的mysql就别动了

bash 复制代码
rm -rf /etc/selinux/targeted/active/modules/100/mysql
rm -rf/usr/lib64/mysql

删除默认配置日志

bash 复制代码
rm -rf /etc/my.cnf
rm -rf /var/log/mysqld.log

四、安装MySQL

创建MySQL目录

bash 复制代码
mkdir /export/software/mysql

上传压缩包到该文件夹

解压文件

bash 复制代码
tar -xvf mysql-8.0.29-1.el7.x86_64.rpm-bundle.tar

进行安装

bash 复制代码
yum -y install libaio

两种安装方法选一个就行

bash 复制代码
# 全量安装:功能全
yum install /export/software/mysql/*.rpm

# 核心安装:能跑能用
rpm -ivh mysql-community-common-8.0.29-1.el7.x86_64.rpm mysql-community-libs-8.0.29-1.el7.x86_64.rpm mysql-community-client-8.0.29-1.el7.x86_64.rpm mysql-community-server-8.0.29-1.el7.x86_64.rpm

五、配置MySQL

初始化

bash 复制代码
mysqld --initialize

更改所属组

bash 复制代码
chown mysql:mysql /var/lib/mysql -R

启动MySQL

bash 复制代码
systemctl start mysqld.service

我的这块报错,说是已经有了MySQL8.2,不能降级装8.0.29,原因是之前安装的Anaconda自带了一个MySQL,需要先把Anaconda的MySQL停掉

root@feilink1 mysql\]# cat /var/log/mysqld.log \| tail -50 2025-12-11T08:04:50.331212Z 0 \[System\] \[MY-015017\] \[Server\] MySQL Server Initialization - start. 2025-12-11T08:04:50.332261Z 0 \[System\] \[MY-013169\] \[Server\] /export/software/anaconda3/bin/mysqld (mysqld 8.4.0) initializing of server in progress as process 7632 2025-12-11T08:04:50.332393Z 0 \[ERROR\] \[MY-010338\] \[Server\] Can't find error-message file '/export/software/anaconda3/english/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive. 2025-12-11T08:04:50.333011Z 0 \[Warning\] \[MY-013242\] \[Server\] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 2025-12-11T08:04:50.333016Z 0 \[Warning\] \[MY-013244\] \[Server\] --collation-server: 'utf8mb3_general_ci' is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead. 2025-12-11T08:04:50.336781Z 1 \[System\] \[MY-013576\] \[InnoDB\] InnoDB initialization has started. 2025-12-11T08:04:50.481188Z 1 \[System\] \[MY-013577\] \[InnoDB\] InnoDB initialization has ended. 2025-12-11T08:04:51.378300Z 6 \[Note\] \[MY-010454\] \[Server\] A temporary password is generated for root@localhost: gh#Ob(GMo0ya 2025-12-11T08:04:52.613948Z 0 \[System\] \[MY-015018\] \[Server\] MySQL Server Initialization - end. 2025-12-11T08:05:24.993219Z 0 \[System\] \[MY-010116\] \[Server\] /usr/sbin/mysqld (mysqld 8.0.29) starting as process 7936 2025-12-11T08:05:24.997105Z 1 \[System\] \[MY-013576\] \[InnoDB\] InnoDB initialization has started. 2025-12-11T08:05:25.085757Z 1 \[ERROR\] \[MY-013171\] \[InnoDB\] Cannot boot server version 80029 on data directory built by version 80400. Downgrade is not supported mysqld: Can't open file: 'mysql.ibd' (errno: 0 - ) 2025-12-11T08:05:25.511718Z 1 \[ERROR\] \[MY-010334\] \[Server\] Failed to initialize DD Storage Engine 2025-12-11T08:05:25.512465Z 0 \[ERROR\] \[MY-010020\] \[Server\] Data Dictionary initialization failed. 2025-12-11T08:05:25.512616Z 0 \[ERROR\] \[MY-010119\] \[Server\] Aborting 2025-12-11T08:05:25.514166Z 0 \[System\] \[MY-010910\] \[Server\] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.29) MySQL Community Server - GPL. 2025-12-11T08:05:46.804765Z 0 \[System\] \[MY-010116\] \[Server\] /usr/sbin/mysqld (mysqld 8.0.29) starting as process 8138 2025-12-11T08:05:46.808283Z 1 \[System\] \[MY-013576\] \[InnoDB\] InnoDB initialization has started. 2025-12-11T08:05:46.967641Z 1 \[ERROR\] \[MY-013171\] \[InnoDB\] Cannot boot server version 80029 on data directory built by version 80400. Downgrade is not supported mysqld: Can't open file: 'mysql.ibd' (errno: 0 - ) 2025-12-11T08:05:47.354085Z 1 \[ERROR\] \[MY-010334\] \[Server\] Failed to initialize DD Storage Engine 2025-12-11T08:05:47.354355Z 0 \[ERROR\] \[MY-010020\] \[Server\] Data Dictionary initialization failed. 2025-12-11T08:05:47.354426Z 0 \[ERROR\] \[MY-010119\] \[Server\] Aborting 2025-12-11T08:05:47.354837Z 0 \[System\] \[MY-010910\] \[Server\] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.29) MySQL Community Server - GPL. 2025-12-11T08:08:58.025098Z 0 \[System\] \[MY-015017\] \[Server\] MySQL Server Initialization - start. 2025-12-11T08:08:58.025813Z 0 \[System\] \[MY-013169\] \[Server\] /export/software/anaconda3/bin/mysqld (mysqld 8.4.0) initializing of server in progress as process 9466 2025-12-11T08:08:58.025841Z 0 \[ERROR\] \[MY-010338\] \[Server\] Can't find error-message file '/export/software/anaconda3/english/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive. 2025-12-11T08:08:58.026389Z 0 \[Warning\] \[MY-013242\] \[Server\] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 2025-12-11T08:08:58.026394Z 0 \[Warning\] \[MY-013244\] \[Server\] --collation-server: 'utf8mb3_general_ci' is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead. 2025-12-11T08:08:58.029460Z 1 \[System\] \[MY-013576\] \[InnoDB\] InnoDB initialization has started. 2025-12-11T08:08:58.178234Z 1 \[System\] \[MY-013577\] \[InnoDB\] InnoDB initialization has ended. 2025-12-11T08:08:58.959295Z 6 \[Note\] \[MY-010454\] \[Server\] A temporary password is generated for root@localhost: la4_bb\*o?Y\>C 2025-12-11T08:09:00.322790Z 0 \[System\] \[MY-015018\] \[Server\] MySQL Server Initialization - end. 2025-12-11T08:09:04.312480Z 0 \[System\] \[MY-010116\] \[Server\] /usr/sbin/mysqld (mysqld 8.0.29) starting as process 9576 2025-12-11T08:09:04.316521Z 1 \[System\] \[MY-013576\] \[InnoDB\] InnoDB initialization has started. 2025-12-11T08:09:04.411941Z 1 \[ERROR\] \[MY-013171\] \[InnoDB\] Cannot boot server version 80029 on data directory built by version 80400. Downgrade is not supported mysqld: Can't open file: 'mysql.ibd' (errno: 0 - ) 2025-12-11T08:09:04.831291Z 1 \[ERROR\] \[MY-010334\] \[Server\] Failed to initialize DD Storage Engine 2025-12-11T08:09:04.831447Z 0 \[ERROR\] \[MY-010020\] \[Server\] Data Dictionary initialization failed. 2025-12-11T08:09:04.831558Z 0 \[ERROR\] \[MY-010119\] \[Server\] Aborting 2025-12-11T08:09:04.832009Z 0 \[System\] \[MY-010910\] \[Server\] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.29) MySQL Community Server - GPL. \[root@feilink1 mysql\]#

bash 复制代码
mv /export/software/anaconda3/bin/mysqld /export/software/anaconda3/bin/mysqld.bak
mv /export/software/anaconda3/bin/mysql /export/software/anaconda3/bin/mysql.bak
bash 复制代码
systemctl stop mysqld
rm -rf /var/lib/mysql/*
mysqld --initialize --user=mysql
systemctl start mysqld

查看生成的临时root密码

bash 复制代码
cat  /var/log/mysqld.log | grep password

如果像我这种安装多次的就选最后一个

登录MySQL

bash 复制代码
mysql -uroot -p
# 生成的临时密码

修改root密码

sql 复制代码
# 更新用户密码,设置为123456
# 第一次改密码必须要符合安全验证"大写+小写+数字+特殊字符"
alter user user() identified by "ShaTeMeWanYi666*";
# 选择数据库
use mysql;
# 授权
CREATE USER 'root'@'%' IDENTIFIED BY 'ShaTeMeWanYi666*';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

如果你想改为123456这样的简单密码,需要设置一下

sql 复制代码
SET GLOBAL validate_password.policy = 0;
SET GLOBAL validate_password.length = 3;
SET GLOBAL validate_password.check_user_name = OFF;
sql 复制代码
SELECT user, host FROM mysql.user WHERE user='root';

我的只有两个,所以只改两个

sql 复制代码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ALTER USER 'root'@'%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;

先要更稳的话可以再加上两个root'@'127.0.0.1和root'@'::1

这几个的区别可以了解一下

sql 复制代码
CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;
FLUSH PRIVILEGES;
sql 复制代码
CREATE USER 'root'@'::1' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'::1' WITH GRANT OPTION;
FLUSH PRIVILEGES;

六、查看、开启、关闭mysql服务

bash 复制代码
# 查看mysql服务状态
systemctl status mysqld
# 关闭mysql服务
systemctl stop mysqld
# 查看mysql服务状态
systemctl status mysqld
# 开启mysql服务
systemctl start mysqld
# 查看mysql服务状态
systemctl status mysqld

七、设置为开机自启动

bash 复制代码
systemctl enable mysqld

查看是否设置自启动成功

bash 复制代码
systemctl list-unit-files | grep mysqld

前边这个enable了就行,后边那个不用管

骚年,准备好成为SQL Boy了么?

相关推荐
随祥2 小时前
网络开源工具
linux
数据库生产实战2 小时前
Oracle的_segment_count和3个event对高并发事务与索引性能的影响分析
数据库·oracle
北京迅为2 小时前
【北京迅为】iTOP-4412精英版使用手册-第七十八章 Qt界面切换
linux·人工智能·嵌入式·4412
程序员侠客行3 小时前
Mybatis二级缓存实现详解
java·数据库·后端·架构·mybatis
Tipriest_3 小时前
linux中的文本分接流tee命令介绍
linux·服务器·数据库
爱喝水的鱼丶3 小时前
SAP-ABAP:在SAP世界里与特殊字符“斗智斗勇”:一份来自实战的避坑指南
运维·服务器·数据库·学习·sap·abap·特殊字符
阿拉伯柠檬3 小时前
MySQL内置函数
linux·数据库·mysql·面试
小Mie不吃饭3 小时前
2025 Oracle小白零基础到入门的学习路线
数据库·oracle
计算机学姐3 小时前
基于SpringBoot的送货上门系统【2026最新】
java·vue.js·spring boot·后端·mysql·spring·tomcat