且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Mysql学习之--修改root用户口令

更新时间:2022-09-30 08:12:57

一、拥有原来的myql的root的密码
方法一:
在mysql系统外,使用mysqladmin
mysqladmin -u root -p password "test123"
Enter password: 【输入原来的密码】

方法二:
通过登录mysql系统,
mysql -uroot -p
Enter password: 【输入原来的密码】
mysql>use mysql;
mysql> update user set password=passworD("test") where user='root';
mysql> flush privileges;
mysql> exit;      

二、忘记原来的myql的root的密码:
     首先,你必须要有操作系统的root权限了。要是连系统的root权限都没有的话,先考虑root系统再走下面的步骤。
类似于安全模式登录系统,有人建议说是pkill mysql,但是我不建议哈。因为当你执行了这个命令后,会导致这样的状况:
/etc/init.d/mysqld status
mysqld dead but subsys locked
这样即使你是在安全模式下启动mysql都未必会有用的,所以一般是这样/etc/init.d/mysqld stop,如果你不幸先用了pkill,那么就start一下再stop咯。
mysqld_safe --skip-grant-tables &
&,表示在后台运行,不再后台运行的话,就再打开一个终端咯。
mysql
mysql> use mysql;
mysql> UPDATE user SET password=password("oracle") WHERE user='root';   
mysql> flush privileges;
mysql> exit;      

案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
[root@ogg ~]# mysqld_safe --skip-grant-tables &
[13117
[root@ogg ~]# 140924 14:03:30 mysqld_safe Logging to '/var/lib/mysql/ogg.err'.
140924 14:03:30 mysqld_safe A mysqld process already exists
[1]+  Exit 1                  mysqld_safe --skip-grant-tables
 
关闭mysql server:
[root@ogg ~]# service mysql stop
Shutting down MySQL..                                      [  OK  ]
 
[root@ogg ~]# mysqld_safe --skip-grant-tables &
[13243
[root@ogg ~]# 140924 14:03:48 mysqld_safe Logging to '/var/lib/mysql/ogg.err'.
140924 14:03:48 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
 
[root@ogg ~]# mysql
Welcome to the MySQL monitor.  Commands end with or \g.
Your MySQL connection id is 1
Server version: 5.1.47-log Source distribution
Copyright (c) 20002010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql
Database changed
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)
 
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
29 rows in set (0.00 sec)
 
mysql> update user set password=PASSWORD('oracle'where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 4  Changed: 3  Warnings: 0
 
mysql> flush privileges;
ERROR 1033 (HY000): Incorrect information in file: './mysql/tables_priv.frm'
 
mysql> exit
Bye
 
[root@ogg ~]# 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.47-log Source distribution
Copyright (c) 20002010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

错误信息:   

[root@ogg mysql]# mysqld_safe --skip-grant-tables &

[1] 3795

1
2
3
4
5
6
7
8
9
10
11
12
[root@ogg mysql]# 140924 14:15:09 mysqld_safe Logging to '/var/lib/mysql/ogg.err'.
140924 14:15:09 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@ogg mysql]# mysql
Welcome to the MySQL monitor.  Commands end with or \g.
Your MySQL connection id is 1
Server version: 5.1.47-log Source distribution
Copyright (c) 20002010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql
Database changed

mysql> flush privileges;

ERROR 1033 (HY000): Incorrect information in file: './mysql/tables_priv.frm'

mysql> exit

Bye

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@ogg mysql]# service mysql stop
Shutting down MySQL.....140924 14:15:44 mysqld_safe mysqld from pid file /var/lib/mysql/ogg.pid ended
                                                           [  OK  ]
[1]+  Done                    mysqld_safe --skip-grant-tables
 
[root@ogg mysql]# service mysql start
Starting MySQL..     
                                      [  OK  ]
                                       
正常连接:                                      
[root@ogg mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with or \g.
Your MySQL connection id is 1
Server version: 5.6.4-m7-log Source distribution
Copyright (c) 20002010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> use mysql;
Database changed
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>









本文转自 客居天涯 51CTO博客,原文链接:http://blog.51cto.com/tiany/1557738,如需转载请自行联系原作者