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服务。

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

相关文章

  • Mac下mysql 5.7.17 安装配置方法图文教程

    Mac下mysql 5.7.17 安装配置方法图文教程

    这篇文章主要为大家详细介绍了mysql 5.7.17 源码编译安装配置方法图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • MYSQL数据表损坏的原因分析和修复方法小结(推荐)

    MYSQL数据表损坏的原因分析和修复方法小结(推荐)

    MYSQL数据表损坏的原因分析和修复方法小结,碰到的朋友可以参考,下面整理一些比较全,希望对大家有所帮助。
    2011-01-01
  • 经典mysql连接查询例题

    经典mysql连接查询例题

    本文通过一个有趣的例子向我们介绍了mysql连接查询,采用了子查询和连接查询混合和多次连接查询,需要的朋友可以参考下
    2015-07-07
  • 使用distinct在mysql中查询多条不重复记录值的解决办法

    使用distinct在mysql中查询多条不重复记录值的解决办法

    使用distinct在mysql中查询多条不重复记录值的解决办法...
    2006-12-12
  • 实现mysql级联复制的方法示例

    实现mysql级联复制的方法示例

    这篇文章主要介绍了实现mysql级联复制的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • MySQL数据库优化与定期数据处理策略

    MySQL数据库优化与定期数据处理策略

    在当今数据驱动的时代,数据库作为信息存储和管理的核心,扮演着至关重要的角色,本文将探讨如何通过一系列有效的策略来优化 MySQL 数据库的查询效率,并实现定期处理数据的机制,以确保主表中的数据保持在合理范围内,需要的朋友可以参考下
    2025-03-03
  • MySQL用truncate命令快速清空一个数据库中的所有表

    MySQL用truncate命令快速清空一个数据库中的所有表

    这篇文章主要介绍了MySQL用truncate命令快速清空一个数据库中的所有表,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • MySL实现如等级成色等特殊顺序的排序详解

    MySL实现如等级成色等特殊顺序的排序详解

    这篇文章主要为大家介绍了MySL实现如等级成色等特殊顺序的排序详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • 2017最新版windows安装mysql教程

    2017最新版windows安装mysql教程

    这篇文章主要介绍了2017最新版windows安装mysql教程,本文图文并茂给大家介绍的非常详细,需要的朋友可以参考下
    2017-08-08
  • SQL查询语句优化的实用方法总结

    SQL查询语句优化的实用方法总结

    下面小编就为大家带来一篇SQL查询语句优化的实用方法总结。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12

最新评论