环境:
centos 7.5 mysql版本 5.5
1)下载数据库
[root@hya ~]# yum -y install mariadb mariadb-server
[root@hya ~]# rpm -qa mariadb-*
mariadb-libs-5.5.65-1.el7.x86_64
mariadb-server-5.5.65-1.el7.x86_64
2)启动数据库并设置密码
[root@hya ~]# systemctl start mariadb
[root@hya ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
[root@hya ~]# mysqladmin -uroot password 'hya123';
或者也可以
##################################################
mysql> use mysql;
mysql> update user set password=PASSWORD('hya123') where user='root';
mysql> flush privileges;
##################################################
正常情况下(知道密码)修改密码
mysqladmin -uroot -phya password ‘新密码’;
3)模拟忘记密码后的恢复
###首先关闭mysql
[root@hya ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables
[root@hya ~]# systemctl restart mariadb
[root@hya ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
MariaDB [(none)]> use mysql;
MariaDB [mysql]> update user set password=PASSWORD('xiaohan123') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0 ###修改成功
###5.7版本不同,为以下命令
mysql> update mysql.user set authentication_string = password("xiaohan123") where user="root" and host="localhost";
####
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> exit
Bye
###然后进my.cnf删除刚才添加的那一条跳过所有错误的代码
[root@hya ~]# vim /etc/my.cnf
skip-grant-tables 删除
[root@hya ~]# systemctl restart mariadb 密码修改完成后记得重启mysql
或者###############################################################
cd /usr/local/mysql/bin
./mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
###################################################################
4)测试
[root@hya ~]# mysql -uroot -pxiaohan123
Welcome to the MariaDB monitor. Commands end with ; or \g.
###可以了