mysql数据库—用户管理、pymysql模块

用户管理

​ 主要为了控制权限,让不同开发者,仅能操作属于自己的业务范围内的数据

创建myqsl账户

账户中涉及的三个数据

​ 账户名 密码 ip地址

​ ip是用于限制某个账户只能在哪些机器上登录

create user 用户名@主机地址  identified by "密码";

# 注意:操作用户 只能由root账户来进行


# 删除 将同时删除所有权限
drop user 用户名@主机地址;

权限管理

涉及到的表

user   与用户相关信息
db		用户的数据库权限信息
tables_priv   用户的表权限
columns_priv  用户的字段权限 

# 权限优先级
user->db->table_priv->columns_priv

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

语法:

grant all  on *.*  to 用户名@主机地址  identified by "密码";

# 如果用户不存在则自动创建新用户,推荐使用
grant all  on *.*  to rose@localhost  identified by "123";


grant all  on day42.*  to rose1@localhost  identified by "123";


grant all  on day42.table1  to rose2@localhost  identified by "123";


grant select(name),update(name)  on day42.table1  to rose3@localhost  identified by "123";



all表示的是对所有字段的增删改查  
*.*  所有库的所有表 


收回权限 
revoke all on *.* from 用户名@主机地址;


revoke all on day42.table1 from rose2@localhost;


# 刷新权限
flush privileges;



#with grant option   表示 可以将他拥有的权限授予其它的用户
grant all  on *.*  to root1@localhost  identified by "123" with grant option;



# 授予某个用户 可以在任何主机上登录
grant all  on *.*  to jack10@"%"  identified by "123";
grant all  on *.*  to jack10@localhost  identified by "123";

2.可视化客户端

​ navicat

pymysql 模块

​ pymysql 是python中提供的第三方模块,帮我们封装了,建立连接,用户认证,sql'的执行以及,结果的获取

基本使用:

import pymysql

"""
1.连接服务器
2.用户认证
3.发送指令
4.提取结果 
"""
# 1. 连接服务器  获取连接对象(本质上就是封装号的socket)
conn = pymysql.connect(
    host = "127.0.0.1",  #如果是本机 可以忽略
    port = 3306,    # 如果没改过 可以忽略
    user = "root", #必填
    password = "111", #必填
    database = "day42" #必填
)

# 2.通过连接拿到游标对象
# 默认的游标返回的是元组类型 不方便使用,需要更换字典类型的游标
c = conn.cursor(pymysql.cursors.DictCursor)

# 3.执行sql
sql = "select  * from table1"
res = c.execute(sql)
# 查询语句将返回查询的结果数量


# 4.提取结果
# print(res)
# print(c.fetchall())


# 5.关闭连接
c.close()
conn.close()



# 移动光标 参数1位移动的位置   mode 指定 相对或绝对
# c.scroll(1,mode="absolute")

# print(c.fetchall())

# print(c.fetchmany(1))
print(c.fetchone())
print(c.fetchone())

sql注入攻击:

指的是,用户在输入数据时,按照sql的语法规范,提交了用于攻击性目的的sql语句,并插入到原始语句中执行。

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

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

如何避免这个问题 ?

1.在服务器端执行sql以前做sql的验证

2.在服务器端将sql交给mysql是作进一步处理,相关的代码其实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
conn = pymysql.connect(
    host = "127.0.0.1",  #如果是本机 可以忽略
    port = 3306,    # 如果没改过 可以忽略
    user = "root", #必填
    password = "111", #必填
    database = "day42", #必填,
    #autocommit=False  # 开启自动提交  不常用....
)

c = conn.cursor(pymysql.cursors.DictCursor)
name = input("name:")
pwd = input("pwd:")

sql = "select *from user where name = %s"

if c.execute(sql,(name,)):
    print("用户名已存在!")
else:
    sql2 = "insert  into user values(%s,%s)"
    if c.execute(sql2,(name,pwd)):
        print("注册成功!")
        conn.commit() # 调用连接对象的提交函数
    else:
        print("注册失败!")

c.close()
conn.close()

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

conn.commit() 
#或者创建链接对象时指定为自动提交
conn = pymysql.connect(host="127.0.0.1",port=3306,user="root",password="",db="day46",autocommit=True)

调用存储过程

# 创建名为add1的存储过程
delimiter |
create procedure add1(in a int,in b int,out c int)
begin
set c = a + b;
end|
delimiter ;


#pymysql中调用
import pymysql
conn = pymysql.connect(
    host = "127.0.0.1",  #如果是本机 可以忽略
    port = 3306,    # 如果没改过 可以忽略
    user = "root", #必填
    password = "111", #必填
    database = "day42", #必填,
    autocommit=True  # 开启自动提交  不常用....
)
c = conn.cursor(pymysql.cursors.DictCursor)
c.callproc("add1",(1,2,1212)) # @_add1_0  @_add1_1  @_add1_2
c.execute("select @_add1_2")
print(c.fetchone())

# 调用存储过程时,传入参数,会自动定义成变量,
# 命名方式 @_过程的名称_参数的索引 从0开始
原文地址:https://www.cnblogs.com/gaohuayan/p/11201413.html