如何使用python轉(zhuǎn)移mysql數(shù)據(jù)庫(kù)中的全部數(shù)據(jù)
數(shù)據(jù)庫(kù)到期
今天,有人告訴我,“馬上就要雙十一了,我遇到了一個(gè)問(wèn)題。”
我很好奇,“是什么問(wèn)題呢?關(guān)于雙十一的商品折扣嗎?”
他說(shuō),“不,是我之前雙十一的時(shí)候,購(gòu)買(mǎi)的mysql數(shù)據(jù)庫(kù)到期了,但是因?yàn)閮r(jià)格較高,我不打算繼續(xù)續(xù)費(fèi)了,現(xiàn)在希望將其中的數(shù)據(jù)轉(zhuǎn)移出去,該怎么做呢?“
確實(shí),雖然專業(yè)的數(shù)據(jù)庫(kù)服務(wù)非常好,但是價(jià)格上還是比較貴的,每年可能需要花費(fèi)數(shù)百元,如果需求量不大,要求不高的情況下,可能確實(shí)不如自己安裝一個(gè)比較節(jié)省。
那么,將數(shù)據(jù)庫(kù)中的數(shù)據(jù)全部遷移出來(lái),通常不是什么困難(尤其是在數(shù)據(jù)量并非巨大的情況下),下面,就介紹幾種方法,可以有效的幫助數(shù)據(jù)的保存與轉(zhuǎn)移。
常規(guī)保存
mysqldump
使用mysqldump工具可以將數(shù)據(jù)庫(kù)導(dǎo)出為sql文件
mysqldump -u 用戶名 -p -B 數(shù)據(jù)庫(kù)名 > 導(dǎo)出文件.sql
# 可選選項(xiàng):--set-gtid-purged=OFF 避免備份無(wú)關(guān)的全局事務(wù)標(biāo)識(shí)符
# 可選選項(xiàng):--single-transaction 保證數(shù)據(jù)一致性
mysqldump -uroot -p --set-gtid-purged=OFF --single-transaction -B 數(shù)據(jù)庫(kù)名 > 導(dǎo)出文件.sql
在需要恢復(fù)的地方執(zhí)行
mysql -u 用戶名 -p < 導(dǎo)出文件.sql
注意:如果使用mysqldump導(dǎo)出大量數(shù)據(jù)會(huì)耗時(shí)較長(zhǎng),因此更適合對(duì)中小規(guī)模的數(shù)據(jù)庫(kù)使用。
將表數(shù)據(jù)寫(xiě)入文件
查詢某表的所有數(shù)據(jù),并將其寫(xiě)入到文件中
mysql -u 用戶名 -p -e "SELECT * FROM 數(shù)據(jù)庫(kù)名.表名" > "文件名.txt"
使用python保存
python直接轉(zhuǎn)存
對(duì)于mysql數(shù)據(jù)庫(kù),可以使用pymysql查詢出其中的所有數(shù)據(jù),然后插入到目標(biāo)數(shù)據(jù)庫(kù)。
import pymysql # 連接源數(shù)據(jù)庫(kù) source_conn = pymysql.connect( host="源數(shù)據(jù)庫(kù)地址", user="用戶名", password="密碼", database="源數(shù)據(jù)庫(kù)名" ) source_cursor = source_conn.cursor() # 連接目標(biāo)數(shù)據(jù)庫(kù) target_conn = pymysql.connect( host="目標(biāo)數(shù)據(jù)庫(kù)地址", user="用戶名", password="密碼", database="目標(biāo)數(shù)據(jù)庫(kù)名" ) target_cursor = target_conn.cursor() # 查詢?cè)磾?shù)據(jù)庫(kù)中的所有表 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文件
將每個(gè)數(shù)據(jù)表,分別存到csv文件中。注意,該方法目前只保存了數(shù)據(jù),但是沒(méi)有保存數(shù)據(jù)表結(jié)構(gòu)與創(chuàng)建語(yǔ)句。
import pymysql import csv source_conn = pymysql.connect( host="源數(shù)據(jù)庫(kù)地址", user="用戶名", password="密碼", database="源數(shù)據(jù)庫(kù)名" ) 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ù)庫(kù)中的全部數(shù)據(jù)的文章就介紹到這了,更多相關(guān)python轉(zhuǎn)移mysql數(shù)據(jù)庫(kù)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)戰(zhàn)小游戲飛機(jī)大戰(zhàn)詳解
飛機(jī)大戰(zhàn)想必是很多人童年時(shí)期的經(jīng)典游戲,我們依舊能記得抱個(gè)老人機(jī)娛樂(lè)的場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于如何利用python寫(xiě)一個(gè)簡(jiǎn)單的飛機(jī)大戰(zhàn)小游戲的相關(guān)資料,需要的朋友可以參考下2021-11-11python使用writerows寫(xiě)csv文件產(chǎn)生多余空行的處理方法
這篇文章主要介紹了python使用writerows寫(xiě)csv文件產(chǎn)生多余空行的處理方法,需要的朋友可以參考下2019-08-08python爬蟲(chóng)beautiful?soup的使用方式
這篇文章主要介紹了python爬蟲(chóng)beautiful?soup的使用方式,Beautiful?Soup依據(jù)給定的解釋器來(lái)解析html文檔,其依據(jù)html中標(biāo)簽把html文檔在內(nèi)存中轉(zhuǎn)化為類似于二叉樹(shù)的數(shù)據(jù)結(jié)構(gòu),并通過(guò)實(shí)現(xiàn)的查詢方法來(lái)查詢二叉樹(shù)以得到我們想要的爬蟲(chóng)數(shù)據(jù)2022-08-08Python+Pytorch實(shí)戰(zhàn)之彩色圖片識(shí)別
這篇文章主要為大家詳細(xì)介紹了如何利用Python+Pytorch實(shí)現(xiàn)彩色圖片識(shí)別功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-09-09Python使用functools實(shí)現(xiàn)注解同步方法
這篇文章主要介紹了Python使用functools實(shí)現(xiàn)注解同步方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-02-02Pandas實(shí)現(xiàn)(pivot_table函數(shù))數(shù)據(jù)透視表方式
pandas的pivot_table()函數(shù)非常強(qiáng)大,主要用于創(chuàng)建數(shù)據(jù)透視表,重要參數(shù)包括index、values、columns和aggfunc,index用于設(shè)置行索引,類似于SQL中的group by,values用于進(jìn)行聚合計(jì)算的數(shù)據(jù)選擇,columns參數(shù)可設(shè)置列層次,非必須2024-09-09