MySQL修改root账号密码的方法

 更新时间:2015年07月21日 10:46:15   作者:潇湘隐者  
这篇文章介绍了两种情况,知道root密码的情况下修改root密码,以及忘记了root密码,如何对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服务。

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

相关文章

  • 分享8个不得不说的MySQL陷阱

    分享8个不得不说的MySQL陷阱

    这篇文章给大家分享8个不得不说的MySQL陷阱,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2018-03-03
  • Mysql5.7忘记root密码怎么办(简单且有效方法)

    Mysql5.7忘记root密码怎么办(简单且有效方法)

    本文给大家分享一个快速且简单的方法来解决Mysql5.7忘记root密码问题,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-02-02
  • Navicat连接MySQL时出现的连接失败问题及解决

    Navicat连接MySQL时出现的连接失败问题及解决

    这篇文章主要介绍了Navicat连接MySQL时出现的连接失败问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • 如何实现mysql的远程连接

    如何实现mysql的远程连接

    这篇文章详细介绍了mysql如何实现远程连接,文中有详细的代码实例讲解,有一定的参考价值,需要的朋友可以参考阅读
    2023-04-04
  • MySQLMerge存储引擎

    MySQLMerge存储引擎

    MERGE存储引擎把一组MyISAM数据表当做一个逻辑单元来对待,让我们可以同时对他们进行查询。本文给大家介绍MySQLMerge存储引擎,需要的朋友参考下吧
    2016-03-03
  • MySQL执行状态的查看与分析

    MySQL执行状态的查看与分析

    今天小编就为大家分享一篇关于MySQL执行状态的查看与分析,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • MySQL中关于超键和主键及候选键的区别

    MySQL中关于超键和主键及候选键的区别

    这篇文章主要介绍了MySQL中关于超键和主键及候选键的区别说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • 利用pt-heartbeat监控MySQL的复制延迟详解

    利用pt-heartbeat监控MySQL的复制延迟详解

    这篇文章主要给大家介绍了利用pt-heartbeat监控MySQL的复制延迟的相关资料,文中详细介绍了pt-heartbeat、监控原理以及安装过程等的相关内容,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-06-06
  • MySQL单表ibd文件恢复方法详解

    MySQL单表ibd文件恢复方法详解

    随着innodb的普及,innobackup也成为了主流备份方式。物理备份对于新建slave,全库恢复的需求都能从容应对;但当面临单表数据误删,或者单表误drop的情况,如果使用物理全备进行恢复呢
    2012-11-11
  • centos 7系统下编译安装 mysql5.7教程

    centos 7系统下编译安装 mysql5.7教程

    因为Mysql5.7的更新特性还是非常多,所以这篇文章就给大家介绍以下在centos上面编译安装mysql5.7的教程。本文给大家介绍的步骤还是相对来说比较详细的,相信对大家具有一定的参考借鉴价值,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-11-11

最新评论