欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

MySQL忘记root密码的解决方法_MySQL

程序员文章站 2024-02-05 08:44:04
...
bitsCN.com
MySQL忘记root密码的解决方法 Linux下的解决方法: 一. MySQL密码的恢复方法之一 如果忘记了MySQL的root密码,可以用以下方法重新设置: 1. KILL掉系统里的MySQL进程; killall -TERM mysqld 2. 用以下命令启动MySQL,以不检查权限的方式启动; safe_mysqld --skip-grant-tables & 3. 然后用空密码方式使用root用户登录 MySQL; mysql -u root 4. 修改root用户的密码; mysql> update mysql.user set password=PASSWORD('新密码') where User='root'; mysql> flush privileges; mysql> quit 重新启动MySQL,就可以使用新密码登录了。 二. MySQL密码的恢复方法二 有可能你的系统没有 safe_mysqld 程序(比如我现在用的 ubuntu操作系统, apt-get安装的mysql) , 下面方法可以恢复 1. 停止mysqld; /etc/init.d/mysql stop (您可能有其它的方法,总之停止mysqld的运行就可以了) 2. 用以下命令启动MySQL,以不检查权限的方式启动; mysqld --skip-grant-tables & 3. 然后用空密码方式使用root用户登录 MySQL; mysql -u root 4. 修改root用户的密码; mysql> update mysql.user set password=PASSWORD('newpassword') where User='root'; mysql> flush privileges; mysql> quit 重新启动MySQL /etc/init.d/mysql restart 就可以使用新密码 newpassword 登录了。 Windows下的解决方法: 1、编辑MySQL的配置文件:my.ini 一般在MySQL安装目录下有my.ini即MySQL的配置文件。 在此配置文件的最后添加如下一行: skip-grant-tables 保存退出编辑。 2、然后重启MySQL服务 在命令行下执行: net stop MySQL net start MySQL 3、设置新的ROOT密码 然后再在命令行下执行: MySQL -u root -p MySQL或mysql -u root -p 直接回车无需密码即可进入数据库了。 此时,在命令行下执行 use mysql; 现在我们执行如下语句把root密码更新为: update user set password=password("root") where user="root"; (注意:此时不用使用mysqladmin -u root -p password '你的新密码'这条命令修改密码,因为'skip-grant-tables'配置, 不信的话,你可以试用一下,它肯定会报如下所示的错误: F:/Documents and Settings/long>mysqladmin -u root -p password 'root' Enter password: Warning: single quotes were not trimmed from the password by your command line client, as you might have expected. mysqladmin: You cannot use 'password' command as mysqld runs with grant tables disabled (was started with --skip-grant-tables). Use: "mysqladmin flush-privileges password '*'" instead) exit 退出MySQL。 4、还原配置文件并重启服务 然后修改MySQL配置文件把刚才添加的那一行'skip-grant-tables'删除。 再次重起MySQL服务,密码修改完毕。 用新密码root试一下吧,又能登入重新进入mysql了. bitsCN.com
相关标签: 密码 Linux