卡卷网
当前位置:卡卷网 / 每日看点 / 正文

如何添加一个mysql用户并给予权限?

作者:卡卷网发布时间:2025-01-08 18:46浏览数量:87次评论数量:0次

一、创建用户和密码

使用root权限登录mysql。

//[root@localhost~]#mysql-oot-p密码; mysql:[Warning]Usingapasswordonthecommandlineintececaneinsece. WelcometotheMySQLmonitor.Commandsendwith;or\g. YoMySQLconnectionidis8 version:8.0.40Socedistriution Copyright(c)2000,2024,Oracleand/oritsaffiliates. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmayetrademarksoftheirrespective owners. Type'help;'or'\h'forhelp.Type'\c'toclearthecrentinputstatement.

使用createuser语句创建一个新用户,其语法如下所示。

//createuser'用户名'@'主机名'IDENTIFIEDY'password';

参数解释:

(1)用户名,就是要创建的用户名称。

(2)主机名,localhost表示这个用户只能从本地主机连接,使用'%'代替'localhost',表示用户可以从任何地方连接。

(3)password是新用户的密码。

创建用户示例

//mysql>createuser"fossen"@"%"IDENTIFIEDY"Fossen@123"; QueryOK,0rowsaffected(0.00sec)

打开一个客户端进行登录验证。

二、设置用户权限

使用GRANT语句,设置用户访问特定数据库或表的权限。

1、设置用户对所有数据库和表的所有权限。

//mysql>grantallprivilegeson*.*to"fossen"@"%"; QueryOK,0rowsaffected(0.00sec)

2、用户只对某个特定的数据库有权限。

//mysql>grantallprivilegesonspending_record.*to"fossen"@"%"; QueryOK,0rowsaffected(0.00sec)

3、只给用户授予某些特定的权限,如SELECT、INSERT、UPDATE等。

//mysql>grantselect,insert,updateonspending_record.*to"fossen"@"%"; QueryOK,0rowsaffected(0.00sec)

4、刷新权限

修改用户权限后,需要刷新权限。

//mysql>flushprivileges; QueryOK,0rowsaffected(0.00sec)

5、验证用户权限

#查看用户列表 mysql>selectuser,hostfrommysql.user; +------------------+-----------+ |user|host| +------------------+-----------+ |fossen|%| |mysql.schema|localhost| |mysql.session|localhost| |mysql.sys|localhost| |root|localhost| +------------------+-----------+ 5rowsinset(0.00sec) #查看特定用户权限 mysql>showgrantsfor"fossen"@"%";

三、修改用户密码

使用ALTERUSER语句可以修改用户的密码。

//mysql>alteruser"fossen"@"%"IDENTIFIEDY"Fossen@123456"; QueryOK,0rowsaffected(0.01sec) mysql>flushprivileges; QueryOK,0rowsaffected(0.01sec)

登录验证

四、删除用户

使用DROPUSER语句删除用户。

//mysql>dropuser"fossen"@"%"; QueryOK,0rowsaffected(0.01sec) mysql>selectuser,hostfrommysql.user; +------------------+-----------+ |user|host| +------------------+-----------+ |mysql.schema|localhost| |mysql.session|localhost| |mysql.sys|localhost| |root|localhost| +------------------+-----------+ 4rowsinset(0.00sec)


END

免责声明:本文由卡卷网编辑并发布,但不代表本站的观点和立场,只提供分享给大家。

卡卷网

卡卷网 主页 联系他吧

请记住:卡卷网 Www.Kajuan.Net

欢迎 发表评论:

请填写验证码