Mysql Access denied for user root @localhost 错误解决方案(亲测成功)

2017-03-23

网上复制党太多,可能本来一个正确的解决方案在粘来粘去的过程中也变得不那么正确。

本站长也遇到了这个问题,从网上找了两篇文档结合使用,亲测成功。

我的操作系统是Win7, Mysql版本是5.7。

以下是摘自互联网:

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

C:\Documents and Settings\MMC>d:

D:\>cd D:\SERVER\MySQL5122\bin

D:\SERVER\MySQL5122\bin>mysqld --defaults-file="D:\SERVER\MySQL5122\my.ini" --console --skip-grant-tables
090801 4:43:54 InnoDB: Started; log sequence number 0 585546
090801 4:43:54 [ERROR] Can't open and lock privilege tables: Table 'mysql.servers' doesn't exist
090801 4:43:54 [Note] mysqld: ready for connections.
Version: '5.1.22-rc-community' socket: '' port: 3306 MySQL Community Server (GPL)
_

只要出现下面信息,就说明 MySQL 已经起来了。

Version: '5.1.22-rc-community' socket: '' port: 3306 MySQL Community Server (GPL)

3. 不关闭此命令行窗口,重新打开一个命令行窗口,同样切到 “mysql\bin” 目录下,然后执行下面的粗体的命令:



C:\Documents and Settings\MMC>d:

D:\>cd D:\SERVER\MySQL5122\bin

D:\SERVER\MySQL5122\bin>mysql -u root mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.22-rc-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>


到这步,我想任何一个使用 MySQL 的人都知道后面要做什么了!(^a^)

mysql>
mysql> UPDATE user SET authentication_string=PASSWORD('mynewpass') where USER='root';
Query OK, 2 rows affected (0.05 sec)
Rows matched: 2 Changed: 2 Warnings: 0

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)

mysql> quit
Bye

4. 好了到此步,可以关闭第一个 DOS 窗口了。打开系统服务控制窗口(控制面板--管理工具--服务),启动 MySQL 服务。

5. 在剩下的第二个 DOS 窗口中,用新的 ROOT 密码连接 MySQL。

D:\SERVER\MySQL5122\bin>mysql -u root -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.22-rc-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> quit
Bye

至此,系统恢复 ROOT 用户管理权限完成。

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

到这里似乎成功了,但我的mysql还是无法连接,提示的错误变了“Mysql 5.7 your password has expired ...

然后在DOS窗口下,执行下面操作


C:\Documents and Settings\MMC>d:

D:\>cd D:\SERVER\MySQL5122\bin

D:\SERVER\MySQL5122\bin>mysql -u root mysql


alter user 'root'@'localhost' identified by '密码';

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

退出dos窗口,连接数据库成功 ^_^