如何使用python轉(zhuǎn)移mysql數(shù)據(jù)庫中的全部數(shù)據(jù)
數(shù)據(jù)庫到期
今天,有人告訴我,“馬上就要雙十一了,我遇到了一個問題。”
我很好奇,“是什么問題呢?關(guān)于雙十一的商品折扣嗎?”
他說,“不,是我之前雙十一的時候,購買的mysql數(shù)據(jù)庫到期了,但是因為價格較高,我不打算繼續(xù)續(xù)費了,現(xiàn)在希望將其中的數(shù)據(jù)轉(zhuǎn)移出去,該怎么做呢?“
確實,雖然專業(yè)的數(shù)據(jù)庫服務(wù)非常好,但是價格上還是比較貴的,每年可能需要花費數(shù)百元,如果需求量不大,要求不高的情況下,可能確實不如自己安裝一個比較節(jié)省。
那么,將數(shù)據(jù)庫中的數(shù)據(jù)全部遷移出來,通常不是什么困難(尤其是在數(shù)據(jù)量并非巨大的情況下),下面,就介紹幾種方法,可以有效的幫助數(shù)據(jù)的保存與轉(zhuǎn)移。
常規(guī)保存
mysqldump
使用mysqldump工具可以將數(shù)據(jù)庫導(dǎo)出為sql文件
mysqldump -u 用戶名 -p -B 數(shù)據(jù)庫名 > 導(dǎo)出文件.sql
# 可選選項:--set-gtid-purged=OFF 避免備份無關(guān)的全局事務(wù)標識符
# 可選選項:--single-transaction 保證數(shù)據(jù)一致性
mysqldump -uroot -p --set-gtid-purged=OFF --single-transaction -B 數(shù)據(jù)庫名 > 導(dǎo)出文件.sql
在需要恢復(fù)的地方執(zhí)行
mysql -u 用戶名 -p < 導(dǎo)出文件.sql
注意:如果使用mysqldump導(dǎo)出大量數(shù)據(jù)會耗時較長,因此更適合對中小規(guī)模的數(shù)據(jù)庫使用。
將表數(shù)據(jù)寫入文件
查詢某表的所有數(shù)據(jù),并將其寫入到文件中
mysql -u 用戶名 -p -e "SELECT * FROM 數(shù)據(jù)庫名.表名" > "文件名.txt"
使用python保存
python直接轉(zhuǎn)存
對于mysql數(shù)據(jù)庫,可以使用pymysql查詢出其中的所有數(shù)據(jù),然后插入到目標數(shù)據(jù)庫。
import pymysql # 連接源數(shù)據(jù)庫 source_conn = pymysql.connect( host="源數(shù)據(jù)庫地址", user="用戶名", password="密碼", database="源數(shù)據(jù)庫名" ) source_cursor = source_conn.cursor() # 連接目標數(shù)據(jù)庫 target_conn = pymysql.connect( host="目標數(shù)據(jù)庫地址", user="用戶名", password="密碼", database="目標數(shù)據(jù)庫名" ) target_cursor = target_conn.cursor() # 查詢源數(shù)據(jù)庫中的所有表 source_cursor.execute("SHOW TABLES") tables = source_cursor.fetchall() for table in tables: table_name = table[0] source_cursor.execute(f"SELECT * FROM {table_name}") rows = source_cursor.fetchall() source_cursor.execute(f"SHOW CREATE TABLE {table_name}") create_table_sql = source_cursor.fetchone()[1] target_cursor.execute(f"DROP TABLE IF EXISTS {table_name}") target_cursor.execute(create_table_sql) for row in rows: placeholders = ", ".join(["%s"] * len(row)) insert_sql = f"INSERT INTO {table_name} VALUES ({placeholders})" target_cursor.execute(insert_sql, row) target_conn.commit() source_cursor.close() source_conn.close() target_cursor.close() target_conn.close()
python保存到csv文件
將每個數(shù)據(jù)表,分別存到csv文件中。注意,該方法目前只保存了數(shù)據(jù),但是沒有保存數(shù)據(jù)表結(jié)構(gòu)與創(chuàng)建語句。
import pymysql import csv source_conn = pymysql.connect( host="源數(shù)據(jù)庫地址", user="用戶名", password="密碼", database="源數(shù)據(jù)庫名" ) source_cursor = source_conn.cursor() source_cursor.execute("SHOW TABLES") tables = source_cursor.fetchall() for table in tables: table_name = table[0] source_cursor.execute(f"SELECT * FROM {table_name}") rows = source_cursor.fetchall() columns = [desc[0] for desc in source_cursor.description] with open(f"{table_name}.csv", "w", newline="", encoding="utf-8") as file: writer = csv.writer(file) writer.writerow(columns) writer.writerows(rows) source_cursor.close() source_conn.close()
到此這篇關(guān)于如何使用python轉(zhuǎn)移mysql數(shù)據(jù)庫中的全部數(shù)據(jù)的文章就介紹到這了,更多相關(guān)python轉(zhuǎn)移mysql數(shù)據(jù)庫數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python使用writerows寫csv文件產(chǎn)生多余空行的處理方法
這篇文章主要介紹了python使用writerows寫csv文件產(chǎn)生多余空行的處理方法,需要的朋友可以參考下2019-08-08Python使用functools實現(xiàn)注解同步方法
這篇文章主要介紹了Python使用functools實現(xiàn)注解同步方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-02-02Pandas實現(xiàn)(pivot_table函數(shù))數(shù)據(jù)透視表方式
pandas的pivot_table()函數(shù)非常強大,主要用于創(chuàng)建數(shù)據(jù)透視表,重要參數(shù)包括index、values、columns和aggfunc,index用于設(shè)置行索引,類似于SQL中的group by,values用于進行聚合計算的數(shù)據(jù)選擇,columns參數(shù)可設(shè)置列層次,非必須2024-09-09