MySQL 忘记 root 密码

/etc/my.cnf中加入一行skip-grant-tables,然后重新启动服务即可免密码登录root

[root@files-6w-03 ~]# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
[root@files-6w-03 ~]#
[root@files-6w-03 ~]# service mysqld stop
Stopping mysqld:                                           [  OK  ]
[root@files-6w-03 ~]#
[root@files-6w-03 ~]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
skip-grant-tables # uncomment when root password wrong

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[root@files-6w-03 ~]# service mysqld start
Starting mysqld:                                           [  OK  ]
[root@files-6w-03 ~]# mysql -u root
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>
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> select host,user,password from user;
+-------------+------+-------------------------------------------+
| host        | user | password                                  |
+-------------+------+-------------------------------------------+
| localhost   | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| files-6w-03 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| 127.0.0.1   | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| localhost   |      | *0CF80525DFB24601057936B5F4796894C5DF4EA2 |
| files-6w-03 |      | *0CF80525DFB24601057936B5F4796894C5DF4EA2 |
| %           | nms4 | *D49537A81886516A0F02ACADE299FB54AF7B2A79 |
+-------------+------+-------------------------------------------+
6 rows in set (0.00 sec)

mysql> update mysql.user set password=password('root') where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 3  Changed: 0  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@files-6w-03 ~]#
[root@files-6w-03 ~]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#skip-grant-tables # uncomment when root password wrong

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[root@files-6w-03 ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@files-6w-03 ~]# mysql -u root -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>
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

mysql> drop database test;
Query OK, 1 row affected (0.11 sec)

mysql> create database arn_iomc3;
Query OK, 1 row affected (0.02 sec)

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

mysql> select host,user,password from user;
+-------------+------+-------------------------------------------+
| host        | user | password                                  |
+-------------+------+-------------------------------------------+
| localhost   | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| files-6w-03 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| 127.0.0.1   | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| localhost   |      | *0CF80525DFB24601057936B5F4796894C5DF4EA2 |
| files-6w-03 |      | *0CF80525DFB24601057936B5F4796894C5DF4EA2 |
| %           | qige | *D49537A81886516A0F02ACADE299FB54AF7B2A79 |
+-------------+------+-------------------------------------------+
6 rows in set (0.00 sec)

Database changed
mysql> update user set user='iomc3' where user='qige';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update user set password=password('iomc3') where user='iomc3';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select host,user,password from user;
+-------------+---------+-------------------------------------------+
| host        | user    | password                                  |
+-------------+---------+-------------------------------------------+
| localhost   | root    | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| files-6w-03 | root    | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| 127.0.0.1   | root    | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| localhost   |         | *0CF80525DFB24601057936B5F4796894C5DF4EA2 |
| files-6w-03 |         | *0CF80525DFB24601057936B5F4796894C5DF4EA2 |
| %           | iomc3   | *463C3AB29EF31B0C97B487DDC59FB82288C9EF68 |
+-------------+---------+-------------------------------------------+
6 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> grant all on arn_iomc3.* to iomc3rw@'%' identified by 'iomc3passwd';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> quit
Bye
[root@files-6w-03 ~]#

results matching ""

    No results matching ""