MySQL Database full backup and recovery

Source: Internet
Author: User

The importance of database backup
    1. Improve the system's high availability and disaster recoverability, when the database system crashes, no data backup can not find data.
    2. Restoring a database using a database backup is the best solution for minimizing the cost of data recovery when a database crashes.
    3. Without a database there is nothing, database backup is a powerful means of disaster prevention.
1. Full Physical backup
1.1使用tar打包文件夹备份 数据库压缩时会使用压缩率较大的xz格式压缩。      rpm -q xz              //检查xz软件包是否安装      yum -y install xz    //安装xz软件包1.2对数据库文件夹/usr/local/mysql/data/ 压缩打包     tar Jcvf /opt/mysql-$(date +%F).tar.xz /usr/local/mysql/data/        //备份数据库文件夹,并以年月日的形式命名1.3如果数据库文件损失,可以解压缩备份文件进行恢复    tar Jxvf /opt/mysql-2018-08-01.tar.xz /usr/local/mysql/data/
2. Use the Mysqldump tool to back up
 2.1 备份某个数据库   mysqldump -u root -p[密码] [选项] [数据库名] [表名] > /备份路径/备份文件名   //备份库到目录下,-p之后可跟密码,也可不写。不写就在回车之后输入密码验证。 备份文件后缀都为.sql, 2.2创建一个数据库school,对school库的info1、info2表备份   mysqldump -u root -p123123 school info1 info2 > /opt/info.sql 2.3备份多个数据库   mysqldump -u root -p --databases mysql stady > /opt/mysql-stady.sql2.4备份所有数据库   mysqldump -u root -p --opt --all-databases > /opt//all.sql2.5备份数据库中的表   mysqldump -u root -p stady info > /opt/stady-info.sql   2.6备份数据库的表结构   mysqldump -u root -p -d stady info > /opt/decribe-info.sql         // -d  库名+表名
3. Recovering a Database
3.1登录Mysql恢复    drop database school;   //先删除损坏的school库    source /opt/school.sql使用mysql命令恢复    drop database school;    //先登录mysql删除损坏的school库    create database school;  //再重新创建库    exit;   //退出mysql    mysql -u root -p123123 school < /opt/school.sql 3.2恢复表操作  和恢复数据库相同    mysql -u root -p stady < /opt/stady-info.sql 但在恢复数据表时要注意,如果备份文件中只备份了表文件,此时mysql中又没有库时,若直接恢复,会失败,必须先创建库,然后再进行恢复。

MySQL Database full backup and recovery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.