MySQL 用户和权限管理

MySQL 用户和权限管理

Acha
2021-04-02 / 0 评论 / 125 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年04月02日,已超过1113天没有更新,若内容或图片失效,请留言反馈。

用户和权限管理

  1. 用户作用

    • 登录mysql
    • 管理mysql
  2. 用户定义

    用户名@'白名单'

    写法:

    • wordpress@'%'
    • wordpress@'localhost'
    • wordpress@'127.0.0.1'
    • wordpress@'10.0.0.%'
    • wordpress@'10.0.0.5%'
    • wordpress@'10.0.0.0/255.255.254.0'
    • wordpress@'10.0.%'
  3. 用户管理

    1. 创建用户

      mysql> create user ac@'10.0.0.%' identified by 'sa';

      说明:8.0以前,可以自动创建用户并授权

      mysql> grant all on *.* to ac@'10.0.0.%' identified by 'sa';

    2. 查询用户

      mysql> select user,host from mysql.user;

    3. 修改用户密码

      mysql> alter user ac@'10.0.0.%' identified by 'sa';

    4. 删除用户

      mysql> drop user ac@'10.0.0.%' ;

  4. 权限管理

    1. 权限列表

      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

    2. 授权命令

      例:grant all on *.* to ac@'10.0.0.%' identified by 'sa' with grant option;

      说明:grant [权限] on [作用目标] to [用户] identified by [密码] with grant option

    3. 授权需求

      • 创建一个管理员用户root,可以通过10网段,管理数据库.

        grant SELECT,INSERT, UPDATE, DELETE on wordpress.* to wordpress@'10.0.0.%' identified by '123';

      • 创建一个应用用户wordpress,可以通过10网段,wordpress库下的所有表进行SELECT,INSERT, UPDATE, DELETE.

        grant SELECT,INSERT, UPDATE, DELETE on wordpress.* to wordpress@'10.0.0.%' identified by '123';

    4. 权限回收

      show grants for wordpress@'10.0.0.%';
      mysql> revoke delete on wordpress.* from 'wordpress'@'10.0.0.%';
      mysql> show grants for wordpress@'10.0.0.%';


MySQL 的启动和关闭

  1. 正常启停
  • sys -v
    • mysql.server start ---> mysqld_safe ---> mysqld
  • system
    • mysql.service ---> mysqld

    需要依赖于 /etc/my.cnf

  1. 维护

    mysqld_safe

    例:修改密码mysqld_safe --skip-grant-tables --skip-networking &

    将参数临时加到命令行,命令行的优先级高


初始化配置

  1. 初始化作用

    • 影响数据库的启动
    • 影响到客户端的功能
  2. 初始化方法

    • 初始化配置文件(例如/etc/my.cnf)
    • 启动命令行上进行设置(例如:mysqld_safe mysqld)
    • 预编译时设置(仅限于编译安装时设置)
  3. 配置文件格式

    [标签]

    xxx=xxx

    [标签]

    xxx=xxx

  4. 标签归类

    服务器端:

    • [mysqld]
    • [mysqld_safe]
    • [server]

    客户端:

    • [mysql]
    • [mysqladmin]
    • [mysqldump]
    • [client]
  5. 模板文件 (5.7)

    # 服务器端配置
    [mysqld]
    # 用户
    user=mysql   
    # 软件安装目录                
    basedir=/application/mysql  
    # 数据路径
    datadir=/data/mysql/data 
    # socket文件位置
    socket=/tmp/mysql.sock
    # 服务器id号
    server_id=6
    # 短口号
    port=3306
    
    # 客户端配置
    [mysql]
    # socket文件位置
    socket=/tmp/mysql.sock
    
  6. 配置文件读取顺序

    查询: mysqld --help --verbose |grep my.cnf
    /etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf

  7. 强制使用自定义配置文件

    --defautls-file

    [root@db01 tmp]# mysqld_safe --defaults-file=/tmp/aa.txt &


MySQL的连接管理

  1. mysql连接命令

    注意:提前应该将用户授权做好

    # 授权
    mysql> grant all on *.* to root@'10.0.0.%' identified by '123';
    
    # TCPIP
    mysql -uroot -p -h 10.0.0.51 -P3306 
    
    # Socket  
    mysql -uroot -p -S /tmp/mysql.sock
    
  2. 客户端工具

    dbforger

    sqlyog

    navicat

0

评论

博主关闭了当前页面的评论