myql权限设置,导出数据,python连接
mysql dcl权限设置
set global validate_password_policy=0;
-- 这个参数用于控制validate_password的验证策略 0-->low 1-->MEDIUM 2-->strong。
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 数据导出
数据库中tb_emp表导出到txt文件,此处保存到桌面
select * from tb_emp into outfile '/Users/ajiang/Desktop/emp.txt';
导出到csv文件中
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,再输入导出命令。
导出表作为原始数据(mysqldump)
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()
cursor.fetchone() —> 返回单个的元组,也就是一条记录(row),如果没有结果 则返回 None
cursor.fetchall() —> 返回多个元组,即返回多个记录(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()