我正在使用SSHTunnel连接到Mysql。我打算将所有表从数据集中导出到CSV。这是代码
def OpenSSHTunnel():
global server
server = SSHTunnelForwarder(
('*.*.*.*', 22),
ssh_username="",
ssh_pkey="C:/Users/Administrator/Desktop/***",
remote_bind_address=('*.*.*.*', 3306),
local_bind_address=('0.0.0.0', 10022))
def exportToCsv(db):
conn = pymysql.connect(host='127.0.0.1',
port=10022,
user='**',
passwd='**',
db=db)
cur = conn.cursor()
#cur.execute("set global max_allowed_packet=67108864")
# RETRIEVE TABLES
cur.execute("SHOW TABLES")
tables = []
for row in cur.fetchall():
tables.append(row[0])
for t in tables:
# SELECT STATEMENTS
cur.execute("SELECT * FROM `{}`".format(t))
tempcsv = '{}.csv'.format(t)
rows = cur.fetchall()
fp = open(tempcsv, 'w',newline='',encoding='utf-8')
writer = csv.writer(fp)
writer.writerow([i[0] for i in cur.description]) # COLUMN HEADERS
writer.writerows(rows)
fp.close()
我收到错误消息“ 2013,'在查询过程中失去与MySQL服务器的连接'”,因此有办法解决该问题吗?
我试图设置全局max_allowed_packet,但是错误“ 1227,'访问被拒绝;您需要(至少一种)此操作的SUPER特权'“