今天遇到个破问题:用了n久的mysql要新建数据库,竟然忘记了密码。 而这个问题居然也很常见!
要修改mysql的root密码,有两个先决条件:
有修改mysql配置文件的权限 有重启mysql服务的权限
先修改配置文件:
引用
# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
# disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
在 [mysqld]下增加 skip-grant-tables,即跳过权限验证。
然后登录mysql,修改root密码:
引用
# mysql
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 3
server version: 5.0.95 source distribution
copyright (c) 2000, 2011, 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('') 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> quit
bye
然后把刚才修改的配置文件再改回来,最后重启服务:
引用
# service mysqld restart
停止 mysql: [确定]
启动 mysql: [确定]
大功告成!
ps:更果断的办法:
引用
关闭mysqld
命令行执行 mysqld --skip-grant-tables & 无密码登陆!