MySQL完全备份与恢复
MySQL完全備份與恢復(fù)
完全備份:對整個(gè)數(shù)據(jù)庫的備份、數(shù)據(jù)庫結(jié)構(gòu)和文件結(jié)構(gòu)的備份,保存的是備完成時(shí)刻的數(shù)據(jù)庫,是增量備份的基礎(chǔ)。
mysql數(shù)據(jù)庫的備份可以采用兩種方式,因?yàn)閿?shù)據(jù)庫實(shí)際上就是文件,直接打包數(shù)據(jù)庫文件夾,或者是使用專用備份工具mysqldump都可以進(jìn)行備份工作。
MySQL完全備份
1.使用tar打包文件夾備份
MySQL的數(shù)據(jù)庫文件默認(rèn)都是保存在安裝目錄的data文件夾下面,可以直接保存data文件夾,但是占用的空間較大,可以使用tar打包壓縮進(jìn)行保存。
(1)數(shù)據(jù)庫文件很大,可以使用壓縮率較大的xz格式壓縮,默認(rèn)情況下已有,如果沒有需安裝xz壓縮格式工具。
[root@localhost mysql]# yum install xz -y(2)對數(shù)據(jù)庫文件夾/usr/local/mysql/data/進(jìn)行打包操作。
[root@localhost mysql]# tar Jcvf /opt/mysql-$(date +%F).tar.xz /usr/local/mysql/data/ [root@localhost ~]# cd /opt/ [root@localhost opt]# ls mysql-2018-07-02.tar.xz rh //備份文件//(3)如果數(shù)據(jù)庫文件損壞數(shù)據(jù)丟失,可以解壓縮備份文件,相當(dāng)于做了數(shù)據(jù)恢復(fù)的工作。
[root@localhost opt]# tar Jxvf /opt/mysql-2018-07-02.tar.xz /usr/local/mysql/data/2使用mysqldump工具備份
前面介紹的對MySQL整個(gè)數(shù)據(jù)庫目錄壓縮的方式,是備份數(shù)據(jù)庫中所有的內(nèi)容。使用mysqldup可以更加靈活地控制備份的內(nèi)容,比如某幾個(gè)表或庫都可以單獨(dú)備份。
(1)使用mysqldump命令對庫school中的表info進(jìn)行備份,備份的文件是/opt/info.sql
[root@localhost opt]# mysqldump -u root -p school info > /opt/info.sql [root@localhost opt]# ls info.sql mysql-2018-07-02.tar.xz(2)使用mysqldump命令對單個(gè)庫進(jìn)行完全備份,備份文件是/opt/school.sql。
[root@localhost opt]# mysqldump -u root -p school > /opt/school.sql Enter password: //root登錄密碼// [root@localhost opt]# ls info.sql mysql-2018-07-02.tar.xz rh school.sql(3)使用mysqldump命令對多個(gè)庫進(jìn)行備份,備份文件是/opt/school-mysql.sql。
[root@localhost opt]# mysqldump -u root -p --databases school mysql > /opt/school-mysql.sql [root@localhost opt]# ls info.sql mysql-2018-07-02.tar.xz rh school-mysql.sql school.sql(4)使用mysqldump命令對所有庫進(jìn)行完全備份,備份文件是all.sql。
[root@localhost opt]# mysqldump -u root -p --all-databases > /opt/all.sql [root@localhost opt]# ls all.sql info.sql mysql-2018-07-02.tar.xz rh school-mysql.sql school.sql(5)使用mysqldump命令也可以直接備份表結(jié)構(gòu),備份文件是/opt/desc-info.sql。
[root@localhost opt]# mysqldump -u roou -p school info > /opt/desc-info.sql [root@localhost opt]# ls all.sql desc-info.sql info.sql mysql-2018-07-02.tar.xz rh school-mysql.sql school.sqlMySQL完全恢復(fù)
1.恢復(fù)整庫操作
(1)首先對庫school進(jìn)行備份
(2)假設(shè)數(shù)據(jù)損壞,刪除數(shù)據(jù)庫school。
[root@localhost opt]# mysql -u root -p //登錄mysql// Enter password: //登錄密碼// Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 17 Server 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 its affiliates. Other names may be trademarks of their respective owners.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命令恢復(fù)庫school。
此時(shí)庫school已經(jīng)被刪除了,需要先創(chuàng)建再進(jìn)行恢復(fù)操作,否則會報(bào)錯(cuò)。
[root@localhost opt]# mysql -u root -p mysql> create database school; //創(chuàng)建庫// Query OK, 1 row affected (0.00 sec) mysql> quit //退出// Bye [root@localhost opt]# mysql -u root -p school < /opt/school.sql //恢復(fù)school庫// [root@localhost opt]# mysql -u root -p //登錄mysql數(shù)據(jù)庫// mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | sys | +--------------------+ 5 rows in set (0.00 sec)2.使用source命令恢復(fù)表
(1)首先對表info進(jìn)行備份
(2)假設(shè)數(shù)據(jù)損壞,刪除數(shù)據(jù)庫school中的表info。
mysql> use school; //進(jìn)入school庫// Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -ADatabase changed mysql> show tables; //查看表// +------------------+ | Tables_in_school | +------------------+ | info | +------------------+ 1 row in set (0.00 sec)mysql> select * from info; //查看數(shù)據(jù)記錄// +----------+-------+ | 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命令恢復(fù)表。
[root@localhost opt]# mysql -u root -p mysql> use school; //進(jìn)入庫// mysql> source /opt/info.sql //恢復(fù)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進(jìn)行恢復(fù)
[root@localhost opt]mysqldump -u root -p school info > /opt/school-info.sql //備份// [root@localhost opt]mysql -u root -p school < /opt/school-info.sql //恢復(fù)//轉(zhuǎn)載于:https://blog.51cto.com/13642258/2135312
總結(jié)
以上是生活随笔為你收集整理的MySQL完全备份与恢复的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Swift4.1第二章 The Basi
 - 下一篇: JEPLUS表格组件数据平铺——JEPL