MySQL 5.7.6+创建新用户修改密码及分配权限

时间:2022-03-04 01:06:57

在MySQL 5.7.6+中,以前的创建新用户,设置密码的语句已经被deprecated了,所以现在已经不推荐使用:

GRANT ALL PRIVILEGES ON mydb.* TO 'username'@'localhost' IDENTIFIED BY 'myPassword';

MySQL官方文档中提到

However, use of GRANT to create accounts or define nonprivilege
characteristics is deprecated as of MySQL 5.7.6. Instead, perform
these tasks using CREATE USER or ALTER USER.

所以,目前推荐使用CREATE USER来创建用户,ALTER USER来修改密码:

CREATE USER username IDENTIFIED BY 'myPassword';

MySQL会使用默认的插件来对myPassword加密。

若要修改用户密码,推荐的方式是使用ALTER USER而非旧的SET PASSWORD语法:

ALTER USER username IDENTIFIED BY 'myNewPassword';

以上两个语法可以参考:
https://dev.mysql.com/doc/refman/5.7/en/create-user.html
https://dev.mysql.com/doc/refman/5.7/en/set-password.html