修改密码
简单点的,需改自己的密码,使用一下命令。
mysql> SET PASSWORD = PASSWORD('123456');
##或者
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('新密码');
修改其他用户的密码
mysql> UPDATE mysql.user SET password=PASSWORD('新密码') WHERE User='root';
mysql> FLUSH PRIVILEGES;
这是忘记root密码的情况,重置密码
第一步,停止mysql服务器运行,关闭mysqld,接下里重新启动mysqld,使用以下命令
mysqld --skip-grant-tables
添加这个参数可以让任何人不用密码就能连接mysql数据库,并且获得任何权限。因为这个是不安全的,可以添加一个参数--skip-networking阻止远程连接。即以下代码启动mysql服务器
mysqld --skip-grant-tables --skip-networking
PS. 如果出现
160803 15:50:21 [Warning] The syntax 'for replication startup options' is deprecated and will be removed in MySQL 6.0. Please use 'CHANGE MASTER' instead.
160803 15:50:21 [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!
160803 15:50:21 [ERROR] Aborting
160803 15:50:21 [Note] ./mysqld: Shutdown complete
加上参数--user=root
mysqld --skip-grant-tables --skip-networking --user=root
第二步,使用mysql客户端访问mysql服务器,接下来运行sql语句更改root的密码。
mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
mysql> FLUSH PRIVILEGES;
FLUSH语句是告诉服务器去重新加载表的权限到内存中,确保密码已经更改生效。
第三步,现在你已经可以使用root帐号和新密码去连接mysql服务器了。所以现在要停止mysql服务器,然后使用正常的参数启动mysql。例如,
mysqld --console
##或者
/etc/init.d/mysql start
只要不带参数--skip-grant-tables和--skip-networking就行了
本文介绍如何在忘记MySQL root密码的情况下重置密码。首先通过特定参数启动MySQL服务,然后使用SQL命令更新root用户的密码。
2969

被折叠的 条评论
为什么被折叠?



