zoukankan      html  css  js  c++  java
  • day03--MySQL用户篇

    mysql用户权限管理

    Linux系统 mysql数据库
    用户的作用 1.登录系统 2.启动进程 3.文件权限 1.登录数据库 2.管理数据库
    创建用户 1.useradd 2.adduser 1.grant 2.create user root@'localhost' 3.insert
    删除用户 1.userdel -r 1.drop user root@'localhost' 2.delete
    修改用户 1.usermod 1.update
    查看用户 1.id 2.passwd 1.select user from mysql.user;

    1.在MySQL中,用户是怎么定义的

    #mysql中,定义一个用户是:
    	用户名@'主机域'
    	
    #用户名写法:
    	用户名如果是字符:
    		mysql> create user root@'10.0.0.1';
    	用户名是数字需要加引号:
    		mysql> create user '123'@'10.0.0.1';
    
    #主机域的写法:
    	localhost
    	127.0.0.1
    	172.16.1.51
    	db01
    	172.16.1.%
    	172.16.1.5%      #172.16.1.50-59
    	172.16.%.%
    	172.%.%.%
    	%
    	10.0.0.0/255.255.255.0
    	10.0.0.0/24					#可以设置,但是不生效
    

    2.用户的管理

    1)创建用户

    mysql> create user root@'localhost';
    mysql> grant all on *.* to root@'localhost' identified by '123';
    mysql> insert ...
    

    2)查看用户

    mysql> select user,host from mysql.user;
    

    3)修改用户密码

    1.命令行使用mysqladmin修改密码
    [root@db02 ~]# mysqladmin -uroot -p123 password 123456
    
    2.update修改用户密码
    mysql> update mysql.user set password=PASSWORD('123') where user='root' and host='localhost';
    
    3.修改当前用户密码
    mysql> set password=password('123456');
    
    4.grant修改密码
    mysql> grant all on *.* to root@'localhost' identified by '123';
    mysql> flush privileges;
    

    4)删除用户

    mysql> drop user qiudao@'10.0.0.0/24';
    

    5)忘记root用户密码怎么办

    1.停止数据库
    systemctl stop mysqld
    
    2.跳过授权表和网络启动
    mysqld_safe --skip-grant-tables --skip-networking &
    
    3.登录数据库
    mysql
    
    4.修改密码
    mysql> flush privileges;
    mysql> grant all on *.* to root@'localhost' identified by '123';
    mysql> flush privileges;
    
    5.退出重启数据库
    mysqladmin -p123 shutdown
    systemctl start mysql
    

    3.权限的管理

    1)授权命令

    grant all on *.* to root@'localhost' identified by '123';
    grant all privileges on *.* to root@'localhost' identified by '123';
    
    grant 				#授权命令
    all privileges 		#权限(所有权限)
    on 					#在...上
    *.* 				#所有库.所有表
    to 					#给
    root@'localhost' 	 #用户名@'主机域'
    identified 			#设置密码
    by 					#是
    '123';				#'密码'
    

    2)所有权限

    #查看用户权限
    mysql> show grants for lhd@'10.0.0.0/255.255.255.0';
    
    #回收权限
    mysql> revoke drop on *.* from lhd@'10.0.0.0/255.255.255.0';
    
    #所有权限
    SELECT, INSERT, UPDATE, DELETE, CREATE, 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, DROP, GRANT
    

    3)作用对象

    #授权root@'localhost'对所有库所有表拥有所有权限,密码是123
    grant all on *.* to root@'localhost' identified by '123';		#所有库所有表授权
    
    #授权root@'localhost'对wordpress库下所有表拥有所有权限,密码是123
    grant all on wordpress.* to root@'localhost' identified by '123';		#单库授权
    
    #授权root@'localhost'对wordpress库下所有表拥有查看,插入,修改的权限,密码是123(最常用)
    grant select,insert,update on wordpress.* to root@'localhost' identified by '123';	#指定权限单库授权
    
    #授权root@'localhost'用户对mysql库下的user表拥有查看,插入,修改的权限,密码是123
    grant select,insert,update on mysql.user to root@'localhost' identified by '123';	#单表授权
    
    #在企业中,单列授权被称为脱敏
    grant select(user) on mysql.user.host to root@'localhost' identified by '123';		#单列授权
    

    4)在企业里权限设定

    #开发跟你要一个数据库用户
    1.你要操作什么库,有没有指定表?
    2.你要什么用户?
    3.你从哪台主机连过来?
    4.密码你有没有要求?
    5.这个用户你要用多久?
    6.走流程,发邮件?
    
    #一般情况给开发的权限
    grant select,update,insert on dev.* to dev@'172.16.1.50' identified by 'QiuDao@123';
    
    #开发:你把root用户给我呗?
    

    4.权限设置实践

    1)准备数据库

    #创建wordpress数据库
    create database wordpress;
    #使用wordpress库
    use wordpress;
    #创建t1、t2表
    create table t1 (id int);
    create table t2 (id int);
    
    #创建blog库
    create database blog;
    #使用blog库
    use blog;
    #创建t1表
    create table tb1 (id int);
    

    2)授权

    #授权wordpress@'10.0.0.5%'对于所有库所有表有查看权限,密码是123
    1.grant select on *.* to wordpress@'10.0.0.5%' identified by '123';
    
    #授权wordpress@'10.0.0.5%'对于wordpress下所有表有插入,删除,修改权限,密码是123
    2.grant insert,delete,update on wordpress.* to wordpress@'10.0.0.5%' identified by '123';
    
    #授权wordpress@'10.0.0.5%'对于wordpress下t1表所有权限,密码123
    3.grant all on wordpress.t1 to wordpress@'10.0.0.5%' identified by '123';
    

    3)提问

    #有一个人,使用wordpress用户通过10.0.0.51登录数据库,请问
    1.对于t1表,有哪些操作权限?
    	所有权限
    2.对于t2表,有哪些操作权限?
    	增、删、改、查
    3.对于tb1表,有哪些操作权限?
    	查
    

    4)总结

    1.如果不在同一级别授权,权限是相加关系
    2.但是我们不推荐在多级别定义重复权限。
    3.最常用的权限设定方式是单库级别授权
    	即:grant select,update,insert on dev.* to dev@'172.16.1.50' identified by 'QiuDao@123';
    4.如果涉及到敏感信息,我们使用脱敏,即单列授权
    	grant select(user) on mysql.user.host to root@'localhost' identified by '123';
    5.查看用户权限
    	show grants for 用户名@'主机域';
    
  • 相关阅读:
    【转】mxGraph教程-开发入门指南
    利用IPC通道进行进程间通信(C#)
    C++引用指针 & 构造函数
    MySQL配置主主及主从备份
    MySQL 主从热备份(读写分离)
    SqlServer双机热备技术实践笔记
    c#中的弱引用:WeakReference
    px、em、rem、%、vw、vh、vm这些单位的区别
    深浅clone
    JavaScript-原始值和引用值
  • 原文地址:https://www.cnblogs.com/tcy1/p/13293487.html
Copyright © 2011-2022 走看看