四、数据库用户管理
1、用户定义
用户定义:
mysql> select user,host,password from mysql.user;
+------+--------------+-------------------------------------------+
| user | host | password
==================================
user 主机范围
使用某个用户 从哪些主机地址可以访问我的数据库
用户的功能:
1、用来登录mysql数据库
2、用来管理数据库对象(库,表)
权限:
功能:针对不同的用户,设置不同的对象管理能力。
select updata delete insert creat ...
权限的范围:
*.* :全局范围
oldboy.* :单库级别
oldboy.t1 :单表级别
创建用户并授权:
grant all on wordpress.* to workpress@'10.0.0.%' identified by 'oldboy123';
授权命令 权限 权限范围 用于 主机范围 密码
修改超级管理员用户:root
修改密码:mysqladmin -uroot -p password oldboy123
root@localhost
普通用户:select,updata,delete,insert,create,drop (增删改查)
只针对用户的操作命令:
mysql> create user zabbix@'10.0.0.%' identified by 'oldboy123';
Query OK, 0 rows affected (0.01 sec)
mysql> drop user root@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,password from mysql.user;
特殊的删除方法:
mysql> delete from mysql.user where user='oldboy' and host='localhost';
Query OK, 1 row affected (0.00 sec)
mysql> flush privileges;
2、用户授权
grant all on wordpress.* to workpress@'10.0.0.%' identified by 'oldboy123';
授权命令 权限 权限范围 用于 主机范围
all权限:
SELECT,INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES,
INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE,
REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE,
CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE
开发用户权限:(root用户进行授权)
grant SELECT,INSERT, UPDATE, DELETE, CREATE, DROP on testdb.* to zabbix@'10.0.0.%';
使用zabbix检查:
mysql> create database testdb;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
创建用户并授权:
mysql> grant all on *.* to root@'10.0.0.%' identified by 'oldboy123';
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,password from mysql.user;
查询用户的权限:
mysql> show grants for zabbix@'10.0.0.%';
创建类似管理员:
mysql> show grants for root@'10.0.0.%';
本地超级管理员:有grants权限
mysql> show grants for root@'localhost';
收回权限:
mysql> revoke create,drop on testdb.* from zabbix@'10.0.0.%';
mysql> show grants for zabbix@'10.0.0.%';
思考:
grant select on *.* to zabbix@'10.0.0.%';
grant INSERT, UPDATE, DELETE, CREATE, DROP on testdb.* to zabbix@'10.0.0.%';
grant update on testdb.t1 to zabbix@'10.0.0.%';
###
mysql> use testdb;
mysql> create table t1(id int);
mysql> show tables;
mysql> insert into t1 values(1);
问:zabbix@'10.0.0.%' 对t1 表到底有什么权限?
如果对某个用户在不同的数据库级别设置了权限,最终权限权限叠加,加起来的最大权限为准。
建议,不要多范围授权。