忘记了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