MySQL( Mariadb)用户权限管理

登录mysql

# mysql -u root -p

回车之后输入密码,不正确重新运行这个命令直到密码正确
成功登录的样子

Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 111
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

查看已有用户

MariaDB [mysql]>  use mysql;
MariaDB [mysql]>  select host,user from user;
+-----------+--------+
| host      | user   |
+-----------+--------+
| %         | Admi2 |
| 127.0.0.2 | root   |
| localhost | Admi2 |
| localhost | root   |
+-----------+--------+

user表中host列的值的意义
% 匹配所有主机(有的环境里这个不包含localhost)
localhost 不会被解析成IP地址,直接通过UNIXsocket连接
127.0.0.1 会通过TCP/IP协议连接,并且只能在本机访问;
::1 ::1就是兼容支持ipv6的,表示同ipv4的127.0.0.1

创建用户

MariaDB [mysql]> create user  Username   IDENTIFIED by 'Password'; 

创建密码为Password的新用户Username

删除用户

delete from user where user='Admin2';

想删除哪个就删除哪个,别把root删了就好

查看用户权限

查看当前用户的权限

MariaDB [mysql]> show grants;

默认的root权限如下“

+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'----' IDENTIFIED BY PASSWORD '*DE5----------------------' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'----' WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

查看某用户权限

MariaDB [mysql]> show grants for Admi2;

上面演示的是查看Admi2的%用户权限,如果想查看其它host的权限,在后面@host即可.如

MariaDB [mysql]> show grants for Admi2@localhost;

权限解释

ALL PRIVILEGES

表示所有权限

IDENTIFIED BY PASSWORD

表示登录密码

WITH GRANT OPTION

表示该用户可以将自己拥有的权限授权给别人

修改权限

刷新权限

MariaDB [mysql]> flush privileges;

不刷新的话需要重启计算机,新权限表才会生效,但谁又想浪费时间重启呢。

赋予权限

MariaDB [mysql]> grant select,insert on *.* to Admin@'localhost';

MariaDB [(none)]>  grant all on *.* to root@'%' IDENTIFIED by '123987456' WITH GRANT OPTION;

MariaDB [mysql]> flush privileges;

给Admin的添加对所有数据库的查询和插入的本地权限
给Root用户任意IP远程登录操作任何数据库的权利,并拥有创建新用户赋予权力的权利

GRANT命令说明:

ALL PRIVILEGES 是表示所有权限,你也可以使用select、update等权限。

ON 用来指定权限针对哪些库和表。

*.* 中前面的*号用来指定数据库名,后面的*号用来指定表名。

TO 表示将权限赋予某个用户。
 WITH GRANT OPTION  表示这个用户可以拥有创建同等权利的新用户的权利

回收权限

MariaDB [mysql]> revoke delete on *.* from 'Admin'@'localhost';
MariaDB [mysql]> flush privileges;

删除Admin的delete权限,其它类似权限为select,update,insert
或者一条命令搞定

MariaDB [mysql]> revoke select, insert, update, delete on *.* from 'Admin'@'localhost';
MariaDB [mysql]> flush privileges;

参考

1.https://www.cnblogs.com/Richardzhu/p/3318595.html
2.https://www.cnblogs.com/fslnet/p/3143344.html
3.https://mariadb.org/documentation/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值