在使用MySQL的过程中经常会因为忘记MySQL密码而苦恼,本文提供集中常见的修改MySQL密码的方法,如有不对或疏漏,还望路过的大神指出。。。
MySQL常用的登录命令
mysql -h127.0.0.1 -utest -ptest -P3306
相信火星人都能看懂,不解释了啊。
查看密码密文
select password('abc');
SET PASSWORD
[root@xianbin ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1206
...
mysql> SET PASSWORD FOR 'root'@'%' = PASSWORD('new_passwd');
Query OK, 0 rows affected (0.28 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.21 sec)
mysql>
使用mysqladmin
若MySQL默认没有密码是采用下面的命令可以为MySQL初始化密码:
[root@xianbin ~]# mysqladmin -uroot password 123456
当MySQL已经有密码时,采用以下方法为MySQL初始化密码:
[root@xianbin ~]# mysqladmin -uroot -p123456 password '012345'
[root@xianbin ~]# mysql -uroot -p012345
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1218
...
mysql>
update mysql user表
下面的方法直接通过登录MySQL数据库修改mysql.user表来达到更新MySQL密码的目的,修改后MySQL的密码为’root_pwd’:
[root@xianbin ~]# mysql -uroot -p012345
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1221
...
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_pwd') WHERE user = 'root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>
skip-grant-tables方法
通过在my.cnf文件的[mysqld]下配置skip-grant-tables后重启MySQL或通过下述方法启动MySQL进程,然后在新窗口中采用update mysql user表修改密码(同上)。
[root@xianbin ~]# mysqld_safe --skip-grant-tables&
[1] 32255
[root@xianbin ~]# 150905 01:41:42 mysqld_safe Logging to '/var/log/mysqld.log'.
150905 01:41:43 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql