• mysql数据库: 用户管理、pymysql使用、sql注入


    本文目录:

    一、用户管理

    二、pymysql增删改查

    三、sql注入攻击

    一、用户管理

    数据安全非常重要 不可能随便分配root账户
    应该按照不同开发岗位分配不同的账户和权限

    mysql中 将于用户相关的数据放在mysql库
    user - > db - > tables_priv -> columns_priv
    如果用户拥有对所有库的访问权 则存储在 user中
    如果用户拥有对部分库的使用权 db
    如果用户拥有对部分表的使用权 tables;
    如果用户拥有对表中某些字段的使用权 columns_priv中

    创建新账户
    create user "账户名"@"主机名" identified by 密码
    create user "tom"@"localhost" identified by "123";

    授予所有数据库所有表的所有权限给jerry这个用户 并允许jerry在任意一台电脑登录
    如果用户不存在会自动创建
    grant all on *.* to "jerry"@"%" identified by "123" with grant option;
    with grant option这个用户可以将拥有的权限授予别人

    授予day45数据库所有表的所有权限给jack这个用户 并允许jerry在任意一台电脑登录
    grant all on day45.* to "jack"@"%" identified by "123";
    授予day45数据库的emp表的所有权限给rose这个用户 并允许jerry在任意一台电脑登录
    grant all on day45.emp to "rose"@"%" identified by "123";
    授予day45数据库的emp表的name字段的查询权限给maria这个用户 并允许jerry在任意一台电脑登录
    grant select(name) on day45.emp to "maria"@"%" identified by "123";

    收回权限
    REVOKE all privileges [column] on db.table from user@"host";

    如何授权就如何收回 因为不同权限信息存到不同的表中
    REVOKE all privileges on day45.emp from maria@"%";

    立即刷新权限信息
    flush privileges;

    # 删除用户
    drop user 用户名@主机
    drop user maria@%

    当你在云服务器部署了 mysql环境时 你的程序无法直接连接到服务器 需要授予在任意一台电脑登录的权限
    grant all on *.* to "jerry"@"%" identified by "123" with grant option;

     

    二、pymysql增删改查

    import pymysql
    
    conn = pymysql.connect(
        host="127.0.0.1",
        port=3306,
        user="root",
        password="root",
        database="day47",
        charset="utf8"
    )
    # cursor 游标对象 负责执行sql语句 获取返回的数据
    # pymysql.cursors.DictCursor指定使用字典类型的游标 默认是元祖类型
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    
    sql = "select *from user"
    
    # 返回值是本次查询的记录条数
    res = cursor.execute(sql)  #执行sql
    print(cursor.fetchall())   # 提取所有结果
    # cursor.scroll(1,mode="absolute") # 游标从开始位置往后移动1条记录
    # cursor.scroll(1,mode="relative") # 游标从当前位置往后移动1条记录
    # print(cursor.fetchone())  # 提取一条记录
    # print(cursor.fetchone())
    # print(cursor.fetchone())
    # print(cursor.fetchmany(2)) # 提取指定数量记录
    
    # print(res)
    import pymysql
    
    conn = pymysql.connect(
        host="127.0.0.1",
        port=3306,
        user="root",
        password="root",
        database="day47",
        charset="utf8"
    )
    # cursor 游标对象 负责执行sql语句 获取返回的数据
    # pymysql.cursors.DictCursor指定使用字典类型的游标 默认是元祖类型
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    
    # sql = "insert into user values(null,'中狗子','123')"
    sql = "update user set name = '小黄' where name = '中狗子'"
    # sql = "delete from user where name = '大狗子'"
    
    res = cursor.execute(sql)
    # pymysql不会自动提交  对数据的修改不会持久化 需要手动commit
    conn.commit()
    print(res)

     

    三、sql注入攻击

    import pymysql
    
    conn = pymysql.connect(
        host="127.0.0.1",
        port=3306,
        user="root",
        password="root",
        database="day47",
        charset="utf8"
    )
    # cursor 游标对象 负责执行sql语句 获取返回的数据
    # pymysql.cursors.DictCursor指定使用字典类型的游标 默认是元祖类型
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    
    name = input("输入用户名:")
    
    pwd = input("输入密码:")
    
    
    sql = "select *from user where name = %s and password = %s"
    res = cursor.execute(sql,(name,pwd))
    if res:
        print("登录成功")
    else:
        print("登录失败")
    
    
    # 什么是sql注入攻击 
    # 一些了解sql语法的攻击者 可以通过一些特殊符号 来修改 sql执行逻辑 达到绕过验证的效果
    # 如何避免?
    # 1.在输入时加上正则判断 不允许输入与sql相关的关键字 这种方式 无法避免 代理服务器发起的攻击
    # 2.在服务器端 执行sql前先来一波判断
    # pymysql中已经帮你做了处理 只要将参数的拼接交给pymysql来完成就能够避免攻击
  • 相关阅读:
    ubuntu高版本环境变量问题
    linux下可以和qq截屏比拟的工具hotshots
    如何以非 root 用户将应用绑定到 80 端口-ssh 篇
    mvn profile 深层次目录打参数核心配置
    java 判断字符串编码
    springmvc 传递和接收数组参数
    系统分层 manager层意义
    mysql insert中文乱码无法插入ERROR 1366 (HY000): Incorrect string value
    Apache Lens —— 统计数据分析查询接口
    云平台需要开发的底层功能
  • 原文地址:https://www.cnblogs.com/wuzhengzheng/p/10273473.html
Copyright © 2020-2023  润新知