今天发现 WordPress 链接不上数据库,登陆 window server 服务器查看,全部服务均运行正常。mysql
使用 root 帐号登陆 mysql 数据库,结果提示密码不匹配。我忽然意识到,服务器可能遭受到 SQL注入 攻击了……sql
至于事故发生的缘由和以后所作的补救措施,之后有机会我会聊一聊的。这里我主要讲一下 mysql 用户密码的重置步骤。数据库
重置 root 密码
在忘记 root 密码的状况下,能够进入 mysql 的安全模式,重置 root 密码。安全
1. 中止 MySQL 服务
打开命令提示符窗口,输入 net stop mysql 关闭 MySQL 服务。服务器
C:\Users\Administrator>net stop mysql57 MySQL57 服务正在中止.. MySQL57 服务已成功中止。
↑ 服务名称不必定都是 mysql,好比个人就是 mysql57,57表明版本号为5.7函数
固然你也能够经过计算机管理面板关闭 MySQL 服务。post
2. 切换到 bin 目录
在命令提示符窗口中,经过 cd 命令切换到 mysql 安装目录下的 bin 目录。ui
C:\Users\Administrator>
cd C:\Program Files\MySQL\MySQL Server 5.7\bin C:\Program Files\MySQL\MySQL Server 5.7\bin>
↑ 默认安装目录为 C:\Program Files\MySQL\MySQL Server加密
3. 进入安全模式
在 bin 目录下输入 mysqld --skip-grant-tables ,跳过权限检查启动 mysql。spa
若是你配置了 my.ini 文件,则须要将其引入: mysqld --defaults-file="../my.ini" --skip-grant-tables
[mysqld] basedir = "C:\ProgramData\MySQL\MySQL Server 5.7" datadir = "C:\ProgramData\MySQL\MySQL Server 5.7\Data"
↑ 我在 my.ini 文件中指定了数据的存放路径,若是不引入配置文件,则会提示 No such file or directory 错误。
4. 重置帐户密码
打开另外一个命令提示符窗口(别关闭安全模式窗口),一样切换到 mysql \ bin 目录,输入 mysql 跳过权限验证链接数据库。
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql Server version: 5.7.16 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
↑ 也能够指定链接参数 mysql -u <用户名> -p <密码> -h <链接地址> -P <端口号> -D <数据库>
执行 update mysql.user set authentication_string="" where user="root"; 重置 root 用户的密码(5.7 以前为 password 字段)。
mysql> update mysql.user set authentication_string="" where user="root"; Query OK, 1 row affected (0.00 sec) mysql> select user,authentication_string from mysql.user\G *************************** 1. row *************************** user: root authentication_string: *************************** 2. row *************************** user: mysql.sys authentication_string: *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE 2 rows in set (0.00 sec)
↑ root 用户的 authentication_string 字段已经被清空了
5. 刷新权限表
执行 flush privileges; 命令刷新权限表,密码已经重置完成,输入 quit 退出。
mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) mysql> quit Bye
关闭全部命令提示符窗口,经过任务管理器结束 mysqld.exe 进程。重启 MySQL 服务,以后就能够直接登陆 root 帐号了。
修改 root 密码
出于安全考虑,root 密码不宜为空,因此须要在密码重置以后,再从新设置一个密码。
方法一:SET PASSWORD
- SET PASSWORD FOR "username"=PASSWORD("new password");
以 root 身份登陆 mysql,再使用 set password 命令修改密码:
mysql> set password for root@localhost = password("pswd"); Query OK, 0 rows affected, 1 warning (0.00 sec)
方法二:mysqladmin
- mysqladmin -u "username" -p password "new password"
执行该命名以后会提示输入原密码,输入正确后便可修改。
C:\Program Files\MySQL\MySQL Server 5.7\bin> mysqladmin -u root -p password pswd Enter password: **** mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
方法三:UPDATE TABLE
- UPDATE mysql.user SET authentication_string=PASSWORD("new password") WHERE user="username";
在重置 root 密码的同时,也能够设置默认密码。不过密码不能为明文,必须使用 password() 函数加密。
mysql> update mysql.user set authentication_string=password("pswd") where user="root"; Query OK, 1 row affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)