1. 创建和删除用户,mysql中的用户是由用户名和主机名来确定的

create user "user_name@host_name" identified by passwd;
drop user user_name;
---------------也可以直接向mysql.user表添加记录----------------
insert into mysql.user(host,user,password) values ("host_name","user_name",password("pwd"));
flush privileges;//使用这种方法必须添加用户,必须刷新权限加载到内存中

2.查看用户权限

show grants for user_name@host_name

3.修改用户密码

set password for user_name@'host_name'=password('...') or
update mysql.user set password=password('...') where user='user_name';
flush privileges;
---------------修改ROOT用户密码也可以这样----------------------
mysqladmin -u root -p password 123456

4.查看所有用户

select host,user,password from mysql.user

5.授予权限

使用GRANT命令,其语法如下:

GRANT
priv_type [(column_list)]
[, priv_type [(column_list)]] ...
ON [object_type] priv_level
TO user_specification [, user_specification] ...
[REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]
[WITH with_option ...] GRANT PROXY ON user_specification
TO user_specification [, user_specification] ...
[WITH GRANT OPTION]
grant select,update,delete on test.* to user_name@host_name

MySQL数据库权限粒度可以分为全局、数据库、表、列、程序。

grant select,update on *.* to user_name@host_name //全局粒度
grant select,update on test.* to user_name@host_name //数据库级粒度
grant select,update on test.tab to user_name@host_name //表级粒度
grant select(name) on test.tab to user_name@host_name //列级粒度

6.回收权限

使用REVOKE命令,其语法如下

REVOKE
priv_type [(column_list)]
[, priv_type [(column_list)]] ...
ON [object_type] priv_level
FROM user [, user] ... REVOKE ALL PRIVILEGES, GRANT OPTION
FROM user [, user] ... REVOKE PROXY ON user
FROM user [, user] ...

在回收用户权限的时候,一定要对上GRANT时的权限粒度,否则回收权限不会成功,例如。

mysql> show grants for zhumuxian@localhost;
+------------------------------------------------------------------------------------------------------------------+
| Grants for zhumuxian@localhost |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'zhumuxian'@'localhost' IDENTIFIED BY PASSWORD '*00A51F3F48415C7D4E8908980D443C29C69B60C9' |
| GRANT SELECT, UPDATE ON `mysql`.* TO 'zhumuxian'@'localhost' |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

这时使用不是数据库级别粒度回收是不会成功,接着看

mysql> revoke all privileges on *.* from zhumuxian@localhost;
Query OK, 0 rows affected (0.00 sec) mysql> show grants for zhumuxian@localhost;
+------------------------------------------------------------------------------------------------------------------+
| Grants for zhumuxian@localhost |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'zhumuxian'@'localhost' IDENTIFIED BY PASSWORD '*00A51F3F48415C7D4E8908980D443C29C69B60C9' |
| GRANT SELECT, UPDATE ON `mysql`.* TO 'zhumuxian'@'localhost' |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

再使用相同粒度的回收就可以了。

mysql> revoke select,update on mysql.* from zhumuxian@localhost;
Query OK, 0 rows affected (0.00 sec) mysql> show grants for zhumuxian@localhost;
+------------------------------------------------------------------------------------------------------------------+
| Grants for zhumuxian@localhost |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'zhumuxian'@'localhost' IDENTIFIED BY PASSWORD '*00A51F3F48415C7D4E8908980D443C29C69B60C9' |
+------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

如果你想回收某个用户所有的权限,不管什么权限级别,粒度,直接使用下面的语句即可。

revoke all privileges,grant option from zhumuxian@localhost

 帐号安全管理

1.在Linux平台会有一个历史文件,它会记录mysql数据库的所有操作。

tail -20 ~/.mysql_history

这里最好使用/dev/null作为.mysql_history的软链接,这样所有的操作都被输出为空

$ ln -f -s /dev/null ~/.mysql_history

2.管理员口令丢失的处理

1.启动MySQL服务时加上--init-file,使其执行含有密码重置的脚本

首先停止Mysql服务,编写一个脚本updatePwd.txt,内容:

set password for root@localhost=password("xxxx");

进入mysql_safe模式,使用mysqld命令执行:

mysqld  --init-file="./updatePwd.txt"
//执行完这条语句,再关闭mysqld进程,停止mysql服务,接着以正常方式启动mysql即可

2.先停止mysql服务,然后以以下语句跳过权限验证:

mysqld --skip-grant-tables --skip-networking

