pymysql

1.mysql用户管理  

    数据安全非常重要  不可能随便分配root账户

    应该按照不同开发岗位分配不同的账户和权限

 

    mysql中 将于用户相关的数据放在mysql库

    user - > db - > tables_priv -> columns_priv

    如果用户拥有对所有库的访问权 则存储在 user中

    如果用户拥有对部分库的使用权          db

    如果用户拥有对部分表的使用权          tables;

    如果用户拥有对表中某些字段的使用权    columns_priv中

 

   

Mysql中创建新账户

    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这个用户  并允许jack在任意一台电脑登录

    grant all on  day45.* to "jack"@"%" identified by  "123";

    授予day45数据库的emp表的所有权限给rose这个用户  并允许rose在任意一台电脑登录

    grant all on  day45.emp to "rose"@"%" identified by  "123";

    授予day45数据库的emp表的name字段的查询权限给maria这个用户  并允许maria在任意一台电脑登录

    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;

 

2.pymysql

后期开发中都是用框架代替pymysql

 

import pymysql

 

conn = pymysql.connect(

    host="127.0.0.1",

    port=3306,

    user="root",

    password="root",

    database="day26",

    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)

 

sql注入攻击 :’—随便写  或者是or 1 – 随便写

 

 什么是sql注入攻击 一些了解sql语法的攻击者  可以通过一些特殊符号 来修改 sql执行逻辑 达到绕过验证的效果

避免的方式 1.在输入时加上正则判断 不允许输入与sql相关的关键字 这种方式 无法避免 代理服务器发起的攻击

2.在服务器端 执行sql前先来一波判断

pymysql中已经帮你做了处理  只要将参数的拼接交给pymysql来完成就能够避免攻击

cursor.execute() 在括号内拼接

pymysql 不会自动提交 对数据的修改不会持久化 需要手动commit

例子:

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)

posted @ 2018-11-26 22:36  彼岸花纽约  阅读(244)  评论(0编辑  收藏  举报