辛星解讀使用mysqldump來進行熱備份,辛星mysqldump

來源:互聯網
上載者:User

辛星解讀使用mysqldump來進行熱備份,辛星mysqldump

    備份的重要性就不言而喻了,特別是對於資料庫管理員來說,備份尤其重要。備份有好多種分法,這裡我也並不是很像把他們全部說完,這裡大致說一下熱備份和冷備份。如果關閉服務的話,就可以進行冷備份了,如果是MyISAM引擎,則直接備份datadir裡面的資料檔案即可,一旦有了災難,直接恢複資料即可,InnoDB的話需要處理的東西稍微多一些,比如ibdata檔案、*.frm檔案盒my.cnf(在windows下是my.ini),設定innodb_data_file_path來制定原先的ibdata路徑。

     上面簡單介紹了下冷備份,再說一下熱備份,如果大家去搜一下熱備份,大部分搜到的都是主從備份和雙機備份,我承認,這些技術都比較重要,但是,我們這裡介紹的是本機備份,對於雙機備份和主從備份,我們後面再介紹。

      對於熱備份工具,我們可以使用mysql內建的mysqldump,當然還可以藉助xtrabackup等第三方備份工具,這種專門的備份工具就好用的多了,不過我們還是先瞭解下mysqldump。我們使用mysqldump進行備份的時候,它會進行鎖表,我們的應用無法向資料庫進行寫操作,如果我們的MyISAM表比較多,我們使用mysqldump也是一個非常不錯的選擇,下面我們看一下這個命令應該怎麼用:

C:\Users\Administrator>mysqldump -u root -proot mysql > D:\my.sqlmysqldump: [Warning] Using a password on the command line interface can be insecure.C:\Users\Administrator>

這裡我解釋一下吧,這個mysqldump就是這個命令了,這裡我們仍然需要使用使用者和密碼的,否則會報如下錯誤:

C:\Users\Administrator>mysqldump  mysql > D:\an.sqlmysqldump: Got error: 1045: Access denied for user 'ODBC'@'localhost' (using password: NO) when trying to connect

它會提示我們許可權不足,無法操作。我們第一個命令中使用了mysqldump之後使用root使用者來備份,我們備份的是mysql資料庫,然後使用>來吧資料寫入到D盤下的my.sql中,這裡的>是重新導向的意思,而且是覆蓋寫,我想有Linux基礎的這個應該都懂的吧。

   然後我們會發現果然在D盤下多了一個my.sql檔案,當然我這裡的內容還是蠻多的,足足有562KB,下面我僅僅摘抄它的前幾行內容,給大家看一下我們匯出的都是些什麼東西:

-- MySQL dump 10.13  Distrib 5.7.3-m13, for Win64 (x86_64)---- Host: localhost    Database: mysql-- -------------------------------------------------------- Server version5.7.3-m13/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;/*!40101 SET NAMES utf8 */;/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;/*!40103 SET TIME_ZONE='+00:00' */;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;---- Table structure for table `columns_priv`--DROP TABLE IF EXISTS `columns_priv`;/*!40101 SET @saved_cs_client     = @@character_set_client */;/*!40101 SET character_set_client = utf8 */;CREATE TABLE `columns_priv` (  `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',  `Db` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',  `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',  `Table_name` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',  `Column_name` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',  `Timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  `Column_priv` set('Select','Insert','Update','References') CHARACTER SET utf8 NOT NULL DEFAULT '',  PRIMARY KEY (`Host`,`Db`,`User`,`Table_name`,`Column_name`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Column privileges';/*!40101 SET character_set_client = @saved_cs_client */;

   我們發現,其實匯出的並不是純資料,而是一些SQL語句,而且還帶有注釋。

    當然了,我們喜歡敲命令列的都知道,它一般會可以跟好幾個參數,沒錯,我們直接敲mysqldump然後斷行符號可以看到它的常見用法:

C:\Users\Administrator>mysqldumpUsage: mysqldump [OPTIONS] database [tables]OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]OR     mysqldump [OPTIONS] --all-databases [OPTIONS]For more options, use mysqldump --helpC:\Users\Administrator>

   這裡我怕大家理解不是很清楚,就特意來解釋下吧,第一個是說我們可以匯出一個資料庫,也可以匯出一個資料庫下的表,第二個是說我們可以匯出n多個資料庫,但是需要加上--databases來說明,第三個則直接匯出所有資料庫。

     既然有備份,就會有恢複,我們使用mysql命令即可,比如我建立一個an資料庫,它使用咱們備份的檔案中的資料,咱們看下面操作,我就不單個示範了,這裡咱們的資料庫an就拿到了我們的備份資料,順便說一下,這個資料庫an必須提前建好,它不具有自己建立新資料庫的能力:

C:\Users\Administrator>mysql -u root -proot an < D:\my.sqlmysql: [Warning] Using a password on the command line interface can be insecure.C:\Users\Administrator>mysql -u root -prootmysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 124Server version: 5.7.3-m13 MySQL Community Server (GPL)Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> use anDatabase changedmysql> show tables;+---------------------------+| Tables_in_an              |+---------------------------+| columns_priv              || db                        || event                     || func                      || general_log               || help_category             || help_keyword              || help_relation             || help_topic                || innodb_index_stats        || innodb_table_stats        || ndb_binlog_index          || plugin                    || proc                      || procs_priv                || proxies_priv              || servers                   || slave_master_info         || slave_relay_log_info      || slave_worker_info         || slow_log                  || tables_priv               || time_zone                 || time_zone_leap_second     || time_zone_name            || time_zone_transition      || time_zone_transition_type || user                      |+---------------------------+28 rows in set (0.00 sec)mysql>




 




相關文章

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.