网站地图    收藏   

主页 > 后端 > PHP语言 >

重设MySQL root密码_自学php网

来源:自学PHP网    时间:2014-12-04 21:50 作者: 阅读:

[导读] 在安装MySQL时,随便设了一个密码,现在怎么也想不起来了,只好重设MySQL的root密码。 在网上搜到一篇文章:Resetting a forgotten MySQL root password,按照文章里的办法恢复成功。把步骤记录如...

在安装MySQL时,随便设了一个密码,现在怎么也想不起来了,只好重设MySQL的root密码。

在网上搜到一篇文章:Resetting a forgotten MySQL root password,按照文章里的办法恢复成功。把步骤记录如下。

首先停止MySQL服务:
# service mysqld stop

接下来,使用mysqld_safe命令在后台启动MySQL服务器,这里使用了–skip-grant-tables选项,允许不使用密码连接MySQL数据库:
# /usr/bin/mysqld_safe --skip-grant-tables &
[1] 5933
nohup: ignoring input and redirecting stderr to stdout
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[5972]: started

接下来,登录数据库,并更新root用户的密码:
# mysql -u root mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.56

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

mysql> update user set Password=PASSWORD('new-password-here') WHERE User='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

现在,把mysqld_safe切换到前台,并使用CTRL-C结束它的运行:
# fg
/usr/bin/mysqld_safe --skip-grant-tables
^C
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6005]: ended

然后重新启动MySQL服务:
# service mysqld start
Starting MySQL database server: mysqld.
Checking for corrupt, not cleanly closed and upgrade needing tables..

现在,就可以使用新设置的root连接数据库了:
# mysql -u root -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 681
Server version: 5.1.56

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

mysql> quit
Bye

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论