最近自己centos虚拟机里面的mariadb忘记了密码,登录不进去了,直接安装一个mysql。
1、首先需要先卸载mariadb
使用rpm命令查询mariadb相关安装包:
rpm -qa|grep mariadb
然后用下面命令卸载:
yum remove mariadb
执行完上面卸载命令后,再用命令rpm -qa|grep mariadb看一下剩余哪些相关mariadb包,如果还剩余如下mariadb-libs-xxxx的包,需要再单独卸载,否则在安装mysql的时候会报如下的包冲突:
ile /usr/share/mysql/czech/errmsg.sys from install of mysql-community-common-5.7.44-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.64-1.el7.x86_64
卸载mariadb-libs-xxxx包的命令如下:
yum remove mariadb-libs-1:5.5.64-1.el7.x86_64
后面的xxx要按你本地的包版本来替换
1.1 卸载mariadb踩的坑
刚开始是不懂怎么卸载mariadb,然后上网搜了一下,搜到下面卸载所有mariadb包的方法,然后就掉坑里了,就是我卸载完这些包之后,rpm命令竟然也跟着卸载了,不能用了。不要用下面的方式卸载mariadb,血的教训:
rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
rpm -e --nodeps mariadb-server-5.5.64-1.el7.x86_64
rpm -e --nodeps mariadb-devel-5.5.64-1.el7.x86_64
rpm -e --nodeps mariadb-5.5.64-1.el7.x86_64
1.1.1 填坑 ,把rpm装回来
CentOS7-rpm命令找不到问题解决
填完坑然后继续
1.2 删除mariadb残留文件
卸载maradb后,原数据库的数据文件不会被删掉,如果重新安装mysql,还是会读到旧的数据,所以此时需要删除旧数据,旧数据默认存放在/var/lib/mysql目录,可以重命名该文件夹或者直接删除
同样的maradb的配置文件最好也要删除掉,否则新安装的mysql有可能读到mariadb的配置文件,配置文件的默认存储路径为:/etc/my.cnf
2、下载mysql安装包
下载地址:
MySQL :: Download MySQL Community Server
选择操作系统和版本:
这里因为我的centos版本是7,试着安装myql 8.0以上的版本装不上,提示c++库不支持,就装了个5.7.38的版本
下载完成后解压
tar -xvpf mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar
解压后有如下的rpm包:
[root@localhost mysql]# ll
总用量 1083048
-rw-r--r--. 1 root root 554516480 3月 23 01:37 mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar
-rw-r--r--. 1 7155 31415 28991900 3月 23 01:26 mysql-community-client-5.7.38-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 318868 3月 23 01:26 mysql-community-common-5.7.38-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 4363096 3月 23 01:26 mysql-community-devel-5.7.38-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 47993516 3月 23 01:26 mysql-community-embedded-5.7.38-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 23315792 3月 23 01:26 mysql-community-embedded-compat-5.7.38-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 132675656 3月 23 01:26 mysql-community-embedded-devel-5.7.38-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 2704332 3月 23 01:26 mysql-community-libs-5.7.38-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 1264876 3月 23 01:26 mysql-community-libs-compat-5.7.38-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 186231736 3月 23 01:27 mysql-community-server-5.7.38-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 126641600 3月 23 01:27 mysql-community-test-5.7.38-1.el7.x86_64.rpm
3、安装mysql
以上那么多包,我们只需要安装下面几个就行了,下面是安装命令:
rpm -ivh mysql-community-common-5.7.38-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.38-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.38-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.38-1.el7.x86_64.rpm
然后启动:
systemctl start mysqld
systemctl enable mysqld
4、进入mysql
新安装的myslq,初始化密码会打印在日志中,查找初始化密码:
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2022-07-29T01:14:01.932720Z 1 [Note] A temporary password is generated for root@localhost: :D8TUZf>EOk<
mysql安装完成后,都需要修改密码才有权限操作数据库,修改密码的方式很多,下面是使用mysqladmin命令修改密码的方式:
[root@localhost mariadb]# mysqladmin -uroot -p":D8TUZf>EOk<" password 新密码
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
使用mysql -uroot -p登录mysql
[root@localhost mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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.
4.1 更改密码
首次登录后,一般要求你更改root用户密码,输入如下sql:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxx';
root表示修改的是root用户的密码,@后面的localhost是允许该用户从该主机登录,xxxxx为指定的密码
4.2 配置允许远程连接
修改完成后需要用新的密码登录mysql,然后,为了让root用户能从其他机器登录,还需要做如下修改:
use mysql;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxxx';
第一个位置,为数据库, 第二个位置,为表,所以 *.*,表示可以访问任意数据的任意表
'root'@'%', root表示远端登录使用的用户名,%表示允许任意ip登录,可将指定ip替换掉%, root与%可以自定义
IDENTIFIED BY 'xxxx' 这个xxxx 是登录时的使用的密码,也相当于改密码
5、找回密码
如果安装完成后没找到初始密码,可以按下面的博文找回密码:
linux下mysql5.7初始密码查看及忘记密码重置_mysql第一次密码没改-CSDN博客
参考文章: