MySQL修改root账号密码的方法

作者:潇湘隐者 时间:2024-01-28 07:25:46 

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

MySQL修改root账号密码的方法

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

MySQL修改root账号密码的方法

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

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

标签:MySQL,修改密码
0
投稿

猜你喜欢

  • 初步认识Python中的列表与位运算符

    2022-06-28 18:10:51
  • 图文详解Python中最神秘的一个魔法函数

    2022-03-25 05:44:24
  • Python一行代码实现生成和读取二维码

    2023-04-30 15:31:18
  • 效率高的Javscript字符串替换函数的benchmark

    2024-04-22 12:47:39
  • mysql的case when字段为空,null的问题

    2024-01-13 14:40:59
  • ASP Session对象的集合以及属性方法事件

    2009-02-02 09:26:00
  • python中数组和列表的简单实例

    2021-04-15 20:04:42
  • 管理员必读10个重要MySQL客户启动选项

    2008-06-07 16:57:00
  • 基于Oracle的面向对象技术入门基础简析开发者网络Oracle

    2010-07-18 12:57:00
  • 剖析SQL Server 事务日志的收缩和截断

    2009-01-15 13:04:00
  • python使用pyqt写带界面工具的示例代码

    2023-09-29 05:15:22
  • Python实现打印九九乘法表的不同方法总结

    2023-05-25 01:05:19
  • java连接mysql数据库乱码的解决方法

    2024-01-21 06:26:15
  • Python实现简单飞机大战

    2022-11-11 17:39:11
  • 关于vs2019安装不了扩展问题的解决方法

    2023-10-08 06:57:29
  • Python实现方便使用的级联进度信息实例

    2021-04-22 12:31:18
  • Python 正则表达式实现计算器功能

    2023-09-02 02:01:44
  • JavaScript自定义日历效果

    2024-04-28 09:33:55
  • 非常全面的Python常见基础面试题及答案

    2022-11-14 17:03:33
  • selenium+python实现基本自动化测试的示例代码

    2023-12-10 00:17:07
  • asp之家 网络编程 m.aspxhome.com