[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 字段已经被清空了
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";