mysql 針對單張表的備份與還原

來源:互聯網
上載者:User

標籤:

A、MySQL 備份工具xtrabackup 的安裝    1. percona 官方xtrabackup 的二進位版本;二進位版本解壓就能用了。    2. 解壓xtrabackup & 建立串連        tar -xzvf percona-xtrabackup-2.3.4-Linux-x86_64.tar.gz -C /usr/local/        ln -s /usr/local/percona-xtrabackup-2.3.4 /usr/local/xtrabackup    3. 設定PATH環境變數        export PATH=/usr/local/xtrabackup/bin/:$PATH        B、在mysql資料庫中建立一個使用者備份的使用者 & 授權    1. 建立使用者        create user [email protected]‘localhost‘ identified by ‘backup123‘;        create user [email protected]‘127.0.0.1‘ identified by ‘backup123‘;    2. 授權        grant reload,lock tables,replication client,process,super on *.* to ‘backuper‘@‘localhost‘;        grant create,insert,select on percona_schema.xtrabackup_history to ‘backuper‘@‘localhost‘;        grant reload,lock tables,replication client,process,super on *.* to ‘backuper‘@‘127.0.0.1‘;        grant create,insert,select on percona_schema.xtrabackup_history to ‘backuper‘@‘127.0.0.1‘;    C、備份前的檢查,這一步的主要目地是在之後做還原作業時,驗證還原是不是有效;(生產是沒有這一步的,    1. select * from tempdb.dict__major;        select * from dict__major;        +--------------+-----------------+        | column_value | column_mean     |        +--------------+-----------------+        |            1 | 漢語言文學      |        |            2 | 精算            |        |            3 | 生物製藥        |        |            4 | 材料化學        |        |            5 | 商務英語        |        |            6 | 考古            |        |            7 | 外交            |        |            8 | 導遊            |        +--------------+-----------------+D、備份tempdb.dict__major 表    1. 備份命令        innobackupex --host=127.0.0.1 --user=backuper --password=backup123 --port=3306 --include=‘tempdb.dict__major‘ /tmp/tempdb            2. 備份完成後會在備份目錄(/tmp/tempdb) 下產生用目前時間命名的目錄,裡面儲存的就是備份檔案        tree /tmp/tempdb/        /tmp/tempdb/        └── 2016-09-10_18-25-16            ├── backup-my.cnf            ├── ibdata1            ├── tempdb            │   ├── dict__major.frm            │   └── dict__major.ibd            ├── xtrabackup_binlog_info            ├── xtrabackup_checkpoints            ├── xtrabackup_info            └── xtrabackup_logfile            E、備份完成後就可以刪除tempdb.dict__major表了(注意這裡一定要儲存一份表的定義,還原時會用到)    mysql>drop table tempdb.dict__major;                F、為了得到一個一致的備份組 在還原作業前還要進行一次日誌的前滾和復原    1. 前滾&復原日誌        innobackupex --apply-log --export /tmp/tempdb/2016-09-10_18-25-16/            2.  與前滾& 復原前的對比        tree /tmp/tempdb/        /tmp/tempdb/        └── 2016-09-10_18-25-16            ├── backup-my.cnf            ├── ibdata1            ├── ib_logfile0            ├── ib_logfile1            ├── tempdb            │   ├── dict__major.cfg            │   ├── dict__major.exp            │   ├── dict__major.frm            │   └── dict__major.ibd            ├── xtrabackup_binlog_info            ├── xtrabackup_binlog_pos_innodb            ├── xtrabackup_checkpoints            ├── xtrabackup_info            └── xtrabackup_logfile            G、還原tempdb.dict__major表    1. 建立 tempdb.dict__major表        create table dict__major(            column_value tinyint not null,            column_mean varchar(32) not null,            constraint pk__dict__major primary key (column_value));    2. 刪除 tempdb.dict__major的資料表空間檔案        alter table tempdb.dict__major discard tablespace;            3. 把備份中的資料表空間檔案複製到tempdb.dict__major 資料表空間應該在的位置        cp /tmp/tempdb/2016-09-10_18-25-16/tempdb/dict__major.ibd /usr/local/mysql/data/tempdb/        cp /tmp/tempdb/2016-09-10_18-25-16/tempdb/dict__major.exp /usr/local/mysql/data/tempdb/        cp /tmp/tempdb/2016-09-10_18-25-16/tempdb/dict__major.cfg /usr/local/mysql/data/tempdb/        chown -R mysql:mysql /usr/local/mysql/data/tempdb/*            4. 匯入資料表空間檔案        alter table tempdb.dict__major import tablespace;            5. 查看dict__major表恢複情況        select * from dict__major;        +--------------+-----------------+        | column_value | column_mean     |        +--------------+-----------------+        |            1 | 漢語言文學      |        |            2 | 精算            |        |            3 | 生物製藥        |        |            4 | 材料化學        |        |            5 | 商務英語        |        |            6 | 考古            |        |            7 | 外交            |        |            8 | 導遊            |        +--------------+-----------------+----------------------------------------------------------------------------------------------------------------------------------------------上一節用的是xtrabackup 對錶進行備份,它的應用情境是單表的資料量大且在備份的過程中還要支援對錶的寫操作;也就是說在目前的情境下mysqldump 這個簡單的備份工具也是可以滿足要求的;現給出mysqldump 備份的一般步驟A:建立備份使用者    1.         create user [email protected]‘127.0.0.1‘ identified by ‘dumper123‘;        grant select on *.* to [email protected]‘127.0.0.1‘;        grant show view on *.* to [email protected]‘127.0.0.1‘;        grant lock tables on *.* to [email protected]‘127.0.0.1‘;        grant trigger on *.* to [email protected]‘127.0.0.1‘;        B:備份tempdb.dict__major表    1.        mysqldump --host=127.0.0.1 --port=3306 --user=dumper --password=dumper123 --quick tempdb dict__major >/tmp/tempdb.dict__major.sql    C: 刪除已經備份的表    1.        mysql>drop table tempdb.dict__major;        D:還原tempdb.dict__major表    1.        mysql -uroot -pxxxxx -h127.0.0.1 -p3306 tempdb </tmp/tempdb.dict__major.sql        E:檢證還原的有效性    1.        select * from dict__major;        +--------------+-----------------+        | column_value | column_mean     |        +--------------+-----------------+        |            1 | 漢語言文學      |        |            2 | 精算            |        |            3 | 生物製藥        |        |            4 | 材料化學        |        |            5 | 商務英語        |        |            6 | 考古            |        |            7 | 外交            |        |            8 | 導遊            |        +--------------+-----------------+

 

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.