zoukankan      html  css  js  c++  java
  • 数据库-用户管理与pymysql

    mysql用户管理

    !这是dba的活儿!,但是万一公司没有dba?

    mysql用户指的是什么?

    我们每一次在操作前都需要指定账号和密码,这个账号就是mysql的用户;

    为什么要管理?

    一个公司不可能只有一个工程师,大公司,不不仅有很多工程师 还有很多不同部门,但是数据库服务器只有一个,大家都要访问,这就涉及到用户和权限问题了

    一个工程师对应一个账户,

    哪些工程师可以操作哪些数据库,哪些表,甚至哪些字段,都可以进行控制,例如,腾讯,有qq和微信不同项目,qq的工程师,就不应该去访问微信项目的数据;

    mysql是如何管理的

    mysql本质上是一款cs软件,它具备用户认证!

    我们有没有做过用户认证呢? ATM! 购物车,都做过,

    我们是如何实现的?写入文件,mysql也是一样的,

    只不过它把文件称为表

    那么要怎么添加账户呢?

    把用户信息写入表中就可以了

    权限相关表

    来看看它都把数据放在哪个表中了!

    自带的mysql数据库,四个表用于存储账户信息以及权限
    user
    db
    table_priv
    columns_priv
    

    权限优先级

    user->db->table_priv->columns_priv

    select *from user;
    #由于字段较多 以表格形式展示 会比较乱,可以添加G来纵向显示
    select *from userG;
    

    内置root账户字段信息解析

    创建账号语句

    create user 用户名@"ip地址"  "identified" by 密码;
    create user tom@"192.168.101" identified by "123";
    

    该语句表面tom只能在101机器上使用,别的机器就无法登录

    用%可以表示在任意机器可用

    注意:该方式创建的账号没有任何权限 所以了解即可

    授权语句

    授权:

    授权语句执行时如果账号不存在会自动创建账号 所以更推荐使用

    注意:默认只有root才能为其他账号授权

    grant all on *.* to tom@"localhost" identified by "123";
    #该语句中的all 增删改查所有权限 但是不包括grant权限
    #*.* 表示任何数据库 任何表 存储在user表
    
    grant all on *.* to toms@"%" identified by "123";
    # host 为% 表示 该账户可以在任何主机上登录但是不包括localhost
    grant all on *.* to toms@"localhost" identified by "123";
    # 继续执行 上述语句保证localhost也可以登录该账户
    
    
    
    
    grant all on db.* to tom@"localhost" identified by "123" 
    #db.* 该用户可以操作db数据库的任何表 存储在 db表
    
    
    grant all on db.t1 to tom@"localhost" identified by "123" 
    #db.* 该用户可以操作db数据库的t1表 存储在 table_privi表
    
    
    grant select(id) on db.t1 to tom@"localhost" identified by "123" 
    #精确到字段 和  操作级别
    #该用户只能查询 db下的t1表 
    
    
    
    grant all on *.* to tom@"localhost" identified by "123" with grant option;
    #with grant option 表示该账户可以将权限授予其他用户
    
    
    
    REVOKE all privileges [column]   on db.table from user@"host";
    #收回权限
    
    
    drop user@"host"
    #删除用户
    
    flush privileges;
    #刷新权限表 一些时候权限信息可能会有所延迟 可以执行该语句立即刷新权限信息
    

    pymysql模块

    pymysql是python提供的一个mysql客户端模块,用于与mysql服务器建立连接,发送查询,并获取结果等;

    基本使用:

    import pymysql
    # 1.建立连接
    try:
        conn = pymysql.connect(host="127.0.0.1",port=3306,user="root",password="",db="day46",)
        print("连接服务器成功!")
        #2.获取游标对象
        cursor = conn.cursor()
        #3.执行sql语句
        count = cursor.execute("select *from user")
        print("结果数量: %s" % count)
    
        # 提取结果
        # print(cursor.fetchall())
        # print(cursor.fetchone())
        # print(cursor.fetchmany(1))
    
        # 移动游标位置  相对当前位置
        cursor.scroll(1,"relative")
        cursor.scroll(-1, "relative")
        print(cursor.fetchone())
    
        # 移动游标位置  使用绝对位置
        cursor.scroll(0, "absolute")
        print(cursor.fetchone())
    
        print(cursor.fetchall())
        # 注意 游标移动到末尾后无法在读取到数据 若需重复读取数据,需要使用scroll来移动游标
    
    except Exception as e:
        print("连接服务器失败.....")
        print(type(e),e)
    finally:
        if cursor:
            cursor.close()
            print("关闭游标")
        if conn:
            conn.close()
            print("关闭链接")
    

    上述代码中 fetch 相关函数返回值类型为元组,使用起来不够方便,我们可以在创建游标时指定游标类型为字典类型像这样:

    cursor = conn.cursor(pymysql.cursors.DictCursor)
    

    sql注入攻击

    何为sql注入

    sql注入指的是,用户在输入数据时,按照sql的语法,来编写带有攻击目的的sql语句,并插入到原始语句中执行.

    例如:登录功能,需要用户输入用户名和密码

    正常的一个登录功能代码如下:

    try:
        conn = pymysql.connect(host="127.0.0.1",port=3306,user="root",password="",db="day46",)
        print("连接服务器成功!")
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        
        user = input("username:")
        password = input("password:")
        
        count = cursor.execute("select *from user where name = '%s' and password = '%s'" % (user,password))
        if count:
            print("登录成功!")
        else:
            print("登录失败!")
    except Exception as e:
        print(type(e),e)
    finally:
        if cursor:cursor.close()
        if conn: conn.close()
    

    上述代码有被注入攻击的危险

    尝试在用户名中输入以下内容,密码随意
     jerry' — ass 
    或者连用户名都不用写
    ' or 1 = 1 -- asaa
    

    解决方案:

    ​ 1.客户端在发送sql给服务器前进行re判断

    ​ 这样的问题在于一些程序可以模拟客户端直接发送请求给服务器

    ​ 2.在服务器端将sql交给mysql是作进一步处理,相关的代码其实pymysql已经做了封装

    ​ 我们只要保证不要自己来拼接sql语句即可,将拼接参数操作交给pymysql.

    try:
        conn = pymysql.connect(host="127.0.0.1",port=3306,user="root",password="",db="day46",)
        print("连接服务器成功!")
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        
        user = input("username:")
        password = input("password:")
    
        sql = "select *from user where name = %s and password = %s"
        print(sql)
        count = cursor.execute(sql,(user,password)) # 参数交给模块
        if count:
            print("登录成功!")
        else:
            print("登录失败!")
    except Exception as e:
        print(type(e),e)
    finally:
        if cursor:cursor.close()
        if conn: conn.close()
    

    增删改

    import pymysql
    
    
    # 1.建立连接
    try:
        conn = pymysql.connect(host="127.0.0.1",port=3306,user="root",password="",db="day46",)
        print("连接服务器成功!")
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        
        #增
        #sql = "insert into user values(null,%s,%s,%s)"
        #count = cursor.execute(sql,("tom","man","123321"))
        # 一次性插入多条记录
        #sql = "insert into user values (null,%s,%s,%s)"
        #count = cursor.executemany(sql, [("周芷若","woman","123"), ("赵敏","woman","321")])
        
        #删
        # count = cursor.execute("delete from user where id = 1")
    
        
        #改
        count = cursor.execute("update user set name = '刘大炮' where id = 1")
    
        if count:
            print("执行成功!")
        else:
            print("执行失败!")
    
        # 获取最新的id
        # print(cursor.lastrowid)
    except Exception as e:
        print(type(e),e)
    
    finally:
        if cursor:cursor.close()
        if conn: conn.close()
    

    强调:pymysql 对于数据的增删改默认都不会生效,必须调用链接对象的commit()来提交修改 或者在创建链接对象时指定为自动提交;

    conn.commit() 
    #或者创建链接对象时指定为自动提交
    conn = pymysql.connect(host="127.0.0.1",port=3306,user="root",password="",db="day46",autocommit=True)
    
  • 相关阅读:
    《新下级学》第七章第二节——冲突
    《新下级学》第七章序和第一节——微观互动
    《新下级学》第六章第七第八节(本章完)——下级软弱的根源等
    《新下级学》第六章第五第六节——下级的底线等
    《新下级学》第六章第三第四节——下级对平级的看法等
    《新下级学》第六章第二节——下级对自己的需求、能力和责任的看法
    《新下级学》第六章序、第一节——下级的思维特点及处境
    《新下级学》第四章、第五章——什么是“下级”
    《新下级学》第三章——新下级学的立场
    变电站GPS校时产品(GPS对时设备)在线监测技术
  • 原文地址:https://www.cnblogs.com/yangyuanhu/p/11188835.html
Copyright © 2011-2022 走看看