MySQL 默认有个root用户,但是这个用户权限太大,一般只在管理数据库时候才用。如果在项目中要连接 MySQL 数据库,则建议新建一个权限较小的用户来连接。

在 MySQL 命令行模式下输入如下命令可以为 MySQL 创建一个新用户:

CREATE USER username IDENTIFIED BY 'password';

新用户创建完成,但是此刻如果以此用户登陆的话,会报错,因为我们还没有为这个用户分配相应权限,分配权限的命令如下:

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

授予username用户在所有数据库上的所有权限。

如果此时发现刚刚给的权限太大了,如果我们只是想授予它在某个数据库上的权限,那么需要切换到root 用户撤销刚才的权限,重新授权:

EVOKE ALL PRIVILEGES ON *.* FROM 'username'@'localhost';

GRANT ALL PRIVILEGES ON wordpress.* TO 'username'@'localhost' IDENTIFIED BY 'password';

甚至还可以指定该用户只能执行 select 和 update 命令:

GRANT SELECT, UPDATE ON wordpress.* TO 'username'@'localhost' IDENTIFIED BY 'password';

这样一来,再次以username登陆 MySQL,只有wordpress数据库是对其可见的,并且如果你只授权它select权限,那么它就不能执行delete 语句。

另外每当调整权限后,通常需要执行以下语句刷新权限:

FLUSH PRIVILEGES;

删除刚才创建的用户:

DROP USER username@localhost;

仔细上面几个命令,可以发现不管是授权,还是撤销授权,都要指定响应的host(即 @ 符号后面的内容),因为以上及格命令实际上都是在操作mysql 数据库中的user表,可以用如下命令查看相应用户及对应的host:

SELECT User, Host FROM user;

MySQL Study之--MySQL用户及权限管理

MySQL服务器通过MySQL权限表来控制用户对数据库的访问,MySQL权限表存放在mysql数据库里,由mysql_install_db脚本初始化。这些MySQL权限表分别user,db,table_priv,columns_priv和host。下面分别介绍一下这些表的结构和内容:

user权限表:记录允许连接到服务器的用户帐号信息,里面的权限是全局级的。

db权限表:记录各个帐号在各个数据库上的操作权限。

table_priv权限表:记录数据表级的操作权限。

columns_priv权限表:记录数据列级的操作权限。

host权限表:配合db权限表对给定主机上数据库级操作权限作更细致的控制。这个权限表不受GRANT和REVOKE语句的影响。

案例分析:

一、创建用户并授权(root用户)

[root@mysrv ~]# mysql -u root -poracle

mysql> select version()\g

+-------------------------------------------+

| version() |

+-------------------------------------------+

| 5.6.25-enterprise-commercial-advanced-log |

+-------------------------------------------+

1 row in set (0.00 sec)

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| prod |

| test |

+--------------------+

5 rows in set (0.01 sec)

1、建立tom用户并授权(特权管理用户)

mysql> grant all on prod.* to 'tom'@'%' identified by 'tom' with grant option;

Query OK, 0 rows affected (0.00 sec)

查看用户创建是否成功:

mysql> select user,host from user ;

查看tom用户的授权:

mysql> show grants for tom;

+----------------------------------------------------------------------------------------------------+

| Grants for tom@% |

+----------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'tom'@'%' IDENTIFIED BY PASSWORD '*71FF744436C7EA1B954F6276121DB5D2BF68FC07' |

| GRANT ALL PRIVILEGES ON `prod`.* TO 'tom'@'%' WITH GRANT OPTION |

+----------------------------------------------------------------------------------------------------+

GRANT 语法:

GRANT privileges (columns)

ON what

TO user IDENTIFIED BY "password"

WITH GRANT OPTION

权限列表:

ALTER: 修改表和索引。

CREATE: 创建数据库和表。

DELETE: 删除表中已有的记录。

DROP: 抛弃(删除)数据库和表。

INDEX: 创建或抛弃索引。

INSERT: 向表中插入新行。

REFERENCE: 未用。

SELECT: 检索表中的记录。

UPDATE: 修改现存表记录。

FILE: 读或写服务器上的文件。

PROCESS: 查看服务器中执行的线程信息或杀死线程。

RELOAD: 重载授权表或清空日志、主机缓存或表缓存。

SHUTDOWN: 关闭服务器。

ALL: 所有权限,ALL PRIVILEGES同义词。

USAGE: 特殊的 "无权限" 权限。

用 户账户包括 "username" 和 "host" 两部分,后者表示该用户被允许从何地接入。tom@'%' 表示任何地址,默认可以省略。还可以是 "tom@192.168.1.%"、"tom@%.abc.com" 等。数据库格式为 db@table,可以是 "test.*" 或 "*.*",前者表示 test 数据库的所有表,后者表示所有数据库的所有表。

子句 "WITH GRANT OPTION" 表示该用户可以为其他用户分配权限。

2、我们用 root 再创建几个用户,然后由 test 数据库的管理员tom为他们分配权限。

mysql> create user 'tom1' identified by 'tom1' ,'tom2' identified by 'tom2';

Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from user ;

root用户退出,tom登陆,并授权用户访问prod库

[root@mysrv ~]# mysql -u tom -ptom

ERROR 1045 (28000): Access denied for user 'tom'@'localhost' (using password: YES)

tom用户竟不能登陆!!!

再对tom用户授权:

mysql> grant all on prod.* to 'tom'@'localhost' identified by 'tom' with grant option;;

Query OK, 0 rows affected (0.00 sec)

mysql> show grants for tom;

+----------------------------------------------------------------------------------------------------+

| Grants for tom@% |

+----------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'tom'@'%' IDENTIFIED BY PASSWORD '*71FF744436C7EA1B954F6276121DB5D2BF68FC07' |

| GRANT ALL PRIVILEGES ON `prod`.* TO 'tom'@'%' WITH GRANT OPTION |

+----------------------------------------------------------------------------------------------------+

2 rows in set (0.00 sec)

mysql> use mysql;

Database changed

mysql> select user,host from user ;

tom登陆:

[root@mysrv ~]# mysql -u tom -ptom prod

mysql> select database();

+------------+

| database() |

+------------+

| prod |

+------------+

1 row in set (0.01 sec)

mysql> select current_user();

+----------------+

| current_user() |

+----------------+

| tom@localhost |

+----------------+

1 row in set (0.00 sec)

创建表:

mysql> show tables;

+----------------+

| Tables_in_prod |

+----------------+

| t1 |

+----------------+

1 row in set (0.00 sec)

mysql> create table t2 as select * from t1;

Query OK, 3 rows affected (0.15 sec)

Records: 3 Duplicates: 0 Warnings: 0

查看表信息:

mysql> desc t2;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(11) | YES | | NULL | |

| name | varchar(10) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

2 rows in set (0.01 sec)

mysql> show create table t2;

+-------+---------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table |

+-------+---------------------------------------------------------------------------------------------------------------------------+

| t2 | CREATE TABLE `t2` (

`id` int(11) DEFAULT NULL,

`name` varchar(10) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+---------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.01 sec)

mysql> show create table t2\G;

*************************** 1. row ***************************

Table: t2

