当前位置:网站首页>Linux环境安装mysql数据库详细教程(含卸载和密码重置过程)
Linux环境安装mysql数据库详细教程(含卸载和密码重置过程)
2022-06-11 19:33:00 【柯晓楠】
本教程适用于centos7/8,mysql 5.x
1、卸载mysql(重要)
在安装mysql之前要确保自己的系统中没有mysql,即使你是刚刚重装的系统或者是刚购买的云服务,也需要检查一下是否存在mysql相关的文件,这一点很重要!(腾讯云的一些镜像默认包含mysql的文件),安装前如果已经存在mysql相关的文件,会导致后续的mysql安装失败。
完全删除mysql步骤:
(1)使用 rpm 命令查看已安装的安装包
输入以下命令检查是否安装了mysql软件:
rpm -qa | grep mysql
如果查到了相关文件,例如
使用 rpm -e 文件名 的命令删除该文件,以上面文件为例,执行以下命令:
rpm -e mysql57-community-release-el7-10.noarch
重复使用 rpm -e 命令删除文件,直到所有文件完全删除
(2)使用yum卸载安装的mysql
yum remove mysql mysql-server mysql-libs mysql-server
(3)搜索mysql文件
全局搜索名称包含 mysql 的所有文件
find / -name '*mysql*'
删除搜索出来的所有 mysql 文件,如果没有安装过mysql,一般会搜索出来 /var/lib/* 和 /usr/share/* 这两个目录下包含 mysql 文件。删除文件命令:
# 以 /var/lib/mysql 为例
rm -rf /var/lib/mysql
使用 rm -rf 命令删除所有搜索出来的mysql文件。
Linux中复制快捷键:
ctrl + insert,粘贴快捷键:shift + insert或使用鼠标中键
最后再检查一下:
# 检查1
rpm -qa|grep mysql
# 检查2
find / -name '*mysql*'
如果有的话继续执行删除,直到删干净为止!
2、下载mysql安装文件
Linux平台上推荐使用RPM包来安装mysql。
下载:
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm安装:
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum安装mysql-server:
yum install -y mysql-server
如果没有权限的,可以执行下面命令设置权限(root用户不用执行):
chown -R mysql:mysql /var/lib/mysql/
启动服务:
systemctl start mysqld.service
查看mysql运行状态:
service mysqld status

查看初始密码:
grep 'password' /var/log/mysqld.log

红框标记处为初始密码,但是有的情况查询不到初始密码,后面会说怎么修改密码。
复制初始密码,进行登录:
mysql -u root -p
输入命令后,点击回车,会让你输入密码,然后输入刚刚的初始密码(输密码的时候不可见),当然支持粘贴,你可以复制初始密码后,然后粘贴。如果没有密码,直接回车即可。
登录成功后显示:
查看所有的数据库:
到此,mysql数据库就安装完成了。
3、修改mysql密码
很多情况下,需要修改初始密码,但是如果在前面步骤中查看不到初始密码,也是没办法登录数据库的,这时就需要修改密码。我们就以无法查看初始密码为例(或是忘记了登录密码),简单介绍一下密码修改过程。
(1)设置跳过登录密码
打开mysql的配置文件:
vi /etc/my.cnf
打开文件后按 a 键进入编辑模式,在 [mysqld] 配置的下面添加 skip-grant-tables,如下图所示:
然后按 esc 键,输入 :wq 保存文件。
(2)重启mysql服务
service mysqld restart
(3)进入mysql
mysql -u root -p
输入完上面命令后按回车,会跳过密码验证,直接进入到mysql中。
(4)修改用户登录密码
进入 mysql 库:
use mysql

修改密码:
update user set password=password('你设置的密码') where User='root' and Host='localhost';
注意: 设置了 Host=‘localhost’ 可能会导致远程连接失败!
更新缓存:
flush privileges;
退出mysql:
exit
(4)还原配置文件
重新进入 my.cnf 文件注释掉 skip-grant-tables,效果如下图所示
按 esc 键退出编辑模式,输入 :wq 保存文件。
再次重启mysql服务:
service mysqld restart
然后连接数据库,输入你修改后的密码看能否进入,能够进入就说明密码修改成功,否则再重新按照步骤操作一遍。
边栏推荐
- Yolov3 pytoch code and principle analysis (I): runthrough code
- YOLOv3 Pytorch代码及原理分析(一):跑通代码
- [Multisim Simulation] using operational amplifier to generate sawtooth wave
- collect. stream(). Use of the collect() method
- Learn about random library · 1
- Qubicle notes: Hello voxel
- CMU 15-445 數據庫課程第五課文字版 - 緩沖池
- Flash ckeditor rich text compiler can upload and echo images of articles and solve the problem of path errors
- Tensorflow---TFRecord文件的创建与读取
- Golang学习笔记—基础篇
猜你喜欢
![[video denoising] video denoising based on salt with matlab code](/img/79/e1f8255061a342e02232ac1e393217.png)
[video denoising] video denoising based on salt with matlab code

Software requirements engineering review

干货丨MapReduce的工作流程是怎样的?

5g communication test manual based on Ti am5728 + artix-7 FPGA development board (dsp+arm)

Practice of tag recognition based on Huawei cloud image

vs2010链接sql2008数据库时无法打开

Unsupervised image classification code analysis notes of scan:learning to classify images without (1): simclr

Merge multiple binary search trees

Building web applications

This article introduces you to the infrastructure of sofaregistry
随机推荐
YOLOv3 Pytorch代码及原理分析(二):网络结构和 Loss 计算
【 aide 】 comment puis - je faire en sorte que les messages sélectionnés ci - dessous puissent être affichés après l'ouverture de l'article Wechat public number dans un navigateur externe?
Merge sort divide and conquer
Crop disease detection using image processing technology and convolutional neural network (CNN)
High concurrency architecture design
Activate function formulas, derivatives, image notes
5g communication test manual based on Ti am5728 + artix-7 FPGA development board (dsp+arm)
Experiment report of basic mechanical experiment (II) of School of distance and continuing education, Dalian University of technology [standard answer]
SISO decoder for SPC (supplementary Chapter 1)
Internet_ Business Analysis Overview
MySQL federated index and BTREE
Hospital intelligent infusion management system source code hospital source code
Software requirements engineering review
I don't want to open an account online. Is it safe to open an account online?
Pymysql uses cursor operation database method to encapsulate!!!
CMU 15-445 数据库课程第五课文字版 - 缓冲池
2022 the latest software testing classic summarized by major manufacturers. After reading it, I'm not afraid I won't get an offer
Unsupervised image classification code analysis notes of scan:learning to classify images without (1): simclr
STL application (unfinished
Operator new and placement new