文档章节

How to Back Up and Restore a MySQL Database

perfectspr
 perfectspr
发布于 2014/11/05 11:19
字数 949
阅读 23
收藏 0

Reference: http://webcheatsheet.com/sql/mysql_backup_restore.php

 

If you're storing anything in MySQL databases that you do not want to lose, it is very important to make regular backups of your data to protect it from loss. This tutorial will show you two easy ways to backup and restore the data in your MySQL database. You can also use this process to move your data to a new web server.

  Back up From the Command Line (using mysqldump) 

If you have shell or telnet access to your web server, you can backup your MySQL data by using the mysqldump command. This command connects to the MySQL server and creates an SQL dump file. The dump file contains the SQL statements necessary to re-create the database. Here is the proper syntax:

$ mysqldump --opt -u [uname] -p[pass] [dbname] > [backupfile.sql]

  • [uname] Your database username

  • [pass] The password for your database (note there is no space between -p and the password)

  • [dbname] The name of your database

  • [backupfile.sql] The filename for your database backup

  • [--opt] The mysqldump option

  For example, to backup a database named 'Tutorials' with the username 'root' and with no password to a file tut_backup.sql, you should accomplish this command: 

$ mysqldump -u root -p Tutorials > tut_backup.sql

This command will backup the 'Tutorials' database into a file called tut_backup.sql which will contain all the SQL statements needed to re-create the database.

With mysqldump command you can specify certain tables of your database you want to backup. For example, to back up only php_tutorials and asp_tutorials tables from the 'Tutorials' database accomplish the command below. Each table name has to be separated by space.

$ mysqldump -u root -p Tutorials php_tutorials asp_tutorials > tut_backup.sql

Sometimes it is necessary to back up more that one database at once. In this case you can use the --database option followed by the list of databases you would like to backup. Each database name has to be separated by space.

$ mysqldump -u root -p --databases Tutorials Articles Comments > content_backup.sql

If you want to back up all the databases in the server at one time you should use the --all-databases option. It tells MySQL to dump all the databases it has in storage.

$ mysqldump -u root -p --all-databases > alldb_backup.sql

The mysqldump command has also some other useful options:

--add-drop-table: Tells MySQL to add a DROP TABLE statement before each CREATE TABLE in the dump.

--no-data: Dumps only the database structure, not the contents.

--add-locks: Adds the LOCK TABLES and UNLOCK TABLES statements you can see in the dump file.

The mysqldump command has advantages and disadvantages. The advantages of using mysqldump are that it is simple to use and it takes care of table locking issues for you. The disadvantage is that the command locks tables. If the size of your tables is very big mysqldump can lock out users for a long period of time.

Back up your MySQL Database with Compress

If your mysql database is very big, you might want to compress the output of mysqldump. Just use the mysql backup command below and pipe the output to gzip, then you will get the output as gzip file.

$ mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]

If you want to extract the .gz file, use the command below:

$ gunzip [backupfile.sql.gz]

Restoring your MySQL Database

Above we backup the Tutorials database into tut_backup.sql file. To re-create the Tutorials database you should follow two steps:

  • Create an appropriately named database on the target machine

  • Load the file using the mysql command:

  $ mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql] 

Have a look how you can restore your tut_backup.sql file to the Tutorials database.

$ mysql -u root -p Tutorials < tut_backup.sql

To restore compressed backup files you can do the following:

gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]

If you need to restore a database that already exists, you'll need to use mysqlimport command. The syntax for mysqlimport is as follows:

mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql]

Backing Up and Restoring using PHPMyAdmin

It is assumed that you have phpMyAdmin installed since a lot of web service providers use it. To backup your MySQL database using PHPMyAdmin just follow a couple of steps:

  • Open phpMyAdmin.

  • Select your database by clicking the database name in the list on the left of the screen.

  • Click the Export link. This should bring up a new screen that says View dump of database (or something similar).

  • In the Export area, click the Select All link to choose all of the tables in your database.

  • In the SQL options area, click the right options.

  • Click on the Save as file option and the corresponding compression option and then click the 'Go' button. A dialog box should appear prompting you to save the file locally.

  Restoring your database is easy as well as backing it up. Make the following: 

  • Open phpMyAdmin.

  • Create an appropriately named database and select it by clicking the database name in the list on the left of the screen. If you would like to rewrite the backup over an existing database then click on the database name, select all the check boxes next to the table names and select Drop to delete all existing tables in the database.

  • Click the SQL link. This should bring up a new screen where you can either type in SQL commands, or upload your SQL file.

  • Use the browse button to find the database file.

  • Click Go button. This will upload the backup, execute the SQL commands and re-create your database.


本文转载自:http://webcheatsheet.com/sql/mysql_backup_restore.php

perfectspr
粉丝 5
博文 43
码字总数 1198
作品 0
大连
技术主管
私信 提问
MySQL Backup and Restore Commands for DBA

MySQL Backup and Restore Commands for Database Administration by Ravi Saive | Published: November 2, 2012 | Last Updated: January 3, 2015 Download Your Free eBooks NOW - 10 Free......

rootliu
2018/06/27
13
0
mysql数据库备份与恢复脚本

#!/bin/bash #mysql database backup and restore the script # define config variable host=localhost user=root passwd=xxxxx #this variable for grant mainHostIp="127.0.0.1" base_pat......

全村人希望
2016/12/19
0
0
Common tasks for MySQL

Copy Database dbname1 to dbname2 in same MySQL instance mysqldump dbname1 -u root -pYourPassword | mysql dbname2 -u root -pYourPassword 2. Backup Database mysqldump -u root -pYo......

perfectspr
2014/11/05
5
0
mysql-cluster的备份与还原

测试start backup ############################################# //备份概念 1.备份时当前数据库的一个快照,备份包含下面三部分 A.metadata:数据库的定义和结构 B.table records:存储在表...

fighting-cluber
2014/03/03
1K
0
mysql存储过程导入导出命令

MySQL 5 has introduced some newinteresting features, like storedprocedures and triggers.I will show in this small post how we can backup and restore these componentsusing mysqld......

li_xiqing
2013/08/04
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Java 8 Optional:优雅地避免 NPE

本篇文章将详细介绍 Optional 类,以及如何用它消除代码中的 null 检查。在开始之前首先来看下什么是 NPE,以及在 Java 8 之前是如何处理 NPE 问题的。 空指针异常(NullPointException,简称...

武培轩
20分钟前
1
0
CountDownLatch实现的并发框架

目录结构 package com.**.**.base.support.executor;import lombok.NoArgsConstructor;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;imp......

MR_TE
22分钟前
7
0
学习记录(day06-事件、按键修饰符、计算属性)

[TOC] 1.1 事件修饰符/按键修饰符 vue 通过事件修饰符对dom事件细节进行控制 <标签 @事件.修饰符="函数"></标签>.prevent ---阻止浏览器默认行为.stop ---阻止浏览器事件冒泡.e...

庭前云落
41分钟前
5
0
006-Sigle-基于blockstack去中心化博客

本篇文章主要讲解有关基于Blockstack的Sigle是一个去中心化的博客项目; 官网地址:https://www.sigle.io/ Github地址:https://github.com/pradel/sigle 页面展示: 介绍: A beautiful de...

Riverzhou
48分钟前
17
0
驰骋工作流引擎开发平台属性功能的隐藏显示介绍

关键字: 工作流程管理系统 工作流引擎 asp.net工作流引擎 java工作流引擎. 表单引擎 工作流功能说明 工作流设计 工作流快速开发平台 业务流程管理 bpm工作流系统 java工作流主流框架 自定义...

孟娟
50分钟前
14
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部