关于一次忘记mysql root账号密码的经历

忘记了mysql的root密码,当幸好我有linux服务器的root权限,所以可以这么处理:

1.首先我们要使用root权限停止mysql:

[dev@LAMP1 ~]$ /etc/init.d/mysqld stop
停止 mysqld: [失败]
[dev@LAMP1 ~]$ sudo su
[sudo] password for dev: 
[root@LAMP1 dev]# /etc/init.d/mysqld stop
停止 mysqld: [确定]
[root@LAMP1 dev]# 

2.然后我们以非授权方式启动mysql:

[root@LAMP1 dev]# /etc/init.d/mysqld --skip-grant-tables
用法:/etc/init.d/mysqld {start|stop|status|restart|condrestart|try-restart|reload|force-reload}
[root@LAMP1 dev]# /usr/bin/mysqld_safe --skip-grant-table &
[1] 596
[root@LAMP1 dev]# 150424 14:43:48 mysqld_safe Logging to '/var/log/mysqld.log'.
150424 14:43:48 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

然后我们就可以用默认账户密码登陆mysql了:

[dev@LAMP1 ~]$ mysql -uroot -hlocalhost -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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> 

默认的账户密码是root,空。

3.然后我们就可以修改mysql的root密码了:

[dev@LAMP1 ~]$ mysql -uroot -p -hlocalhost
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=password('root') 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

4.然后我们退出之后,就把所有mysql进程关掉:

[dev@LAMP1 ~]$ ps -ef|grep mysql
root       407 32695  0 13:31 pts/1    00:00:00 mysql -uroot -hlocalhost -p
root       596   497  0 14:43 pts/2    00:00:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-table
mysql      683   596  0 14:43 pts/2    00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --skip-grant-table --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
dev        705   577  0 14:50 pts/3    00:00:00 grep mysql
[dev@LAMP1 ~]$ kill -9 32695
-bash: kill: (32695) - 不允许的操作
[dev@LAMP1 ~]$ sudo su
[sudo] password for dev: 
[root@LAMP1 dev]# kill -9 32695
[root@LAMP1 dev]# ps -ef|grep mysql
root       407     1  0 13:31 pts/1    00:00:00 mysql -uroot -hlocalhost -p
root       596   497  0 14:43 pts/2    00:00:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-table
mysql      683   596  0 14:43 pts/2    00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --skip-grant-table --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root       720   711  0 14:50 pts/3    00:00:00 grep mysql
[root@LAMP1 dev]# kill -9 596
[root@LAMP1 dev]# ps -ef|grep mysql
root       407     1  0 13:31 pts/1    00:00:00 mysql -uroot -hlocalhost -p
mysql      683     1  0 14:43 pts/2    00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --skip-grant-table --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root       722   711  0 14:51 pts/3    00:00:00 grep mysql
[root@LAMP1 dev]# kill 683
[root@LAMP1 dev]# ps -ef|grep mysql
root       407     1  0 13:31 pts/1    00:00:00 mysql -uroot -hlocalhost -p
root       725   711  0 14:51 pts/3    00:00:00 grep mysql
[root@LAMP1 dev]# 

5.然后我们重新启动mysql:

[root@LAMP1 dev]# /etc/init.d/mysqld start
正在启动 mysqld: [确定]
[root@LAMP1 dev]# 

6.然后我们使用刚刚我们设置的root密码重新登陆就好了:

[root@LAMP1 dev]# mysql -uroot -hlocalhost -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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> 

参考:
http://blog.sina.com.cn/s/blog_4e11d20b01000aqw.html
http://www.centoscn.com/CentOS/Intermediate/2013/0817/1334.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值