其实想写这个主要是因为工作时遇到一台部门自用测试机中MariaDB的root密码丢失的情况,同事的备份文件中也没有找到,这很尬尴,看了一下用的还是SCL中的版本,与自带的安装源版本不大一样,就记录一下该种方式的安装和怎么在密码丢失的情况下重置密码。
关于SCL
SCL(Software Collections)可以让你在同一个操作系统上安装和使用多个版本的软件,而不会影响整个系统的安装包。
SCL主页: https://www.softwarecollections.org/en/
安装SCL
1 | [root@toptop ~]# yum install epel-release -y |
安装MariaDB
SCL中关于MariaDB的介绍如下:
rh-mariadb102 - A recent stable release of MariaDB 10.2. This Software Collection gives users of CentOS and RHEL an alternative to MySQL, which is binary compatible with MariaDB in most practical cases and can be replaced with it.
那么现在进行安装:
1 | [root@toptop ~]# yum install rh-mariadb102 |
在会话中启用MariaDB命令,这样MariaDB中的命令就均在直接在bash中执行,无需进入相关目录:
1 | [root@toptop ~]# scl enable rh-mariadb102 bash |
开启MariaDB服务
1 | [root@toptop ~]# systemctl enable --now rh-mariadb102-mariadb.service |
此时可以查看MariaDB的安装位置和版本:
1 | [root@toptop ~]# which mysql |
MySQL安全配置向导
1 | [root@toptop ~]# mysql_secure_installation |
登录验证
1 | [root@toptop ~]# mysql -u root -p |
接下来,其他的配置文件就自行在配置文件调整吧。
修改密码
下面说一下MariaDB的root密码丢失时处理的过程:
1 | [root@toptop ~]# cd /etc/opt/rh/rh-mariadb102/my.cnf.d/ |
在mariadb-server.cnf
文件中增加如下一行:
1 | [mysqld] |
此时重启MariaDB服务,即可免密进入MariaDB:
1 | [root@toptop my.cnf.d]# systemctl restart rh-mariadb102-mariadb |
在数据库中执行如下语句:
1 | MariaDB [(none)]> use mysql |
接下来退出数据库,删除mariadb-server.cnf
中增加的skip-grant-tables
,重启服务,使用刚刚设置的密码,即可登录MariaDB。
1 | [root@toptop my.cnf.d]# systemctl restart rh-mariadb102-mariadb |
到此重置密码的过程结束,备份还是很重要的,应该防止此类问题再次发生。