mysql六:数据备份、pymysql模块
閱讀目錄
一 MySQL數(shù)據(jù)備份
二 pymysql模塊
一 MySQL數(shù)據(jù)備份
#1. 物理備份: 直接復(fù)制數(shù)據(jù)庫(kù)文件,適用于大型數(shù)據(jù)庫(kù)環(huán)境。但不能恢復(fù)到異構(gòu)系統(tǒng)中如Windows。 #2. 邏輯備份: 備份的是建表、建庫(kù)、插入等操作所執(zhí)行SQL語(yǔ)句,適用于中小型數(shù)據(jù)庫(kù),效率相對(duì)較低。 #3. 導(dǎo)出表: 將表導(dǎo)入到文本文件中。一、使用mysqldump實(shí)現(xiàn)邏輯備份
?
#語(yǔ)法: # mysqldump -h 服務(wù)器 -u用戶名 -p密碼 數(shù)據(jù)庫(kù)名 > 備份文件.sql#示例: #單庫(kù)備份 mysqldump -uroot -p123 db1 > db1.sql mysqldump -uroot -p123 db1 table1 table2 > db1-table1-table2.sql#多庫(kù)備份 mysqldump -uroot -p123 --databases db1 db2 mysql db3 > db1_db2_mysql_db3.sql#備份所有庫(kù) 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ù)單個(gè)庫(kù)時(shí),可以修改sql文件 DROP database if exists school; create database school; use school;?
三、備份/恢復(fù)案例
#數(shù)據(jù)庫(kù)備份/恢復(fù)實(shí)驗(yàn)一:數(shù)據(jù)庫(kù)損壞 備份: 1. # mysqldump -uroot -p123 --all-databases > /backup/`date +%F`_all.sql 2. # mysql -uroot -p123 -e 'flush logs' //截?cái)嗖a(chǎn)生新的binlog 3. 插入數(shù)據(jù) //模擬服務(wù)器正常運(yùn)行 4. mysql> set sql_log_bin=0; //模擬服務(wù)器損壞 mysql> drop database db;恢復(fù): 1. # mysqlbinlog 最后一個(gè)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ù)最后個(gè)binlog文件#數(shù)據(jù)庫(kù)備份/恢復(fù)實(shí)驗(yàn)二:如果有誤刪除 備份: 1. mysqldump -uroot -p123 --all-databases > /backup/`date +%F`_all.sql 2. mysql -uroot -p123 -e 'flush logs' //截?cái)嗖a(chǎn)生新的binlog 3. 插入數(shù)據(jù) //模擬服務(wù)器正常運(yùn)行 4. drop table db1.t1 //模擬誤刪除 5. 插入數(shù)據(jù) //模擬服務(wù)器正常運(yùn)行恢復(fù): 1. # mysqlbinlog 最后一個(gè)binlog --stop-position=260 > /tmp/1.sql # mysqlbinlog 最后一個(gè)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ù)最后個(gè)binlog文件 mysql> source /tmp/2.log //恢復(fù)最后個(gè)binlog文件注意事項(xiàng): 1. 完全恢復(fù)到一個(gè)干凈的環(huán)境(例如新的數(shù)據(jù)庫(kù)或刪除原有的數(shù)據(jù)庫(kù)) 2. 恢復(fù)期間所有SQL語(yǔ)句不應(yīng)該記錄到binlog中四、實(shí)現(xiàn)自動(dòng)化備份
備份計(jì)劃: 1. 什么時(shí)間 2:00 2. 對(duì)哪些數(shù)據(jù)庫(kù)備份 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# 備份并截?cái)嗳罩?/span> 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 {} \;手動(dòng)測(cè)試: [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 * * * /mysql_back.sql五、表的導(dǎo)出和導(dǎo)入
SELECT... INTO OUTFILE 導(dǎo)出文本文件 示例: mysql> SELECT * FROM school.student1 INTO OUTFILE 'student1.txt' FIELDS TERMINATED BY ',' //定義字段分隔符 OPTIONALLY ENCLOSED BY '”' //定義字符串使用什么符號(hào)括起來(lái) 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'; #可能會(huì)報(bào)錯(cuò) mysql> select * from db1.emp into outfile 'C:\\db1.emp.txt' fields terminated by ',' lines terminated by '\r\n'; ERROR 1238 (HY000): Variable 'secure_file_priv' is a read only variable#數(shù)據(jù)庫(kù)最關(guān)鍵的是數(shù)據(jù),一旦數(shù)據(jù)庫(kù)權(quán)限泄露,那么通過(guò)上述語(yǔ)句就可以輕松將數(shù)據(jù)導(dǎo)出到文件中然后下載拿走,因而mysql對(duì)此作了限制,只能將文件導(dǎo)出到指定目錄 在配置文件中 [mysqld] secure_file_priv='C:\\' #只能將數(shù)據(jù)導(dǎo)出到C:\\下 重啟mysql 重新執(zhí)行上述語(yǔ)句 報(bào)錯(cuò):Variable 'secure_file_priv' is a read only六、數(shù)據(jù)庫(kù)遷移
務(wù)必保證在相同版本之間遷移 # mysqldump -h 源IP -uroot -p123 --databases db1 | mysql -h 目標(biāo)IP -uroot -p456二 pymysql模塊
#安裝 pip3 install pymysql一 鏈接、執(zhí)行sql、關(guān)閉(游標(biāo))
import pymysql user=input('用戶名: ').strip() pwd=input('密碼: ').strip()#鏈接 conn=pymysql.connect(host='localhost',user='root',password='123',database='egon',charset='utf8') #游標(biāo) cursor=conn.cursor() #執(zhí)行完畢返回的結(jié)果集默認(rèn)以元組顯示 #cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)#執(zhí)行sql語(yǔ)句 sql='select * from userinfo where name="%s" and password="%s"' %(user,pwd) #注意%s需要加引號(hào) print(sql) res=cursor.execute(sql) #執(zhí)行sql語(yǔ)句,返回sql查詢成功的記錄數(shù)目 print(res)cursor.close() conn.close()if res:print('登錄成功') else:print('登錄失敗')二 execute()之sql注入
注意:符號(hào)--會(huì)注釋掉它之后的sql,正確的語(yǔ)法:--后至少有一個(gè)任意字符
根本原理:就根據(jù)程序的字符串拼接name='%s',我們輸入一個(gè)xxx' -- haha,用我們輸入的xxx加'在程序中拼接成一個(gè)判斷條件name='xxx' -- haha'
最后那一個(gè)空格,在一條sql語(yǔ)句中如果遇到select * from t1 where id > 3 -- and name='egon';則--之后的條件被注釋掉了#1、sql注入之:用戶存在,繞過(guò)密碼 egon' -- 任意字符#2、sql注入之:用戶不存在,繞過(guò)用戶與密碼 xxx' or 1=1 -- 任意字符解決方法:
# 原來(lái)是我們對(duì)sql進(jìn)行字符串拼接 # sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd) # print(sql) # res=cursor.execute(sql)#改寫(xiě)為(execute幫我們做字符串拼接,我們無(wú)需且一定不能再為%s加引號(hào)了) sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引號(hào),因?yàn)閜ymysql會(huì)自動(dòng)為我們加上 res=cursor.execute(sql,[user,pwd]) #pymysql模塊自動(dòng)幫我們解決sql注入的問(wèn)題,只要我們按照pymysql的規(guī)矩來(lái)。三 增、刪、改:conn.commit()
import pymysql #鏈接 conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') #游標(biāo) cursor=conn.cursor()#執(zhí)行sql語(yǔ)句 #part1 # sql='insert into userinfo(name,password) values("root","123456");' # res=cursor.execute(sql) #執(zhí)行sql語(yǔ)句,返回sql影響成功的行數(shù) # print(res)#part2 # sql='insert into userinfo(name,password) values(%s,%s);' # res=cursor.execute(sql,("root","123456")) #執(zhí)行sql語(yǔ)句,返回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語(yǔ)句,返回sql影響成功的行數(shù) print(res)conn.commit() #提交后才發(fā)現(xiàn)表中插入記錄成功 cursor.close() conn.close()四 查:fetchone,fetchmany,fetchall
?
import pymysql #鏈接 conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') #游標(biāo) cursor=conn.cursor()#執(zhí)行sql語(yǔ)句 sql='select * from userinfo;' rows=cursor.execute(sql) #執(zhí)行sql語(yǔ)句,返回sql影響成功的行數(shù)rows,將結(jié)果放入一個(gè)集合,等待被查詢# cursor.scroll(3,mode='absolute') # 相對(duì)絕對(duì)位置移動(dòng) # cursor.scroll(3,mode='relative') # 相對(duì)當(dāng)前位置移動(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) '''五 獲取插入的最后一條數(shù)據(jù)的自增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) #在插入語(yǔ)句后查看 conn.commit()cursor.close() conn.close()?
?
?
?
?
?
?
?
MySQL系列
第一篇:初識(shí)數(shù)據(jù)庫(kù)
第二篇:庫(kù)操作
第三篇:表操作
第四篇:數(shù)據(jù)操作
第五篇:索引原理與慢查詢優(yōu)化
第六篇:數(shù)據(jù)備份、pymysql模塊
第七篇:視圖、觸發(fā)器、事務(wù)、存儲(chǔ)過(guò)程、函數(shù)
第八篇:ORM框架SQLAlchemy
轉(zhuǎn)載于:https://www.cnblogs.com/shangdelu/p/8478486.html
總結(jié)
以上是生活随笔為你收集整理的mysql六:数据备份、pymysql模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 迅为IMX8MM开发板Linux系统修改
- 下一篇: SQLServer日期格式化