• (九)MySQL用户和权限管理


    (1)用户管理

    1)登录和退出mysql

    例:
    mysql -h192.168.111.150 -P3306 -uroot -predhat mysql -e 'select user,host,authentication_string from mysql.user'
    -h 指定主机名 [默认为localhost]
    -P MySQL端口 [默认为3306]
    -u 指定用户名 [默认为root]
    -p 指定登录密码 [默认为空]
    此处mysql为指定登录的数据库
    -e 接SQL语句

    2)创建用户

    方法一:create user语句创建
    create user user1@'localhost' identified by 'password';
    方法二:grant 语句创建
    grant all on . to 'user1'@'localhost' identified by 'password';
    grant all on blog.* to 'user2'@'%' identified by 'password';

    3)删除用户

    方法一:drop user
    drop user 'user1'@'localhost';
    方法二:delete语句删除
    delete from mysql.user where user='user1' and host='localhost';
    flush privileges;

    4)修改用户密码

    root 用户修改自己的密码

    方法一:#mysqladmin -uroot -p'123' password 'new_password' //123为旧密码
    方法二:update mysql.user set authentication_string=password('new_password') where user='root' and host='localhost'; flush privileges
    方法三 :set password=password('new_password');

    root修改其他用户密码

    方法一:set password for user3@'localhost'=password('new_password');
    方法二:update mysql.user set authentication_string=password('new_password') where user='root' and host='localhost'; flush privileges;

    普通用户修改自己密码

    方法:set password=password('new_password');

    (2)权限管理

    1)权限应用级别

    user(全局) ---> db(数据库级别)--->tables_priv(表级别)---->columns_priv(字段级别)
    所有用户名和密码信息都是user表中,全局用户的权限在user表中;库级别的用户权限信息在db表中

    2)授权语法格式以及授权

    grant 权限列表 on 库名.表名 to '用户名'@'客户端主机' [identified by '密码' with option参数];

    权限列表:	、
    	all 所有权限
    	select,update
    数据库.表名:
    	*.*	 所有库下的所有表
    	web.*	web库下的所有表
    	web.stu_info  web库下的stu_info表
    客户端主机:
    	% 所有主机
    	192.168.2.%  192.168.2.0网段的所有主机
    	192.168.2.18	指定主机
    	localhost		指定主机
    with_option参数:
    	grant option 授权选项,其他用户可以授权
    	max_queries_per_hour :定义每小时允许执行的查询数
    	max_updates_per_hour :定义每小时允许执行的更新数
    	max_connections_per_hour :定义每小时可以建立的连接数
    	max_user_connections :定义单个用户同时可以建立的连接数
    

    示例

    grant all on . to admin1@'%' identified by 'new_password' with grant option;
    grant all on web.* to admin2@'%' identified by 'new_password';

    3)查看权限

    show grantsG 用户查看自己的权限
    show grants for 'admin1'@'%'G 管理员查看其他用户的权限

    4)revoke回收权限

    语法:revoke 权限列表 on 库.表 from 用户名@'客户端主机';

    示例

    revoke insert,delete on . from admin1@'%'; 回收部分权限
    revoke all privileges on . from admin2@'%'; 回收所有权限
    revoke all privileges,grant option on . from admin2@'%'; 回收所有权限

    5)删除用户

    5.6之前需要先把权限去掉在删除用户:revoke all privilege drop user
    5.7之后可以直接删除用户:drop user

  • 相关阅读:
    Windows10安装Oracle19c数据库详细记录(图文详解)
    构建 FTP 文件传输服务器
    构建 Samba 文件共享服务器
    Linux磁盘配额(xfs)
    Linux配置磁盘配额(ext4)
    Linux 制作ISO镜像
    Linux磁盘分区
    用户和文件权限管理命令的使用(实验)
    CentOS7 配置yum源
    VMware CentOS 7 安装(vmware的版本15.5)
  • 原文地址:https://www.cnblogs.com/lovelinux199075/p/8922149.html
Copyright © 2020-2023  润新知