MySQL·忘记密码

来源:互联网 发布:python隐马尔可夫模型 编辑:程序博客网 时间:2024/06/03 17:09

1、首先停止数据库


[root@localhost /]# systemctl stop mariadb.service


2、在my.cnf中加入skip-grant-tables 


[root@localhost /]# vi etc/my.cnf


在[mysqld]的段中加上一句:skip-grant-tables 
[mysqld]

skip-grant-tables 

然后保存

[root@localhost /]# systemctl restart mariadb.service




3、进入mysql

[root@localhost /]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-MariaDB MariaDB Server


Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MariaDB [(none)]> use mysql


Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A


Database changed

4、update更新密码

MariaDB [mysql]> UPDATE user SET Password=password('123456')WHERE User='root';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4  Changed: 4  Warnings: 0


MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)


MariaDB [mysql]> quit

Bye


5、在[mysqld]的段中删除或注释:skip-grant-tables 

[mysqld]

#skip-grant-tables 


进入my.cnf 删除后并重启MySQL


root@localhost /]# vi etc/my.cnf

[root@localhost /]# systemctl restart mariadb.service


进入MySQL


[root@localhost /]# mysql -u root -p123456

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-MariaDB MariaDB Server