最近自己centos虚拟机里面的mariadb忘记了密码,登录不进去了,解决安装一个mysql。
使用rpm命令查询mariadb相关安装包:
rpm -qa|grep 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
但是这里会有个坑,就是我卸载完这些包之后,rpm命令竟然也跟着卸载了,不能用了。为了填坑,我参考了下面这篇博文把rpm命令给重新装回来了
填完坑然后继续
卸载maradb后,原数据库的数据文件不会被删掉,如果重新安装mysql,还是会读到旧的数据,所以此时需要删除旧数据,旧数据默认存放在/var/lib/mysql目录,可以重命名该文件夹或者直接删除
同样的maradb的配置文件最好也要删除掉,否则新安装的mysql有可能读到mariadb的配置文件,配置文件的默认存储路径为:/etc/my.cnf
下载地址:
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
以上那么多包,我们只需要安装下面几个就行了,下面是安装命令:
- 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
新安装的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 -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.
-
参考文章: