免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1253 | 回复: 1
打印 上一主题 下一主题

MySQL修改root账号密码 [复制链接]

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2015-07-11 22:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-10 13:39 |只看该作者 |倒序浏览
MySQL数据库中如何修改root用户的密码呢?下面总结了修改root用户密码的一些方法

1: 使用set password语句修改
  1. mysql> select user();
  2. +----------------+
  3. | user()         |
  4. +----------------+
  5. | root@localhost |
  6. +----------------+
  7. 1 row in set (0.08 sec)

  8. mysql> set password=password('123456');
  9. Query OK, 0 rows affected (0.00 sec)

  10. mysql> flush privileges;
  11. Query OK, 0 rows affected (0.00 sec)

  12. mysql> exit
复制代码


2: 更新mysql数据库的user表
  1. mysql> use mysql;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A

  4. Database changed
  5. mysql> update user set password=PASSWORD('QwE123') where user='root';
  6. Query OK, 4 rows affected (0.03 sec)
  7. Rows matched: 4  Changed: 4  Warnings: 0

  8. mysql> flush privileges;
  9. Query OK, 0 rows affected (0.00 sec)

  10. mysql> quit
复制代码

3:使用mysqladmin命令修改

命令一般为 mysqladmin -u root -p'oldpassword' password newpass 如下所示:
  1. [root@DB-Server ~]# mysqladmin -u root  -p'123456' password 'Qwe123'
  2. Warning: Using a password on the command line interface can be insecure.
复制代码
验证root密码修改是否成功
  1. [root@DB-Server ~]# mysql -u root -p'Qwe123' -e 'show databases';
  2. Warning: Using a password on the command line interface can be insecure.
  3. +--------------------+
  4. | Database           |
  5. +--------------------+
  6. | information_schema |
  7. | mysql              |
  8. | performance_schema |
  9. | test               |
  10. +--------------------+
复制代码
上面都是在知道root密码的情况下修改root密码,如果忘记了root密码,如何修改root的密码呢?

1:首先停掉MySQL服务
  1. [root@DB-Server ~]# service mysql stop
  2. Shutting down MySQL..[  OK  ]
  3. [root@DB-Server ~]#



  4. [root@DB-Server ~]# /etc/rc.d/init.d/mysql stop
  5. Shutting down MySQL..[  OK  ]
复制代码
2:然后使用mysqld_safe命令启动mysql,更新root账号的密码

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

    --skip-networking :跳过TCP/IP协议,只在本机访问(这个选项不是必须的。可以不用)是可以不用
  1. [root@DB-Server ~]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
  2. [1] 5145
  3. You have new mail in /var/spool/mail/root
  4. [root@DB-Server ~]# 150709 14:10:53 mysqld_safe Logging to '/var/lib/mysql/DB-Server.localdomain.err'.
  5. 150709 14:10:53 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

  6. [root@DB-Server ~]# mysql -u root  mysql
  7. Reading table information for completion of table and column names
  8. You can turn off this feature to get a quicker startup with -A

  9. Welcome to the MySQL monitor.  Commands end with ; or \g.
  10. Your MySQL connection id is 1
  11. Server version: 5.6.20-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)

  12. Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

  13. Oracle is a registered trademark of Oracle Corporation and/or its
  14. affiliates. Other names may be trademarks of their respective
  15. owners.

  16. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  17. mysql> show databases;
  18. +--------------------+
  19. | Database           |
  20. +--------------------+
  21. | information_schema |
  22. | mysql              |
  23. | performance_schema |
  24. | test               |
  25. +--------------------+
  26. 4 rows in set (0.00 sec)

  27. mysql> use mysql
  28. Database changed
  29. mysql> UPDATE user SET password=PASSWORD("Qwe123") WHERE user='root';
  30. Query OK, 4 rows affected (0.01 sec)
  31. Rows matched: 4  Changed: 4  Warnings: 0

  32. mysql> flush privileges;
  33. Query OK, 0 rows affected (0.00 sec)

  34. mysql> exit
  35. Bye
复制代码
3:重新启动MySQL服务。


作者:潇湘隐者
出处:http://www.cnblogs.com/kerrycode/

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
2 [报告]
发表于 2015-07-23 11:07 |只看该作者
条条大路通罗马,方法很多种。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP