mysql學習之旅-資料庫自動備份和手動恢複

來源:互聯網
上載者:User

標籤:


1、匯入測試資料庫資料

建庫
show databases;
CREATE DATBASE easthome;
建表
 CREATE TABLE tj(name varchar(8),tel int(8));
CREATE TABLE sex(name varchar(8),sex int(1));
插入資訊
INSERT INTO easthome.tj(name,tel) VALUES(‘LIYAN‘,88888888);
INSERT INTO easthome.tj(name,tel) VALUES(‘zy‘,77777777);
insert into easthome.sex(name,sex) values(‘zy‘,1);
mysql> insert into easthome.sex(name,sex) values(‘LIYAN‘,0);
Query OK, 1 row affected (0.00 sec)
insert into easthome.sex(name,sex) values(‘yangchen‘,1);
 insert into sex(name,sex) values(‘zhangyun‘,0);
mysql> show tables;
+--------------------+
| Tables_in_easthome |
+--------------------+
| sex                |
| tj                 |
+--------------------+
2 rows in set (0.00 sec)

mysql> select * from sex;
+----------+------+
| name     | sex  |
+----------+------+
| zy       |    1 |
| LIYAN    |    0 |
| yangchen |    1 |
| zhangyun |    0 |
+----------+------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM easthome.tj;
+-------+----------+
| name  | tel      |
+-------+----------+
| LIYAN | 88888888 |
| zy    | 77777777 |
+-------+----------+
2 rows in set (0.00 sec)


2、開始備份
mysqldump備份成dump檔案
==============
MySQL   安裝位置:D:\app\mysql57
資料庫名稱為:easthome
MySQL root   密碼:redhat
Database Backup目的地:D:\db_backup\

指令碼:

rem *******************************Code Start*****************************
@echo off
set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%"
D:\app\mysql57\bin\mysqldump.exe  -uroot -predhat --default-character-set=binary -f easthome > D:\db_bakup\easthome_%Ymd%.dump
@echo on
rem *******************************Code End*****************************

將以上代碼儲存為backup_db.bat   調試時可以在結尾加上pause,查看是否正常執行
然後使用Windows的“計劃任務”定時執行該指令碼即可。(例如:每天淩晨5點執行back_db.bat)
說明:此方法可以不用關閉資料庫,並且可以按每一天的時間來名稱備份檔案。
通過%date:~5,2%來組合得出當前日期,組合的效果為yyyymmdd,date命令得到的日期格式預設為yyyy-mm-dd(如果不是此格式可以通過pause命令來暫停命令列視窗看通過%date:~,20%得到的當前電腦日期格式),所以通過%date:~5,2%即可得到日期中的第五個字元開始的兩個字元,例如今天為2009-02-05,通過%date:~5,2%則可以得到02。(日期的字串的下標是從0開始的)

3、匯入到冷備庫中     #在這裡卡了半天,原來mysqldump只是匯出工具,匯入時必須要用mysql

在cmd中登入mysql
mysql -uroot -p
create database mytest
use mytest
source d:\db_bakup\easthome_20160901.dump
show tables;    #檢查是否匯入成功

效果如下:
mysql> source D:\db_bakup\easthome_20160901.dump
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| sex              |
| tj               |
+------------------+
2 rows in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| mytest             |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql學習之旅-資料庫自動備份和手動恢複

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.