MySQL管理员用户设置和修改密码的方法

MySQL管理员用户设置和修改密码的方法

Posted by Sunfy on 2022-05-23
Words 384 and Reading Time 1 Minutes
Viewed Times
Viewed Times
Visitors In Total

1>. 为管理员root用户设置密码的方法

1
2
[root@Howden ~]# mysqladmin -uroot password '12345678'  #为没有密码的用户设置密码
[root@Howden ~]# mysqladmin -uroot password '12345678' -S /data/3306/mysql.sock #适合多实例mysql

2>. 修改密码的方法一:Shell命令行修改

1
2
[root@Howden ~]# mysqladmin -uroot -p123456 password '12345678'
[root@Howden ~]# mysqladmin -uroot -p123456 password '12345678' -S /data/3306/mysql.sock

3>. 修改密码的方法二:用SQL语句UPDATE修改(适合密码丢失后通过—skip-grant-tables参数启动数据库后修改密码)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
mysql> DESC mysql.user;  #查看user表结构,找到Password字段
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Password | char(41) | NO | | | |
.....

#此处省略39行。。。

mysql> SELECT user,host,password FROM mysql.user; #可以看到,mysql中存储的用户密码是加密的
+--------+-----------+-------------------------------------------+
| user | host | password |
+--------+-----------+-------------------------------------------+
| system | localhost | *E1CC002C309F98DEEE357E8B53BE83C4E2C451A0 |
+--------+-----------+-------------------------------------------+

1 row in set (0.00 sec)

mysql> UPDATE mysql.user SET password=password(12345678) WHERE user='system' AND host='localhost'; #用password函数将新密码加密,否则修改后的密码是明文的,无法登录

Query OK, 1 row affected (0.15 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> FLUSH PRIVILEGES; #需要刷新之后才生效,否则只存在于内存中,无法写入到数据文件中
Query OK, 0 rows affected (0.00 sec)

4>. 修改密码的方法三:用SQL语句SET修改(最省事,但只能修改当前登录用户的密码)

1
2
3
4
5
mysql> SET password=password('12345678');  #这里必须加引号
Query OK, 0 row affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

Copyright 2021 sunfy.top ALL Rights Reserved

...

...

00:00
00:00