【MySQL运维】设置或修改用户密码的方法

TangLu MySQL 2020-02-08 6783 0

一、新增或修改root密码的方法

方法1:使用mysqladmin为root用户新增密码(适用于刚装好MySQL还没有设置密码的情况

mysqladmin -u root -p password -h localhost "newpassword"


方法2:使用mysqladmin为root用户修改密码

mysqladmin -u root -p oldpassword -h localhost password "newpassword"


方法3:修改mysql.user表

mysql > update mysql.user set authentication_string=PASSWORD('newpass') where user='root'and host='localhost';
flush privileges;


方法4:使用SQL语句password()函数修改密码(需要先登录MySQL客户端

mysql > SET password=password("newpassword")


方法5:密码丢失后通过配置文件增加--skip-grant-tables参数启动数据库后修改密码的方法

#方法1
mysql > UPDATE mysql.user SET password=password("newpassword") WHERE user='root' and host='localhost';
#方法2
mysql > update mysql.user set authentication_string=PASSWORD('newpass') where user='root'and host='localhost';

mysql > flush privileges;

注意事项:

1、执行密码修改语句后记得使用flush privileges语句来重读授权表

2、使用SQL语句修改密码记得指定where条件,否则全表都会受到影响


二、新增或修改普通用户密码的方法

方法1:使用root登录MySQL后修改mysql.user表

update mysql.user set authentication_string=PASSWORD('newpass') where user='user1' and host='%';
flush privileges;


方法2:使用root登录MySQL后使用grant语句修改密码

grant all privileges on *.* to user01@'%' identified by 'newpassword'
flush privileges;


方法3:普通用户自行修改密码

set password=password('newpassword')


方法4:使用alter语句修改密码,root和普通用户都可以使用该方法

alter user 'root'@'localhost' identified by 'newpassword' ;



评论