MySQL單實例重置密碼的兩種方法
MySQL單實例重置密碼的兩種方法
在工作學習中,我們有時會忘記數據庫的密碼,下面是MySQL單實例密碼重置的步驟。
說明:
(1)[[email protected] ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)
(2)[[email protected] ~]# mysql --version
mysql Ver 14.14 Distrib 5.7.13, for Linux (i686) using EditLine wrapper
主要步驟如下:
首先停止MySQL
[[email protected]
/etc/init.d/mysqld: line 46: /usr/local/mysql: is a directory
/etc/init.d/mysqld: line 47: /usr/local/mysql/data: is a directory
Shutting down MySQL. SUCCESS!
查看MySQL的狀態:
[[email protected] ~]# /etc/init.d/mysqld status
/etc/init.d/mysqld: line 46: /usr/local/mysql: is a directory
/etc/init.d/mysqld: line 47: /usr/local/mysql/data: is a directory
ERROR! MySQL is not running
查看MySQL的進程:
[[email protected] ~]# ps aux|grep mysql|grep -v grep
使用--skip-grant-tables啟用MySQL忽略登入授權驗證
[[email protected] ~]# mysqld_safe --skip-grant-tables --user=mysql &
[1] 6559
[[email protected] ~]# 2017-07-30T14:23:38.600285Z mysqld_safe Logging to ‘/usr/local/mysql/data/mysqld.err‘.
2017-07-30T14:23:38.640326Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
無需密碼即可登入MySQL
[[email protected] ~]# mysql
重置root密碼
說明:新安裝的MySQL5.7,登錄時提示密碼錯誤,安裝的時候並沒有更改密碼,後來通過免密碼登錄的方式更改密碼,輸入update mysql.user set password=password(‘wtf123‘) where user=‘root‘ and host=‘localhost‘時提示ERROR 1054 (42S22): Unknown column ‘password‘ in ‘field list‘,原來是mysql5.7數據庫下已經沒有password這個字段了,password字段改成了authentication_string.
mysql> update mysql.user set authentication_string=password(‘wtf123‘) where user=‘root’and host=‘localhost‘;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
刷新:mysql> flush privileges;
退出:mysql> quit
說明:不能使用set password=password(‘wtf1234‘);
重啟服務再登入
[[email protected] ~]# /etc/init.d/mysqld restart
[[email protected] ~]# mysql -uroot -pwtf123
說明:查看數據庫密碼命令:
mysql> select user,host,authentication_string from mysql.user;
擴展:通過修改/etc/my.cnf 配置文件來重置mysql密碼
1.打開mysql的配置文件,命令:vim /etc/my.cnf 。在配置文件中新增一行 skip-grant-tables,結果如下圖所示:
2.保存並退出!
3.重啟mysqld,命令:service mysqld restart
4.無需密碼即可登入MySQL
[[email protected] ~]# mysql
mysql> update mysql.user set authentication_string=password(‘wtf123‘) where user=‘root’and host=‘localhost‘;
flush privileges; #刷新權限
退出:quit
5.退出後還原my.cnf重啟,命令如下:
vim /etc/my.cnf #打開mysql配置文件,將skip-grant-tables前面加#;
/etc/init.d/mysqld restart #重新啟動mysql;
用新密碼登入mysql數據庫,命令如下:
#mysql –uroot –p123456 即可正常登入數據庫了!
本文出自 “聖騎士控魔之手” 博客,請務必保留此出處http://wutengfei.blog.51cto.com/10942117/1952520
MySQL單實例重置密碼的兩種方法