myql权限设置,导出数据,python连接
mysql dcl权限设置
set global validate_password_policy=0; -- 该参数用于控制validate_password的验证策略 0-->low 1-->MEDIUM 2-->label。 et global validate_password_length=6; -- 这里设置密码长度的最小值(如果这个值最小为4),这里是6 -- validate_password_number_count 密码中最小数字。 -- validate_password_mixed_case_count大小写的最小数字。 -- validate_password_special_char_count 最小特殊字符数。 -- validate_password_dictionary_file 字典文件 -- 可通过以下设置查看密码; SHOW VARIABLES LIKE 'validate_password%'
所有操作都可以先创建用户或使用现有用户root用户下操作
这里新建了一个叫王大锤的用户,可以通过任何客户机登录,密码为123456
create user 'wangdachui'@'%' identified by '123456';
创建一个只能本机的用户
create user 'wangdachui'@'localhost' identified by '123456';
删除用户操作
drop user if exists 'wangdachui'@'%';
修改用户密码
alter user 'wangdachui'@'%' identified by 'Wang.618';
授予用户权限(可根据实际情况授权)
grant select on hrs.* to 'wangdachui'@'%';/*查询hrs数据库权限*/ grant insert on hrs.* to 'wangdachui'@'%';/*插入权限*/ grant all privileges on *.* to 'wangdachui'@'%'; grant all privileges on *.* to 'wangdachui'@'%' with grant option;/*所有数据库,所有权限*/
with grant option的作用
对象的owner赋予用户权限(如:testuser1)
grant select ,update on hrs.* to user1 [with grant option ] 1.如果带了 with grant option 那么用户user1可以将select ,update权限传递给其他用户( 如user2) grant select,update on hrs.* to user2 2.如果没带with grant option 那么用户user1不能给user2授权
简单地说,将权限传递给第三方
mysql 数据导出
select * from tb_emp into outfile '/Users/ajiang/Desktop/emp.txt';
select * from tb_emp into outfile '/Users/Hao/Desktop/emp2.csv' fields terminated by ',' lines terminated by '\n';
这里如遇权限问题,如:Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
解决方案如下:
1.找你的安装目录下的一个my.ini 文件修改secure_file_priv的值
(如未在安装目录下找到相关文件,请查看programData目录的mysql的mysql server 里面有没有)
2.打开文件找到secure_file_priv=“C:/ProgramData/MySQL/MySQL Server 8.0/Uploads”修改为,secure_file_priv=““;即可
3.重启服务mysql,然后输入此处导出命令
如果这里有权限问题,没有读写权限,可以尝试服务-mysql-属性-登录-本地系统账户,然后尝试重启mysql,输入导出命令。
)
1.命令行出输入mysqldump验证是否成功(直接在命令行验证不是mysql下面)
如果失败,可能是环境变量问题,可以通过控制面板-系统-高级系统设置-环境变量-系统变量找到path,直接将C:\Program\MySQL\MySQL Server 8.0\bin\添加到最后,可根据自己安装的路径加入
2导出数据,也直接在命令行输入,这里导出数据库hrs下面的tb_dept,在当前文件下创建emp.txt需要输入文件并保存数据root密码
mysqldump -u root -p hrs tb_dept > emp.txt
mysqldump -u root -p hrs tb_dept > emp.sql
mysqldump -u root -p hrs > emp.txt
python连接到数据库
需要安装的第三方库pymysql,cryptography
pip install pymysql pip install cryptography
:1.建立连接对象
2.通过连接对象或去游标对象
3.通过游标对象项数据库发出sql句子获得执行结果
4.操作成功,手动提交,炒作失败,手动回滚
5.操作完成,关闭连接,释放资源
eg:接入MySQL实现数据库insert,drop,update操作
# 接入MySQL实现数据库insert,drop,update操作 import pymysql dept_no =int(input(部门号:
)
) dept_name
=
input
(
'部门名称:'
) dept_location
=
input
(
'所在地:'
)
# 1.建立链接---Connection conn
= pymysql
.connect
(host
=
'localhost'
, port
=
3306
, user
=
'guest'
, password
=
'123456'
, database
=
'hrs'
, charset
=
'utf8mb4'
)
try
:
# 2.通过连接对象获取游标对象 ---> Cursor
with conn
.cursor
(
)
as cursor
:
# dir()查看对象的属性
# print(dir(cursor))
# print(cursor)
# 3.通过游标对象向数据库发出sql语句获取执行结果 affected_rows
= cursor
.execute
(
'insert into tb_dept(dno, dname, dloc) value(%s, %s, %s)'
,
(dept_no
, dept_name
, dept_location
)
)
# ffected_rows = cursor.execute('DELETE FROM `hrs`.`tb_dept` WHERE (`dno` = "50")')
# affected_rows = cursor.execute('update tb_dept set dloc = %s where dno = %s ',(dept_location,dept_no))
# (%s, %s, %s) 安全占位符
if affected_rows
==
1
:
print
(
'新增部门成功'
)
# print('删除部门成功')
# print('更新部门成功')
# 4.如果操作成功,手动提交 conn
.commit
(
)
# 数据持久化,手动提交
except pymysql
.MySQLError
as err
:
print
(err
)
# 4. 如果操作失败,手动回滚 conn
.rollback
(
)
finally
:
# 5.操作完成,关闭连接,释放资源 conn
.close
(
)
—> 返回单个的元组,也就是一条记录(row),如果没有结果 则返回 None —> 返回多个元组,即返回多个记录(rows),如果没有结果 则返回 (),注意再MySQL中是null,再python则是None cursor.fecthmany(n) —>抓取指定个数数据,不推荐使用
eg:接入MySQL数据库,实现select操作
import pymysql
from pymysql.cursors import Cursor
# 1. 建立连接 ---> Connection
conn = pymysql.connect(host='localhost', port=3306,
user='guest', password='123456',
database='hrs', charset='utf8mb4')
try:
# 2. 通过连接对象获取游标对象 ---> Cursor
with conn.cursor() as cursor: # type: Cursor
# 3. 通过游标对象向数据库发出SQL语句获得执行结果
cursor.execute(
'select dno, dname, dloc from tb_dept'
)
# 4. 通过游标对象抓取数据
# fetchone / fetchall / fetchmany
while row := cursor.fetchone():
print(row)
except pymysql.MySQLError as err:
print(err)
finally:
# 5. 关闭连接,释放资源
conn.close()