数据备份、pymysql模块
生活随笔
收集整理的這篇文章主要介紹了
数据备份、pymysql模块
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
----------mysql數(shù)據(jù)備份-------------
?
?
#1. 物理備份: 直接復(fù)制數(shù)據(jù)庫文件,適用于大型數(shù)據(jù)庫環(huán)境。但不能恢復(fù)到異構(gòu)系統(tǒng)中如Windows。 #2. 邏輯備份: 備份的是建表、建庫、插入等操作所執(zhí)行SQL語句,適用于中小型數(shù)據(jù)庫,效率相對較低。 #3. 導(dǎo)出表: 將表導(dǎo)入到文本文件中。?
一、使用mysql實現(xiàn)邏輯備份
#語法: # mysqldump -h 服務(wù)器 -u用戶名 -p密碼 數(shù)據(jù)庫名 > 備份文件.sql#示例: #單庫備份 mysqldump -uroot -p123 db1 > db1.sql mysqldump -uroot -p123 db1 table1 table2 > db1-table1-table2.sql#多庫備份 mysqldump -uroot -p123 --databases db1 db2 mysql db3 > db1_db2_mysql_db3.sql#備份所有庫 mysqldump -uroot -p123 --all-databases > all.sql?
二、恢復(fù)邏輯備份
#方法一: [root@egon backup]# mysql -uroot -p123 < /backup/all.sql#方法二: mysql> use db1; mysql> SET SQL_LOG_BIN=0; mysql> source /root/db1.sql#注:如果備份/恢復(fù)單個庫時,可以修改sql文件 DROP database if exists school; create database school; use school;?
?三、備份/恢復(fù)案例
#數(shù)據(jù)庫備份/恢復(fù)實驗一:數(shù)據(jù)庫損壞 備份: 1. # mysqldump -uroot -p123 --all-databases > /backup/`date +%F`_all.sql 2. # mysql -uroot -p123 -e 'flush logs' //截斷并產(chǎn)生新的binlog 3. 插入數(shù)據(jù) //模擬服務(wù)器正常運行 4. mysql> set sql_log_bin=0; //模擬服務(wù)器損壞 mysql> drop database db;恢復(fù): 1. # mysqlbinlog 最后一個binlog > /backup/last_bin.log 2. mysql> set sql_log_bin=0; mysql> source /backup/2014-02-13_all.sql //恢復(fù)最近一次完全備份 mysql> source /backup/last_bin.log //恢復(fù)最后個binlog文件#數(shù)據(jù)庫備份/恢復(fù)實驗二:如果有誤刪除 備份: 1. mysqldump -uroot -p123 --all-databases > /backup/`date +%F`_all.sql 2. mysql -uroot -p123 -e 'flush logs' //截斷并產(chǎn)生新的binlog 3. 插入數(shù)據(jù) //模擬服務(wù)器正常運行 4. drop table db1.t1 //模擬誤刪除 5. 插入數(shù)據(jù) //模擬服務(wù)器正常運行恢復(fù): 1. # mysqlbinlog 最后一個binlog --stop-position=260 > /tmp/1.sql # mysqlbinlog 最后一個binlog --start-position=900 > /tmp/2.sql 2. mysql> set sql_log_bin=0; mysql> source /backup/2014-02-13_all.sql //恢復(fù)最近一次完全備份 mysql> source /tmp/1.log //恢復(fù)最后個binlog文件 mysql> source /tmp/2.log //恢復(fù)最后個binlog文件注意事項: 1. 完全恢復(fù)到一個干凈的環(huán)境(例如新的數(shù)據(jù)庫或刪除原有的數(shù)據(jù)庫) 2. 恢復(fù)期間所有SQL語句不應(yīng)該記錄到binlog中 View Code?
?四、實現(xiàn)自動化備份
備份計劃: 1. 什么時間 2:00 2. 對哪些數(shù)據(jù)庫備份 3. 備份文件放的位置備份腳本: [root@egon ~]# vim /mysql_back.sql #!/bin/bash back_dir=/backup back_file=`date +%F`_all.sql user=root pass=123if [ ! -d /backup ];then mkdir -p /backup fi# 備份并截斷日志 mysqldump -u${user} -p${pass} --events --all-databases > ${back_dir}/${back_file} mysql -u${user} -p${pass} -e 'flush logs'# 只保留最近一周的備份 cd $back_dir find . -mtime +7 -exec rm -rf {} \;手動測試: [root@egon ~]# chmod a+x /mysql_back.sql [root@egon ~]# chattr +i /mysql_back.sql [root@egon ~]# /mysql_back.sql 配置cron: [root@egon ~]# crontab -l 2 * * * /mysql_back.sql View Code?
五、表的導(dǎo)出和導(dǎo)入
SELECT... INTO OUTFILE 導(dǎo)出文本文件 示例: mysql> SELECT * FROM school.student1 INTO OUTFILE 'student1.txt' FIELDS TERMINATED BY ',' //定義字段分隔符 OPTIONALLY ENCLOSED BY '”' //定義字符串使用什么符號括起來 LINES TERMINATED BY '\n' ; //定義換行符mysql 命令導(dǎo)出文本文件 示例: # mysql -u root -p123 -e 'select * from student1.school' > /tmp/student1.txt # mysql -u root -p123 --xml -e 'select * from student1.school' > /tmp/student1.xml # mysql -u root -p123 --html -e 'select * from student1.school' > /tmp/student1.html LOAD DATA INFILE 導(dǎo)入文本文件 mysql> DELETE FROM student1; mysql> LOAD DATA INFILE '/tmp/student1.txt' INTO TABLE school.student1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '”' LINES TERMINATED BY '\n'; View Code?
?六、數(shù)據(jù)庫遷移
?
務(wù)必保證在相同版本之間遷移 # mysqldump -h 源IP -uroot -p123 --databases db1 | mysql -h 目標(biāo)IP -uroot -p456?
?
?
? ? ? ? ? ? ? ------------------pymysql模塊----------------------?
#安裝 pip3 install pymysql import pymysql try:# 1.conn是一個表示連接的對象conn = pymysql.connect(host="127.0.0.1",port = 3306,user = "root",password = "123321",database = "testDB")print("連接服務(wù)器成功!")#2. 查詢數(shù)據(jù) 需要借助cursor類 游標(biāo) 默認(rèn)游標(biāo)返回值類型為元組# pymysql.cursors.DictCursor 可以將結(jié)果轉(zhuǎn)為字典cursor = conn.cursor(pymysql.cursors.DictCursor)# 3.執(zhí)行sql語句sql = "select *from user;"res = cursor.execute(sql) #res為本次查詢得到的記錄條數(shù)print(res)# 4.提取數(shù)據(jù)print(cursor.fetchall()) # 提取本次查詢所有結(jié)果# print(cursor.fetchone()) # 提取本次查詢的第一條記錄# print(cursor.fetchone()) # 提取本次查詢的第一條記錄# print(cursor.fetchmany(2)) # 指定提取記錄的條數(shù)# 如果游標(biāo)已經(jīng)到達(dá)最后 將無法在讀取到數(shù)據(jù) 可以使用scroll來移動游標(biāo)位置cursor.scroll(-1,mode="relative")# mode參數(shù)表示 是相對位置relative 還是絕對位置 absoluteprint(cursor.fetchall()) # 提取本次查詢所有結(jié)果except Exception as e:print(type(e),e) finally:# 無論是否執(zhí)行成功 最后都需要關(guān)閉連接if cursor:cursor.close() # 關(guān)閉游標(biāo)if conn:conn.close() # 關(guān)閉連接?
?
?一、excute()之sql注入
?
登錄 客戶端在網(wǎng)頁輸入用戶名和密碼 瀏覽器要把接收的數(shù)據(jù)傳給后臺服務(wù)器 再由后臺服務(wù)器 交給數(shù)據(jù)庫服務(wù)器可以會遇到sql注入攻擊 什么sql注入攻擊 一些不法分子 可能會在輸入的數(shù)據(jù)中 添加一系列sql語句 來跳過認(rèn)證環(huán)節(jié) 甚至直接刪除數(shù)據(jù)解決方案1.在客戶端接收數(shù)據(jù)時 做一個re的判斷 如果包含sql相關(guān)的字符 就直接報錯2.在服務(wù)器收到某一個客戶端 發(fā)送的數(shù)據(jù)時 做一個判斷其實pymysql 已經(jīng)封裝好了相關(guān)的判斷邏輯 只要將參數(shù)交給pymysql來拼接即可 import pymysql# 1.conn是一個表示連接的對象 conn = pymysql.connect(host="127.0.0.1",port = 3306,user = "root",password = "123321",database = "testDB") print("連接服務(wù)器成功!") # 原來是我們對sql進(jìn)行字符串拼接 # sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd) # print(sql) # res=cursor.execute(sql)#改寫為(execute幫我們做字符串拼接,我們無需且一定不能再為%s加引號了) sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引號,因為pymysql會自動為我們加上 res=cursor.execute(sql,[user,pwd]) #pymysql模塊自動幫我們解決sql注入的問題,只要我們按照pymysql的規(guī)矩來。?
?
?
?
?二、增、刪、改:conn.commit()
import pymysql #鏈接 conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') #游標(biāo) cursor=conn.cursor()#執(zhí)行sql語句 #part1 # sql='insert into userinfo(name,password) values("root","123456");' # res=cursor.execute(sql) #執(zhí)行sql語句,返回sql影響成功的行數(shù) # print(res)#part2 # sql='insert into userinfo(name,password) values(%s,%s);' # res=cursor.execute(sql,("root","123456")) #執(zhí)行sql語句,返回sql影響成功的行數(shù) # print(res)#part3 sql='insert into userinfo(name,password) values(%s,%s);' res=cursor.executemany(sql,[("root","123456"),("lhf","12356"),("eee","156")]) #執(zhí)行sql語句,返回sql影響成功的行數(shù) print(res)conn.commit() #提交后才發(fā)現(xiàn)表中插入記錄成功 cursor.close() conn.close() View Code?
?
三、查:fetchone, fetchmany, fetcall
import pymysql #鏈接 conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') #游標(biāo) cursor=conn.cursor()#執(zhí)行sql語句 sql='select * from userinfo;' rows=cursor.execute(sql) #執(zhí)行sql語句,返回sql影響成功的行數(shù)rows,將結(jié)果放入一個集合,等待被查詢# cursor.scroll(3,mode='absolute') # 相對絕對位置移動 # cursor.scroll(3,mode='relative') # 相對當(dāng)前位置移動 res1=cursor.fetchone() res2=cursor.fetchone() res3=cursor.fetchone() res4=cursor.fetchmany(2) res5=cursor.fetchall() print(res1) print(res2) print(res3) print(res4) print(res5) print('%s rows in set (0.00 sec)' %rows)conn.commit() #提交后才發(fā)現(xiàn)表中插入記錄成功 cursor.close() conn.close()''' (1, 'root', '123456') (2, 'root', '123456') (3, 'root', '123456') ((4, 'root', '123456'), (5, 'root', '123456')) ((6, 'root', '123456'), (7, 'lhf', '12356'), (8, 'eee', '156')) rows in set (0.00 sec) ''' View Code?
?
四、獲取插入的最后一條自增ID
import pymysql conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') cursor=conn.cursor()sql='insert into userinfo(name,password) values("xxx","123");' rows=cursor.execute(sql) print(cursor.lastrowid) #在插入語句后查看 conn.commit()cursor.close() conn.close() View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/pdun/p/10561751.html
總結(jié)
以上是生活随笔為你收集整理的数据备份、pymysql模块的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BUUCTF-- Linux Labs
- 下一篇: Redis数据结构之简单动态字符串SDS