MySQL修改root账号密码的方法

MysqL数据库中如何修改root用户的密码呢?下面总结了修改root用户密码的一些方法

1: 使用set password语句修改

MysqL> select user();
+----------------+
| user()  |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.08 sec)
 
MysqL> set password=password('123456');
Query OK,0 rows affected (0.00 sec)
 
MysqL> flush privileges;
Query OK,0 rows affected (0.00 sec)
 
MysqL> exit

2: 更新MysqL数据库的user表

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('QwE123') where user='root';
Query OK,4 rows affected (0.03 sec)
Rows matched: 4 Changed: 4 Warnings: 0
 
MysqL> flush privileges;
Query OK,0 rows affected (0.00 sec)
 
MysqL> quit

3:使用MysqLadmin命令修改

命令一般为 MysqLadmin -u root -p'oldpassword' password newpass 如下所示:

[root@DB-Server ~]# MysqLadmin -u root -p'123456' password 'Qwe123'
Warning: Using a password on the command line interface can be insecure.

验证root密码修改是否成功

[root@DB-Server ~]# MysqLadmin -u root -p'123456' password 'Qwe123'
Warning: Using a password on the command line interface can be insecure.


 

上面都是在知道root密码的情况下修改root密码,如果忘记了root密码,如何修改root的密码呢?

1:首先停掉MysqL服务

[root@DB-Server ~]# service MysqL stop
Shutting down MysqL..[ OK ]
[root@DB-Server ~]# 


[root@DB-Server ~]# /etc/rc.d/init.d/MysqL stop
Shutting down MysqL..[ OK ]


2:然后使用MysqLd_safe命令启动MysqL,更新root账号的密码

    --skip-grant-tables:不启动grant-tables(授权表),跳过权限控制。

    --skip-networking :跳过TCP/IP协议,只在本机访问(这个选项不是必须的。可以不用)是可以不用

[root@DB-Server ~]# MysqLd_safe --user=MysqL --skip-grant-tables --skip-networking &
[1] 5145
You have new mail in /var/spool/mail/root
[root@DB-Server ~]# 150709 14:10:53 MysqLd_safe Logging to '/var/lib/MysqL/DB-Server.localdomain.err'.
150709 14:10:53 MysqLd_safe Starting MysqLd daemon with databases from /var/lib/MysqL
 
[root@DB-Server ~]# MysqL -u root MysqL
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Welcome to the MysqL monitor. Commands end with ; or \g.
Your MysqL connection id is 1
Server version: 5.6.20-enterprise-commercial-advanced MysqL Enterprise Server - Advanced Edition (Commercial)
 
copyright (c) 2000,2014,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> show databases;
+--------------------+
| Database  |
+--------------------+
| information_schema |
| MysqL  |
| performance_schema |
| test  |
+--------------------+
4 rows in set (0.00 sec)
 
MysqL> use MysqL
Database changed
MysqL> UPDATE user SET password=PASSWORD("Qwe123") WHERE user='root'; 
Query OK,4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
 
MysqL> flush privileges;
Query OK,0 rows affected (0.00 sec)
 
MysqL> exit
Bye

3:重新启动MysqL服务。

以上所述就是本文的全部内容了,希望大家能够喜欢

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...