MySQL 修改账户名与密码

1,088 阅读1分钟

因为平常进数据库时都是记住密码,你们懂得!

Mac OS:

目前网上对于修改数据库用户名和密码的方法太多了,以下为我尝试且成功的一种:

NidhoggDJokingdeMacBook-Pro:~ joking$ cd /usr/local/mysql/bin/ 
NidhoggDJokingdeMacBook-Pro:bin joking$ ./mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('1234');
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> 

PS:根据 MySQL 版本的不同,执行命令:

最开始没注意有这个问题,还是补充一下。

MySQL 5.7.6 and later: 
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass'; 

MySQL 5.7.5 and earlier: 
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');

  这里是MySQL官方的文献资料!

Windows:

无论在那个环境下切记最先关闭数据库的服务!

DOS命令 net stop mysql

转到 mysqlbin 目录

可以通过打开mysql应用快速找到mysqlbin目录

复制路径然后在DOS里输入cd空格粘贴路径

输入mysqld --skip-grant-tables回车。

如果没有出现提示信息,那就对了再开一个DOS窗口(刚才那个DOS窗口已经不能动了),

转到 mysqlbin 目录输入mysql回车,如果成功,将出现MySQL提示符 >

输入FLUSH PRIVILEGES;(记得带分号)

输入SET PASSWORD FOR 'root'@'localhost' = PASSWORD('1234');

重启服务net start mysql


Mysql程序在D盘(其实总是忘如何返回上级目录):

C:\Users\Administrator>cd..

C:\Users>cd..

C:\>d:

D:\>

在进入mysql文件夹的bin目录