当前位置:   article > 正文

MySQL 如何修改密码

MySQL 如何修改密码

**

MySQL 如何修改 root 密码

**

一、如果 mysql 未设置 root 初始密码,可直接登录,修改密码。

mysql -u root -p

--- 连接权限数据库
mysql> use mysql;  
 
--- 低版本 mysql 5.x 
mysql> update user set password=password('123') where user='root' and host='localhost';  
 
--- 高版本 mysql 8.x 
mysql> update user set authentication_string='123' where user='root';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

--- 刷新权限:
mysql> flush privileges;  
Query OK, 0 rows affected (0.06 sec)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在这里插入图片描述

二、第二种方式:

最简单的方法就是借助第三方工具 Navicat for MySQL 或者 mysql-SQLyogUltimate 来修改,
方法如下:

1、登录 mysql 到指定库,如:登录到 testdb 库。

2、然后点击上方“用户”按钮。

3、选择要更改的用户名,然后点击上方的“编辑用户”按钮。

4、出现如图界面,输入新密码,并确认新密码,点击“保存”按钮即可。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

三、第三种方式:

1、方法1: 用 SET PASSWORD 命令

首先登录 MySQL( mysql -uroot -p*** )。

--- 低版本 mysql 5.x 

格式:mysql> set password for 用户名@localhost = password('新密码');  
 
例子:mysql> set password for root@localhost = password('123');  

--- 高版本 mysql 8.x 

格式:set password for root@localhost = '密码';

示例:
mysql> set password for root@localhost='123';
Query OK, 0 rows affected (0.06 sec)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

2、方法2:用 mysqladmin 命令( 未登录 mysql )

格式:mysqladmin -u用户名 -p旧密码 password 新密码  
 
例子:mysqladmin -uroot -p123456 password 123  
  • 1
  • 2
  • 3

实操示例:
在这里插入图片描述

3、方法3:用 UPDATE 直接编辑 user 表

首先登录 MySQL( mysql -uroot -p* )。**

--- 连接权限数据库
mysql> use mysql;  
 
--- 低版本 mysql 5.x 
mysql> update user set password=password('123') where user='root' and host='localhost';  
 
--- 高版本 mysql 8.x 
mysql> update user set authentication_string='123' where user='root';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

--- 刷新权限:
mysql> flush privileges;  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

实操示例:
在这里插入图片描述

4、方法4:在忘记 root 密码的时候,可以这样

以 windows 为例:

1)关闭正在运行的 MySQL 服务

WIN + R 打开运行,输入 services.msc 打开服务列表,打开 mysql 服务,关闭。
  • 1

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
2)打开 DOS 窗口,转到 mysql\bin 目录。
( 默认安装目录 : C:\Program Files\MySQL\MySQL Server 8.0\bin )

3)无密码登录 mysql (启动 MySQL 服务的时候跳过权限表认证)
WIN + R 打开运行,输入 CMD 打开控制台终端输入:

	--- 低版本 mysql 5.x : 
	
	C:\Users\Administrator>   mysqld --skip-grant-tables 回车。
	
	--- 高版本 mysql 8.x  输入:
	
	mysqld --defaults-file="C:\ProgramData\MySQL\MySQL Server 8.0\my.ini" --console --skip-grant-tables --shared-memory 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

4)再开一个 DOS 窗口(因为刚才那个DOS窗口已经不能动了),转到 mysql\bin 目录。

5) 输入 mysql 回车,如果成功,将出现 MySQL 提示符 >。

在这里插入图片描述

6) 连接权限数据库:

mysql>   use mysql;   
Database changed
  • 1
  • 2

7)登录后,更新置空 root 用户的校验字符串(可以理解为就是登陆密码)
并刷新权限:

	--- 低版本 mysq5.x 
	mysql>  update user set password=password("123") where user="root"; 
	(别忘了最后加分号) 。  
	mysql>   flush privileges;  

	--- 高版本 mysq8.x
	mysql>  update user set authentication_string='12311' where user='root';
	mysql>   flush privileges;  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

8)重置 root 登录密码为:123 ,并刷新权限:(必须步骤):

-- 重置密码为:123
mysql> alter user 'root'@'localhost' identified by '123';
Query OK, 0 rows affected (0.07 sec)

-- 刷新权限:
mysql> flush privileges;
Query OK, 0 rows affected (0.07 sec)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

9)退出 mysql :

mysql>   quit  或者:exit
  • 1

10)必要时注销或重启系统,再进入,使用用户名 root 和刚才设置的新密码 123 登录。

	 C:\Users\Administrator>mysql -uroot -p123
	mysql: [Warning] Using a password on the command line interface can be insecure.
	Welcome to the MySQL monitor.  Commands end with ; or \g.
	Your MySQL connection id is 26
	Server version: 8.0.23 MySQL Community Server - GPL
	
	Copyright (c) 2000, 2021, Oracle and/or its affiliates.
	
	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> exit
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/1016815
推荐阅读
相关标签
  

闽ICP备14008679号