Create Table: CREATE TABLE `t2` (

`id` int(11) DEFAULT NULL,

`name` varchar(10) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1

1 row in set (0.00 sec)

mysql> select * from t2;

+------+-------+

| id | name |

+------+-------+

| 10 | tom |

| 20 | jerry |

| 30 | rose |

+------+-------+

3 rows in set (0.00 sec)

3、tom用户为tom1,tom2授权

mysql> grant select on prod.* to tom1;

Query OK, 0 rows affected (0.00 sec)

mysql> grant select on prod.* to tom2;

Query OK, 0 rows affected (0.02 sec)

mysql> grant insert,update on prod.* to tom2;

Query OK, 0 rows affected (0.00 sec)

tom2登陆(从远程登陆):

C:\Users\Administrator>mysql -h 192.168.8.240 -utom2 -ptom2

mysql> select database();

+------------+

| database() |

+------------+

| NULL |

+------------+

1 row in set (0.00 sec)

mysql> use prod;

Database changed

mysql> select database();

+------------+

| database() |

+------------+

| prod |

+------------+

1 row in set (0.00 sec)

mysql> select current_user();

+----------------+

| current_user() |

+----------------+

| tom2@% |

+----------------+

1 row in set (0.00 sec)

mysql> show grants for tom2;

+------------------------------------------------------------------+

| Grants for tom2@% |

+------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'tom2'@'%' IDENTIFIED BY PASSWORD |

| GRANT SELECT, INSERT, UPDATE ON `prod`.* TO 'tom2'@'%' |

+------------------------------------------------------------------+

2 rows in set (0.00 sec)

mysql> show tables;

+----------------+

| Tables_in_prod |

+----------------+

| t1 |

| t2 |

+----------------+

2 rows in set (0.00 sec)

mysql> select * from t1;

+------+-------+

| id | name |

+------+-------+

| 10 | tom |

| 20 | jerry |

| 30 | rose |

+------+-------+

3 rows in set (0.00 sec)

mysql> select * from t2;

+------+-------+

| id | name |

+------+-------+

| 10 | tom |

| 20 | jerry |

| 30 | rose |

+------+-------+

3 rows in set (0.00 sec)

mysql> insert into t1 values (40,'john');

Query OK, 1 row affected (0.00 sec)

mysql> commit;

Query OK, 0 rows affected (0.09 sec)

mysql> select * from t1;

+------+-------+

| id | name |

+------+-------+

| 10 | tom |

| 20 | jerry |

| 30 | rose |

| 40 | john |

+------+-------+

4 rows in set (0.00 sec)

mysql> update t1 set name='ellen' where id=40;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from t1;

+------+-------+

| id | name |

+------+-------+

| 10 | tom |

| 20 | jerry |

| 30 | rose |

| 40 | ellen |

+------+-------+

4 rows in set (0.00 sec)

mysql> delete from t1;

ERROR 1142 (42000): DELETE command denied to user 'tom2'@'192.168.8.254' for tab

le 't1'

mysql> commit;

Query OK, 0 rows affected (0.05 sec)

mysql> select * from t1;

+------+-------+

| id | name |

+------+-------+

| 10 | tom |

| 20 | jerry |

| 30 | rose |

| 40 | ellen |

+------+-------+

4 rows in set (0.00 sec)

4、回收tom2的update权限:

mysql> revoke update on prod.* from tom2;

Query OK, 0 rows affected (0.00 sec)

tom2再重新登陆:

C:\Users\Administrator>mysql -h 192.168.8.240 -utom2 -ptom2

mysql> use prod;

Database changed

mysql> update t1 set name='lily' where id=10;

ERROR 1142 (42000): UPDATE command denied to user 'tom2'@'192.168.8.254' for tab

le 't1'

---update失败!

二、修改用户口令:

1、root用户修改普通用户口令

mysql> set password for tom1=password('oracle');

Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

tom1重新登陆:

C:\Users\Administrator>mysql -h 192.168.8.240 -utom1 -ptom1

Warning: Using a password on the command line interface can be insecure.

ERROR 1045 (28000): Access denied for user 'tom1'@'192.168.8.254' (using passwor

d: YES)

---旧口令登陆失败!

C:\Users\Administrator>mysql -h 192.168.8.240 -utom1 -poracle

mysql>

2、普通用户修改自己密码:

C:\Users\Administrator>mysql -h 192.168.8.240 -utom1 -poracle

mysql> set password=password('tom1');

Query OK, 0 rows affected (0.00 sec)

重新登陆:

C:\Users\Administrator>mysql -h 192.168.8.240 -utom1 -ptom1

mysql>

---新密码登陆成功 !

三、删除用户:

1、回收用户所有权限

mysql> revoke all on prod.* from tom2;

Query OK, 0 rows affected (0.01 sec)

2、删除用户

mysql> drop user tom2;

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from user;

------- 摘要 --------------------------------------

创建用户:

GRANT insert, update ON testdb.* TO user1@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

CREATE USER user2 IDENTIFIED BY 'password';

分配权限:

GRANT select ON testdb.* TO user2;

查看权限:

SHOW GRANTS FOR user1;

修改密码:

SET PASSWORD FOR user1 = PASSWORD('newpwd');

SET PASSWORD = PASSWORD('newpwd');

移除权限:

REVOKE all ON *.* FROM user1;

删除用户:

DROP USER user1;

数据库列表:

SHOW DATABASES;

数据表列表:

SHOW TABLES;

当前数据库:

SELECT DATABASE();

当前用户:

SELECT USER();

数据表结构:

DESCRIBE table1;

刷新权限:

FLUSH PRIVILEGES;

grant和revoke可以在几个层次上控制访问权限

1,整个服务器,使用 grant ALL 和revoke ALL

2,整个数据库,使用on database.*

3,特点表,使用on database.table

4,特定的列

5,特定的存储过程

user表中host列的值的意义

% 匹配所有主机

localhost localhost不会被解析成IP地址,直接通过UNIXsocket连接

127.0.0.1 会通过TCP/IP协议连接,并且只能在本机访问;

::1 ::1就是兼容支持ipv6的,表示同ipv4的127.0.0.1

grant 普通数据用户,查询、插入、更新、删除 数据库中所有表数据的权利。

grant select on testdb.* to common_user@’%’

grant insert on testdb.* to common_user@’%’

grant update on testdb.* to common_user@’%’

grant delete on testdb.* to common_user@’%’

或者,用一条 MySQL 命令来替代:

grant select, insert, update, delete on testdb.* to common_user@’%’

grant 数据库开发人员,创建表、索引、视图、存储过程、函数。。。等权限。

grant 创建、修改、删除 MySQL 数据表结构权限。

grant create on testdb.* to developer@’192.168.0.%’;

grant alter on testdb.* to developer@’192.168.0.%’;

grant drop on testdb.* to developer@’192.168.0.%’;

grant 操作 MySQL 外键权限。

grant references on testdb.* to developer@’192.168.0.%’;

grant 操作 MySQL 临时表权限。

grant create temporary tables on testdb.* to developer@’192.168.0.%’;

grant 操作 MySQL 索引权限。

grant index on testdb.* to developer@’192.168.0.%’;

grant 操作 MySQL 视图、查看视图源代码 权限。

grant create view on testdb.* to developer@’192.168.0.%’;

grant show view on testdb.* to developer@’192.168.0.%’;

grant 操作 MySQL 存储过程、函数 权限。

grant create routine on testdb.* to developer@’192.168.0.%’; -- now, can show procedure status

grant alter routine on testdb.* to developer@’192.168.0.%’; -- now, you can drop a procedure

grant execute on testdb.* to developer@’192.168.0.%’;

grant 普通 DBA 管理某个 MySQL 数据库的权限。

grant all privileges on testdb to dba@’localhost’

其中,关键字 “privileges” 可以省略。

grant 高级 DBA 管理 MySQL 中所有数据库的权限。

grant all on *.* to dba@’localhost’

MySQL grant 权限,分别可以作用在多个层次上。

1. grant 作用在整个 MySQL 服务器上:

grant select on *.* to dba@localhost; -- dba 可以查询 MySQL 中所有数据库中的表。

grant all on *.* to dba@localhost; -- dba 可以管理 MySQL 中的所有数据库

2. grant 作用在单个数据库上:

grant select on testdb.* to dba@localhost; -- dba 可以查询 testdb 中的表。

3. grant 作用在单个数据表上:

grant select, insert, update, delete on testdb.orders to dba@localhost;

4. grant 作用在表中的列上:

grant select(id, se, rank) on testdb.apache_log to dba@localhost;

5. grant 作用在存储过程、函数上:

grant execute on procedure testdb.pr_add to ’dba’@’localhost’

grant execute on function testdb.fn_add to ’dba’@’localhost’

注意:修改完权限以后 一定要刷新服务,或者重启服务,刷新服务用:FLUSH PRIVILEGES。

mysql-front权限管理_mysql 用户及权限管理 小结相关推荐

  1. mysql用户及权限管理_MySQL 用户及权限管理

    1.mysql用户的定义 在mysql中,用户由用户名和主机域组成的. 1.1 用户名的写法 mysql> create user test1@'localhost'; 注意:用户名不要以数字. ...

  2. mysql vip的作用_MySQL用户与权限的管理

    用户连接到MySQL,可以做各种查询,这都是MySQL用户与权限功能在背后维持着操作. 用户与数据库服务器交互数据,分为两个阶段: (1)你有没有权连接上来 (2)你有没有权执行本操作 1.你有没有权 ...

  3. mysql 用户管理_mysql 用户管理及数据权限问题总结

    1.远程登录mysql mysql -h ip -u root -p 密码 2.创建用户 格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码 ...

  4. mysql取消权限设置_mysql用户授权与权限撤销

    mysql用户授权与权限撤销 实验练习: 1.允许root从192.168.4.0/24网段访问,对所有库/表有完全控制权限,需要验证的密码为xujunxian. 2.建立一个管理账号dba007,对 ...

  5. linux用户权限简介,Linux用户及权限管理

    [文件管理.管道.用户及组管理.用户及权限管理]\用户及组管理 用户与组管理 Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这 ...

  6. linux权限管理之用户和组管理

    用户 和 权限 的基本概念 基本概念 用户 是 Linux 系统工作中重要的一环,用户管理包括 用户 与 组 管理 在 Linux 系统中,不论是由本机或是远程登录系统,每个系统都必须拥有一个账号,并 ...

  7. 在MySQL中,如何给用户授予权限

    MySQL简介: MySQL是一个小型关系型数据库管理系统,开发者为瑞典MySQL AB公司.在2008年1月16号被Sun公司收购.而2009年,SUN又被Oracle收购.MySQL是一种关联数据 ...

  8. mysql 列级权限授予用户_mysql 用户及权限管理 小结

    MySQL 默认有个root用户,但是这个用户权限太大,一般只在管理数据库时候才用.如果在项目中要连接 MySQL 数据库,则建议新建一个权限较小的用户来连接. 在 MySQL 命令行模式下输入如下命 ...

  9. mysql设置user权限允许远程_mysql 用户及权限管理 允许远程连接

    mysq,功能强大的关系型数据库,它的用户管理在开发过程中当然也尤其重要,接下来就看看mysql的用户管理 1.登录数据库 mysql -uroot -p 回车 输入密码... 回车 2.登录成功后, ...

最新文章

  1. /etc/fstab自动挂载文件详解
  2. linux-basic(8)linux磁盘与文件系统管理
  3. android commit apply,关于SharedPreference的commit和apply和ANR
  4. 【kafka】kafka 启动报错 InvalidReceiveException: Invalid receive (size = -720899)
  5. python全局变量修改_python中全局变量的修改
  6. POJ3752 字母旋转游戏【水题】
  7. 有趣的算法:1元=1分
  8. 算法设计与分析:Jewels and Stones(Week 1)
  9. 汉字编码-GB2312-GBK-GB18030-Big5
  10. 利用扭力仪来检测特小公斤数电批输出扭矩
  11. “CHK文件恢复”和“文件恢复”有什么区别?
  12. 关于Windows系统脚本(jscript)
  13. PyCharm 下载/上传gitlab 代码
  14. Java 中代码优化的 30 个小技巧(下)
  15. Fuchsia源码分析--系统调用流程
  16. 网络舆情数据汇总统计的方法技巧
  17. Application程序jar打包笔记
  18. Matlab/Simulink中PMSM模型的反电动势系数和转矩系数
  19. 从马文到AlphaGo AI走过了怎样的70年?
  20. 简单 洛谷 P1563 【模拟】玩具谜题普及场

热门文章

  1. python专业名词_python 术语
  2. python shelve模块_python3 shelve模块的详解
  3. 道旅:使用ARMS做业务监控数据清洗
  4. 走出舒适圈,从来都不简单
  5. 对话阿里巴巴贾扬清:如何成为一名优秀的 AI 架构师?
  6. 《阿里巴巴Java开发规约》插件使用详细指南
  7. ubuntu虚拟机卡住开机_虚拟机Ubuntu开机问题及解决方法
  8. jquery获取手机验证码按钮计时插件getVerifyCode.js
  9. 《小美斗地主》:棋牌手游新商业模式启示录
  10. 魔方游戏实现:任意阶魔方的表示