zoukankan      html  css  js  c++  java
  • MySQL 用户管理

    查看MySQL有哪些帐号
    select host, user from mysql.user;

    mysql帐号登录需要验证:用户名@客户端主机 + 密码;

    创建、修改、锁定、删除帐号
    用户名@主机 + 密码;
    主机名: localhost / 'db1.liuxw.com' / 192.168.18.128 / 192.168.18.128/255/255.255.0 / 192.168.18.% 或 192.168.18.1_
    %和_分别代表:%代表所有,_代表一个通配符;避免使用%,因为连接用户不确定;
    示例:create user 'zhangsan'@'localhost' identified by '123456';
    修改密码示例:alter user 'zhangsan'@'localhost' identified by '12341234'; #推荐使用
    set password for 'zhangsan'@'localhost'=password('12341234');

    alter user 'zhangsan'@'localhost' password expire; #让用户口令失效,修改后用户再次登录必须修改密码;
    alter user 'zhangsan'@'localhost' account lock; #锁定用户
    alter user 'zhangsan'@'localhost' account unlock; #解锁用户

    drop user 'zhangsan'@'localhost'; #删除用户,建议生产环境不要直接删除用户,可以先对帐号重命名,过段时间确认没有什么问题了,再执行drop
    remame user 'zhangsan'@'localhost' to 'zhangsan_bak'@'localhost';

    select user(), current_user(); #查看当前连接用户

    5.6版本安全加固
    delete from mysql.user where user!='root' or host!='localhost';
    truncate table mysql.db;
    flush privileges;

    -- ===================================================================
    授权 grant
    grant用于创建或是给现有的用户添加权限,语法:grant [权限list,insert,select...] on 授权对象 to 要创建或是授权的用户 identified by '密码';
    授权对象:
      所有对象,全局:*.*
      数据库级别:<db_name>.*
      表级别:<db_name>.<table_name>
    要创建或是授权的用户:‘user_name’@'hostname'
    密码:可选

    show privileges; #查看数据库有那些权限
    all 关键字,代表不包含对其它帐号授权以外的所有权限
    with grant option 关键字,代表可以给其它帐号授权
    如果现有系统使用root连接DB时,怎么样平滑回收权限?
    答:先创建一个 all with grant option权限的帐号,然后用这个帐号逐步回收root用户的权限
    查看用户权限的命令:
    show grants;
    show grants for current_user(); #当前连接用户
    show grants for 'user_name'@'hostname'; #查看其它用户

    授权原则:权限尽力少,用多少给多少。

    回收权限 revoke
    revoke用于撤销对用户的授权;语法:revoke [权限list] on 权限对象 from 帐号名称;

    密码忘记了怎么处理?
    方法1:启动时增加下面两个连接参数
    --skip-grant-tables #无需用户和密码登录,登录后禁止使用create user,grant,revoke,set password
    --skip-networking #禁止提供网络验证使用,只能本机连接进来
    然后利用:update mysql.user set authentication_string=password('新密码') where user='用户名' and host='主机';
    最后重启mysql

    方法2:利用拷贝user表文件方式  略


  • 相关阅读:
    理解构造器
    if与switch的性能比较
    Java对象的内存(一)
    shell编程_条件判断if
    shell编程_基础&变量
    集群架构篇:Nginx架构演进<拆分数据库 多台web节点共享静态资源>
    集群架构篇:Nginx流行架构LNMP
    集群架构篇:Nginx常用模块
    LInux系统@安装CentOS7虚拟机
    docker pull越来越慢的解决方法
  • 原文地址:https://www.cnblogs.com/shlc/p/10196481.html
Copyright © 2011-2022 走看看