重设 mysql 密码_重设MySQL root密码

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

首先停止MySQL服务:

# /etc/init.d/mysql stop

Stopping MySQL database server: mysqld.

接下来,使用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 --user=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.0.51a-24 (Debian)

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服务:

# /etc/init.d/mysql start

Starting MySQL database server: mysqld.

Checking for corrupt, not cleanly closed and upgrade needing tables..

现在,就可以使用新设置的root连接数据库了:

# mysql --user=root --pass=new-passwd-here

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 681

Server version: 5.0.51a-24 (Debian)

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

mysql> quit

Bye

本文以创作共用版权协议发布,转载本文要求遵循“署名-非商业性使用-相同方式共享3.0”的创作共用协议,并以链接形式指明本文地址。

文章的脚注信息由WordPress的wp-posturl插件自动生成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值