MySQL完全備份與恢複

來源:互聯網
上載者:User

標籤:quit   oca   row   query   gis   rpo   mysqld   縮排   sql命令   

MySQL完全備份與恢複完全備份:對整個資料庫的備份、資料庫結構和檔案結構的備份,儲存的是備完成時刻的資料庫,是增量備份的基礎。mysql資料庫的備份可以採用兩種方式,因為資料庫實際上就是檔案,直接打包資料庫檔案夾,或者是使用專用備份工具mysqldump都可以進行備份工作。MySQL完全備份1.使用tar打包檔案夾備份

MySQL的資料庫檔案預設都是儲存在安裝目錄的data檔案夾下面,可以直接儲存data檔案夾,但是佔用的空間較大,可以使用tar打包壓縮排行儲存。

(1)資料庫檔案很大,可以使用壓縮率較大的xz格式壓縮,預設情況下已有,如果沒有需安裝xz壓縮格式工具。

[[email protected] mysql]# yum install xz -y

(2)對資料庫檔案夾/usr/local/mysql/data/進行打包操作。

[[email protected] mysql]# tar Jcvf /opt/mysql-$(date +%F).tar.xz /usr/local/mysql/data/[[email protected] ~]# cd /opt/[[email protected] opt]# lsmysql-2018-07-02.tar.xz  rh    //備份檔案//

(3)如果資料庫檔案損壞資料丟失,可以解壓縮備份檔案,相當於做了資料恢複的工作。

[[email protected] opt]# tar Jxvf /opt/mysql-2018-07-02.tar.xz /usr/local/mysql/data/
2使用mysqldump工具備份

前面介紹的對MySQL整個資料庫目錄壓縮的方式,是備份資料庫中所有的內容。使用mysqldup可以更加靈活地控製備份的內容,比如某幾個表或庫都可以單獨備份。

(1)使用mysqldump命令對庫school中的表info進行備份,備份的檔案是/opt/info.sql

[[email protected] opt]# mysqldump -u root -p school info > /opt/info.sql[[email protected] opt]# lsinfo.sql  mysql-2018-07-02.tar.xz  

(2)使用mysqldump命令對單個庫進行完全備份,備份檔案是/opt/school.sql。

[[email protected] opt]# mysqldump -u root -p school > /opt/school.sqlEnter password:      //root登入密碼//[[email protected] opt]# lsinfo.sql  mysql-2018-07-02.tar.xz  rh  school.sql

(3)使用mysqldump命令對多個庫進行備份,備份檔案是/opt/school-mysql.sql。

[[email protected] opt]# mysqldump -u root -p --databases school mysql > /opt/school-mysql.sql[[email protected] opt]# lsinfo.sql  mysql-2018-07-02.tar.xz  rh  school-mysql.sql  school.sql

(4)使用mysqldump命令對所有庫進行完全備份,備份檔案是all.sql。

[[email protected] opt]# mysqldump -u root -p --all-databases > /opt/all.sql[[email protected] opt]# lsall.sql  info.sql  mysql-2018-07-02.tar.xz  rh  school-mysql.sql  school.sql

(5)使用mysqldump命令也可以直接備份表結構,備份檔案是/opt/desc-info.sql。

[[email protected] opt]# mysqldump -u roou -p school info > /opt/desc-info.sql[[email protected] opt]# lsall.sql  desc-info.sql  info.sql  mysql-2018-07-02.tar.xz  rh  school-mysql.sql  school.sql
MySQL完全恢複

1.恢複整庫操作
(1)首先對庫school進行備份

[[email protected] opt]# mysqldump -u root -p school > /opt/school.sql

(2)假設資料損毀,刪除資料庫school。

[[email protected] opt]# mysql -u root -p   //登入mysql//Enter password:     //登入密碼//Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 17Server version: 5.7.17 Source distributionCopyright (c) 2000, 2016, 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> show databases;     //查看所有庫//+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || school             || sys                |+--------------------+5 rows in set (0.00 sec)mysql> drop database school;     //刪除school庫//Query OK, 0 rows affected (0.00 sec)mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || sys                |+--------------------+4 rows in set (0.00 sec)

(3)不登入mysql,使用mysql命令恢複庫school。

此時庫school已經被刪除了,需要先建立再進行恢複操作,否則會報錯。

[[email protected] opt]# mysql -u root -pmysql> create database school;   //建立庫//Query OK, 1 row affected (0.00 sec)mysql> quit   //退出//Bye[[email protected] opt]# mysql -u root -p school < /opt/school.sql      //恢複school庫//[[email protected] opt]# mysql -u root -p  //登入mysql資料庫//mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || school             || sys                |+--------------------+5 rows in set (0.00 sec)

2.使用source命令恢複表
(1)首先對錶info進行備份

[[email protected] opt]# mysqldump -u root -p school info > /opt/info.sql

(2)假設資料損毀,刪除資料庫school中的表info。

mysql> use school;   //進入school庫//Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;           //查看錶//+------------------+| Tables_in_school |+------------------+| info             |+------------------+1 row in set (0.00 sec)mysql> select * from info;   //查看資料記錄//+----------+-------+| name     | score |+----------+-------+| zhangsan | 88.00 || lisi     | 70.00 |+----------+-------+2 rows in set (0.00 sec)mysql> drop table info;    //刪除info表//Query OK, 0 rows affected (0.02 sec)mysql> show tables;       Empty set (0.00 sec)      //無表//

(3)登入mysql,使用source命令恢複表。

[[email protected] opt]# mysql -u root -pmysql> use school;             //進入庫//mysql> source /opt/info.sql   //恢複info表//Query OK, 0 rows affected (0.00 sec)mysql> show tables;+------------------+| Tables_in_school |+------------------+| info             |+------------------+1 row in set (0.00 sec)mysql> select * from info;+----------+-------+| name     | score |+----------+-------+| zhangsan | 88.00 || lisi     | 70.00 |+----------+-------+2 rows in set (0.00 sec)

(4)也可以使用mysql進行恢複

[[email protected] opt]mysqldump -u root -p school info > /opt/school-info.sql  //備份//[[email protected] opt]mysql -u root -p school < /opt/school-info.sql //恢複//

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.