系统:Centos 6.5 64位
Mysql:5.1.73
今天想做个数据库测试实验,居然忘记了Mysql数据的密码。于是使用安全模式启用。
[root@mail ~]# service mysqld stop #先停止数据库
Stopping mysqld: [ OK ]
[root@mail ~]# mysqld_safe --skip-grant-tables & #利用安全模式启用数据库
[1] 1547
[root@mail ~]# 150403 10:43:26 mysqld_safe Logging to ‘/var/log/mysqld.log‘.
150403 10:43:26 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
mysql -uroot #可以这样输入,也可以直接输入mysql回车,因为安全模式是root权限的。
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; #切换到mysql数据库
Database changed
mysql> UPDATE user SET Password=PASSWORD(‘root‘) where USER=‘root‘; #更新 root用户密码密码为root,这个根据自己改。
Query OK, 0 rows affected (0.01 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql> FLUSH PRIVILEGES; #强制刷新数据库记录。
Query OK, 0 rows affected (0.00 sec)
mysql> quit #退出
Bye
到这里我相信很多朋友都会直接重启数据库,我之前也一样,问题一样存在。因为我们刚才是用安全模式启用过Mysql,在进程还会存在一个mysqld_safe Number of processes running now: 0 必须要把这个安全模式的进程结束后再重启mysql即可登陆。
[root@mail ~]# killall -9 mysqld #结束进程里残留的Mysql进程。
[root@mail ~]# /usr/bin/mysqld_safe: line 158: 1634 Killed nohup /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --skip-grant-tables --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock < /dev/null >> /var/log/mysqld.log 2>&1
150403 10:46:47 mysqld_safe Number of processes running now: 0
150403 10:46:47 mysqld_safe mysqld restarted
[root@mail ~]# service mysqld start
Starting mysqld: [ OK ]
[root@mail ~]# mysql -uroot -p
输入刚才更改的密码即可进入数据库。
ERROR 1045 (28000): Access denied for user 'root'@'localhost' 的解决方法
原文地址:http://2305427.blog.51cto.com/2295427/1627937