Linux运维知识之linux MariaDB(MySQL)数据库更改用户权限
小标 2018-12-21 来源 : 阅读 1003 评论 0

摘要:本文主要向大家介绍Linux运维知识之linux MariaDB(MySQL)数据库更改用户权限了,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。

本文主要向大家介绍Linux运维知识之linux MariaDB(MySQL)数据库更改用户权限了,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。


平时维护MariaDB(MySQL)数据库服务器,难免会用到一些常用的命令,MariaDB数据库长时间不出问题,有些sql语句就会忘记,之前也没有记载,今天没事就记录下,也共享给大家一块看看,有不足之处还望谅解。


本文操作适用于MariaDB所有版本,适用于MySQL5.2以上版本

本文生产环境Centos7.3 64位 ,MariaDB server 10.2.5


MariaDB 赋予用户权限命令的简单格式可概括为:


grant 权限 on 数据库对象 to 用户;


# mysql -u root -p //登录数据库
 Enter password:
 MariaDB [(none)]> show databases; //查看当前数据库中所有数据库
 MariaDB [(none)]> create database renwole; //新建数据库名为“renwole”
 MariaDB [renwoleBD]> show tables; //显示某个数据库中的表文件
 MariaDB [(none)]> select version(),current_date; //查看数据库版本和当前日期
 MariaDB [(none)]> drop database renwole; //删除renwole数据库
 MariaDB [renwoleDB]> desc 表名称; //查看数据库表结构
 MariaDB [(none)]> show variables like '%dir%'; //查看数据库存储路径
 MariaDB [(none)]> show grants; 查看当前用户权限
 MariaDB [(none)]> show grants for root@'localhost'; //查看用户权限


下面就MariaDB数据库实例讲解,例如:


创建添加一个renwole用户,密码为renwole123


# mysql -u root -p //登录数据库
 Enter password:
 MariaDB [(none)]> insert into mysql.user(Host,User,Password) values("localhost","renwole",password("renwole123"));


新建一个renwoleDB数据库,并授权用户renwole拥有该数据库的所有权限。


MariaDB [(none)]> create database renwoleDB; //新建
 MariaDB [(none)]> grant all privileges on renwoleDB.* to renwole@% identified by 'renwole123'; //授权,关键字 “privileges” 可以省略。
 MariaDB [(none)]> flush privileges; //刷新用户权限


如果想给一个用户查询、插入、更新、删除数据库中的所有表数据权利,可以这样来写:


MariaDB [(none)]> grant select on on renwoleDB.* to renwole@’%’ //查询
 MariaDB [(none)]> grant insert on on renwoleDB.* to renwole@’%’ //插入
 MariaDB [(none)]> grant update on on renwoleDB.* to renwole@’%’ //更新
 MariaDB [(none)]> grant delete on on renwoleDB.* to renwole@’%’ //删除


或者,用一个语句命令替代:


MariaDB [(none)]> grant select,insert,delete,update on renwoleDB.* to renwole@’%’ identified by 'renwole123';


如果想查看所有Mysql用户的权限,代码如下;


MariaDB [(none)]> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;


如果想删除一个用户及权限,可以这样写;


MariaDB [(none)]> drop user renwole@localhost;


如果想修改一个用户密码;


MariaDB [(none)]> update mysql.user set password=password('New-password') where User="renwole" and Host="%";
 MariaDB [(none)]> flush privileges;


添加高级root用户整个mysql服务器权限


grant all on *.* to root@'%' identified by 'Password';


注意:【identified by】这个句子可以顺带设置密码,如果不指定该用户口令不变。


撤销已经赋予给MariaDB用户的权限。

revoke 跟 grant 的语法相似,只需要把关键字 “to” 换成 “from” 即可,例如:


grant all on *.* to renwole@%;
revoke all on *.* from renwole@%;


MariaDB数据库的 grant、revoke 用户权限注意事项;grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 “grant option“,例如:


MariaDB [(none)]> grant select on renwoleDB.* to renwole@% with grant option;


这个特性一般用不到。实际中,数据库权限最好由root用户来统一管理。


注意:有时候renwole@’%’授权任意主机连接的时候需要加单引号,但有时又不需要。

 

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注系统运维Linux频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程