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

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

一、拥有原来的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 &
[ 1 3117
[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 &
[ 1 3243
[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)  2000 2010 , 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)  2000 2010 , 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)  2000 2010 , 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)  2000 2010 , 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,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
存储 关系型数据库 MySQL
|
29天前
|
关系型数据库 MySQL 数据库
mysql中如何将默认用户名root改成其他?
mysql中如何将默认用户名root改成其他?
23 0
|
1月前
|
关系型数据库 MySQL 数据库
MySQL忘记root密码、远程无法连接的解决方法
MySQL忘记root密码、远程无法连接的解决方法
|
1月前
|
SQL 存储 关系型数据库
【MySQL 数据库】11、学习 MySQL 中的【锁】
【MySQL 数据库】11、学习 MySQL 中的【锁】
76 0
|
存储 关系型数据库 MySQL
|
1月前
|
SQL 关系型数据库 MySQL
【MySQL 数据库】4、MySQL 事务学习
【MySQL 数据库】4、MySQL 事务学习
44 0
|
10天前
|
SQL 存储 关系型数据库
6本值得推荐的MySQL学习书籍
本文是关于MySQL学习书籍的推荐,作者在DotNetGuide技术社区和微信公众号收到读者请求后,精选了6本值得阅读的MySQL书籍,包括《SQL学习指南(第3版)》、《MySQL是怎样使用的:快速入门MySQL》、《MySQL是怎样运行的:从根儿上理解MySQL》、《深入浅出MySQL:数据库开发、优化与管理维护(第3版)》以及《高性能MySQL(第4版)》和《MySQL技术内幕InnoDB存储引擎(第2版)》。此外,还有12本免费书籍的赠送活动,涵盖《SQL学习指南》、《MySQL是怎样使用的》等,赠书活动有效期至2024年4月9日。
|
16天前
|
SQL 关系型数据库 MySQL
轻松入门MySQL:深入学习数据库表管理,创建、修改、约束、建议与性能优化(3)
轻松入门MySQL:深入学习数据库表管理,创建、修改、约束、建议与性能优化(3)
|
2月前
|
SQL 关系型数据库 MySQL
MySQL技能完整学习列表10、数据导入和导出——1、数据导入(LOAD DATA, mysqldump)——2、数据导出(SELECT ... INTO OUTFILE, mysqldump)
MySQL技能完整学习列表10、数据导入和导出——1、数据导入(LOAD DATA, mysqldump)——2、数据导出(SELECT ... INTO OUTFILE, mysqldump)
48 0
|
存储 SQL 关系型数据库