I'm using SSHTunnel to connect to Mysql. I'm planning to export all tables from the dataset to CSV. Here is the code
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()
I got the error "2013, 'Lost connection to MySQL server during query'" so is there a way to fix the problem?
I tried to set global max_allowed_packet, but the error "1227, 'Access denied; you need (at least one of) the SUPER privilege(s) for this operation'"
I couldn't tell you why, but I had this problem, and it's related to the SSH tunnel. For some reason, "returning" the tunnel/connection from a method and passing it around doesn't seem to work. I tried just doing everything inside the tunnel & connection in the main method, and that fixed it for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.