然后使用以下语句登录mysql并修改root密码:

mysql -u root -p //此时密码为空
update mysql.user set password=password("xxxx") where user="root"
flush privileges;
--------然后重新启动mysql即可---------------

MySQL学习笔记二:权限管理的更多相关文章

  1. mysql学习笔记二 —— 权限体系

    要点: 1.MySQL的API2.MySQL的相关文件3.MySQL的权限体系 1.MySQL的API 应用程序接口 (application program interface) 1.1 命令行中的 ...

  2. MongoDB 学习笔记之 权限管理基础

    权限管理基础 MongoDB有很多用户roles,这里只是简单列举下命令的使用,具体的role的含义,请查阅官方文档. https://docs.mongodb.com/manual/referenc ...

  3. sql分类及基本sql操作,校对规则(mysql学习笔记二)

    sql针对操作对象分为不同语言 数据操作(管理)语言 DML或者将其细分为 ( 查询  DQL 管理(增,删,改)  DML) 数据定义语言(对保存数据的格式进行定义) DDL 数据库控制语言(针对数 ...

  4. MySQL学习笔记(二):MySQL数据类型汇总及选择参考

    本文主要介绍了MySQL 的常用数据类型,以及实际应用时如何选择合适的类型.  ******几个通用的简单原则:******* 1. 更小的通常更好.但是要确保没有低估需要存储的值的范围,如果无法确定 ...

  5. MySQL学习笔记(二)

    连接与断开服务器 应该以下面的方式连接MySQL服务器,而不是将密码以明文方式输入连接. C:\> mysql -h host -u user -pEnter password: ******* ...

  6. MySQL学习笔记二

    Ø function 函数 函数的作用比较大,一般多用在select查询语句和where条件语句之后.按照函数返回的结果, 可以分为:多行函数和单行函数:所谓的单行函数就是将每条数据进行独立的计算,然 ...

  7. mysql学习(用户权限管理)

    1. 添加数据库用户 create user 'username'@'host' identified by 'password'; 提示: 如果想让该用户可以从其他主机登陆,host可以设置为'%' ...

  8. MySQL学习笔记(二)—查询

    一.多表连接查询 新建两张表t_user.t_order.              1.内连接      返回满足条件的所有记录. (1)显式内连接      使用inner join关键字,在on ...

  9. MySQL 学习笔记 二

    Ø function 函数 函数的作用比较大,一般多用在select查询语句和where条件语句之后.按照函数返回的结果, 可以分为:多行函数和单行函数:所谓的单行函数就是将每条数据进行独立的计算,然 ...

随机推荐

  1. Eclipse 双击变量,其他相同变量有底色

    转载:http://blog.csdn.net/majian_1987/article/details/46691697 方便下次查找. 在Eclipse中,鼠标选中或者光标移动到Java类的变量名时 ...

  2. Code of Conduct

    v

  3. replace实现正则过滤替换非法字符

    html+js结构如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...

  4. go中安装Beego不成功笔记

    在go.Git等等之类的相关软件安装好了之后,便可以安装Beego. 1.开始的时候是由于网络限制,不能访问Github,这个还好说找VPNFQ... 2.之后的问题,在git Bash中输入go g ...

  5. Swift 3 and OpenGL on Linux and macOS with GLFW

    https://solarianprogrammer.com/2016/11/19/swift-opengl-linux-macos-glfw/ Swift 3 and OpenGL on Linux ...

  6. select2的相关问题

    在弹出框中无法使用select2的问题: 通常情况下,使用select2只需要在加载相关js和css后,添加如下代码即可: $("#selectId").select2(); 但如 ...

  7. StreamingAssets文件夹在不同平台上的引用

    On a desktop computer (Mac OS or Windows) the location of the files can be obtained with the followi ...

  8. SQL Server 事件通知(Event notifications)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 基础知识(Rudimentary Knowledge) 事件通知监控DDL(NotifyQue ...

  9. 剑指Offer面试题:24.复杂链表的复制

    一.题目:复杂链表的复制 题目:请实现函数ComplexListNode Clone(ComplexListNode head),复制一个复杂链表.在复杂链表中,每个结点除了有一个Next指针指向下一 ...

  10. 一个App完成入门篇-终结篇(八)- 应用收官

    经过以上几步的学习,我们终于来到最后一个步骤了,应用APP也接近尾声. 通过之前的几节教程,不知道您对使用DeviceOne开发一个应用是不是已经得心应手了,本节教程将教会大家如何在开发完成之后通过D ...