一、MySQL用户管理
1、为什么要用用户管理?
简述:首先MySQL是一个基于tcp协议socket以成C/S架构的服务器,既然是服务器那么一定有请求但是我们不能什么请求都要响应吧,所以我们必须通过用户管理以用来限制用户请求。
2、怎么用MySQL的用户管理?
简述:
2.1首先创建用户:其步骤如下
2.1.1 先使用mysql库 use mysql:
2.1.2 在通过create user 用户名@'主机地址(客户端ip地址)' identified by " 新密码":完成创建
2.2 为创建的用户授予使用数据库的权限其步骤如下:
2.2.1 第一种:grant all(全部权限)on *.*(所有的库与表)to 用户名@'主机地址' identified by '密码':
第二种:grant select(id,name),insert(id,name)on 库名.*(这个库下的所有表) to 用户名@'主机地址' identified by "密码":
第三种: grant select(id,name),insert(id,name)on 库名.表名(一个库指定的表)to 用户名@'主机地址' identified by "密码":
2.2.2已被授权的用户可以为其他用户授权其有的权限其步骤为:在授予权限的identified by "密码":后加上with grant option
2.3删除已授权的权限其步骤如下:
2.3.1 revoke all on *.* from 用户名@'主机地址':
2.3.2 刷新权限的步骤为 :
flush privileges;
补充:更新权限update mysql.user set grant_priv='Y' where user='用户名' and host='主机地址':
2.4 删除用户的步骤如下:
2.4.1 drop user 用户名@'主机地址':
二、pymysql的用法
import pymysql conn=pymysql.connect( host='127.0.0.1',#主机地址 port=3306, #数据库端口 user='用户名',#数据库用户名 password='密码',#数据库密码 database='库名',#数据库库名 charset='utf8'#数据库中库的编码格式 ) cursor=conn.cursor(pymysql.cursors.DictCursor)#获取游标对象且其中#pymysql.cursors.DictCursor指的是返回值的类型其类型默认为元组,也可为字典类型 res=cursor.exectue('select * from 表名') print(cursor.fetchall())#显示的个数 print(cursor.fetchone()) print(cursor.fetchmany(3)) conn.commit()#提交 conn.rockback()#回滚 cursor.close() conn.close()