zoukankan      html  css  js  c++  java
  • mysql 用户管理 pymysql模块

    mysql用户管理:

      mysql是一个tcp服务器用于操作服务器上的文件数据

      在mysql自带的mysql数据库中有4个表用于用户管理的

      分别是:优先级从高到低

      user -> db -> tables_priv -> columns_priv

      

    1.创建用户的语句

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

      create user 用户名@'192.168.1.%' identified by '123';

      create user 用户名@'%' identified by '123';

      create user db1@“127.0.0.1” identified by “123”;

      这里的主机地址不是服务器地址而是表示这个账户可以在哪台电脑上登陆 %表示所有都可以登录

    2.授权的语句:对文件夹,对文件,对文件某一字段的权限

      常用权限:select update delete alter

      all 可以代表除了 geant 之外的所有权限

      语法:grant [权限的名称 select insert delete ......|all] on 数据库.表名 to 用户名@主机地址;

      grant all on *.* to db1@'localhost'; 可以访问所有库和表

      grant all on test.* to db1@'localhost'; 可以访问test库的所有表

      grant all on test.table1 to db1@'localhost';可以访问test库的table1表

      grant select(id,name),insert(id,name) on test.table1 to db1@'localhost'; 仅能查看和添加 test库的table1表中的id和name字段

      

      grant [权限的名称 select insert.... | all ] on 数据库.表名 to 用户名@主机地址 with grant option;

      with grant option 这个用户可以将他有的权限授予给别的用户

      特点:如果授权时用户不存在直接自动创建用户,这是grant的特点

    3.删除权限

      revoke 权限的名称 on 数据库.表名 from 用户名@"主机

    pymysql使用步骤
        核心类Connect链接和Cursor读写
        1.与数据库服务器建立链接
        2.获取游标对象(用于发送和接受数据)
        3.用游标执行sql语句
        4.使用fetch方法来获取执行的结果
        5.关闭链接,先关游标,再关链接
        
        游标的常用方法
        1.创建游标 conn.cursor(指定查询结果的数据类型)
        2.excute 执行sql语句
        3.fetchone(当sql只有一条记录时) 
           fetchmany(sql有多条并且需要指定条数)    
           fetchall (多条)
        4.scroll 用于修改游标的当前位置
        
        pymysql默认不提交修改(指的是对于表中的记录操作不提交) 删库 删表 是无法撤销的
    
    import pymysql
    
    conn = pymysql.Connect(
        host="127.0.0.1",  #链接
        user="root",     #用户
        password="123",    #密码
        database="home",   #数据库名称
        port=3306,    #端口号 可选
        charset="utf8" #编码 可选
    )
    
    
    #获取游标对象  pymysql.cursors.DictCursor指定返回结果为字典 默认为元组类型
    cursor=conn.cursor(pymysql.cursors.DictCursor)
    
    #查询数据
     sql="select * from student"
    
    #执行sql语句
    #如果是select 语句返回的是查询的条数
     res=cursor.execute(sql)
    
     print(res)
    #获取查询的结果
     print(cursor.fetchall()) #获取所有
     print(cursor.fetchmany(2)) # 括号内指定获取多少个值就获取多少个 不填写默认一个
     print(cursor.fetchmany(2)) # 第二次基于上一次的光标位置向下获取
     print(cursor.fetchone())  #获取一个值 第一次获取第一个
     print(cursor.fetchone())  #第二次获取第二个值
     cursor.scroll(1,"absolute") #从文件的开头挪动一个位置 跳过表的第一条数据
     print(cursor.fetchall())
    
    
     print(cursor.fetchone())
     cursor.scroll(-1)
     print(cursor.fetchone()) #这里获取的值和上一次fetchone获取的一样
    
    try:
        cursor.execute("update student set gender='男' where sname='理解'")
    
        #如果出错没有修改成功 就会回滚 ,比如 网络信号差 突然断电等等 出现错误的时候
        cursor.execute("update class set gender='女' where sname='钢蛋'")
        conn.commit() #对于表中的记录操作 默认是不提交的 需要用commit 来提交修改
    except:
        conn.rollback() #rollback 回滚
    
    
    #关闭链接
    cursor.close()
    conn.close()
    

    名" ;
      revoke all on *.* from scote@"localhost";

      update mysql.user set Grant_priv = "N" where user ="scote" and host = "localhost";

    4.刷新权限表

       flush privileges;

    5.删除用户

      drop user 用户名@"主机地址";

    pymysql模块

      
  • 相关阅读:
    C#的注释
    为知笔记发布到博客,css设置
    eclipse创建springboot项目的三种方法
    创建maven父项目以及子项目
    C# 快捷键(总结)
    idea 项目转 eclipse项目
    freemarker常见语法大全
    Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot
    Spring mvc请求处理流程详解(一)之视图解析
    React+SpringBoot项目部署
  • 原文地址:https://www.cnblogs.com/layerluo/p/9665050.html
Copyright © 2011-2022 走看看