MySQL单实例重置密码的两种方法
在工作学习中,我们有时会忘记数据库的密码,下面是MySQL单实例密码重置的步骤。
说明:
(1)[root@mysql1 ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)
(2)[root@mysql1 ~]# mysql --version
mysql Ver 14.14 Distrib 5.7.13, for Linux (i686) using EditLine wrapper
主要步骤如下:
首先停止MySQL
[root@mysql1 ~]# /etc/init.d/mysqld stop
/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的状态:
[root@mysql1 ~]# /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的进程:
[root@mysql1 ~]# ps aux|grep mysql|grep -v grep
使用--skip-grant-tables启用MySQL忽略登入授权验证
[root@mysql1 ~]# mysqld_safe --skip-grant-tables --user=mysql &
[1] 6559
[root@mysql1 ~]# 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
[root@mysql1 ~]# 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');
重启服务再登入
[root@mysql1 ~]# /etc/init.d/mysqld restart
[root@mysql1 ~]# 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
[root@mysql1 ~]# 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 �Curoot �Cp123456即可正常登入数据库了!