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.

MySQL Backup software

If you are administrating multiple MySQL servers, you can consider using a commercial solution for MySQL Backup. One solution could be Backup Bird. It is a full cloud server backup service, that monitor the progress of each backup and backup your files as well as your databases.

How to Baskup and Restore a MySQL database的更多相关文章

  1. Migrate MySQL database using dump and restore

    kaorimatz/mysqldump-loader: Load a MySQL dump file using LOAD DATA INFILEhttps://github.com/kaorimat ...

  2. Backup and Restore MySQL Database using mysqlhotcopy

    mysqlhotcopy is a perl script that comes with MySQL installation. This locks the table, flush the ta ...

  3. Writing to a MySQL database from SSIS

    Writing to a MySQL database from SSIS 出处  :  http://blogs.msdn.com/b/mattm/archive/2009/01/07/writin ...

  4. MySQL Database on Azure新功能

    本月中国版的MySQL Database on Azure发布了两项新功能: 1.主从复制——只读实例 在这之前Azure上的MySQL数据库也是支持主从复制的,但是只能作为on-premises部署 ...

  5. CentOS 7 安装 MySQL Database

    CentOS 7 安装 MySQL Database 1. 现在安装包,MySQL的安装包被分成了社区版和企业版,而本文将记录社区版本MySQL安装过程,下载MySQL版本如下: mysql-5.7. ...

  6. mysql-databaseython 3.4.0 with MySQL database

    Phttp://shttp://stackoverflow.com/questions/23376103/python-3-4-0-with-mysql-databasetackoverflow.co ...

  7. MySQL Database on Azure

    在国际版的Microsoft Azure上,MySQL服务是与ClearDB合作运营的.由于ClearDB无法在中国地区提供服务,因此微软中国的研发团队开发了专门面向中国市场的MySQL Databa ...

  8. MySQL Database on Azure 参数设置

    在使用MySQL过程中,经常会根据需要对MySQL的参数进行一些设置和调整.作为PaaS版本的MySQL,MySQL Database on Azure在参数设置方面有一些限制,客户不能像使用on-p ...

  9. MySQL Database on Azure 的用户名

    MySQL Database on Azure是中国版Windows Azure上的一个PaaS服务,类似于AWS上的RDS.MySQL Database on Azure的用户名与on-premis ...

随机推荐

  1. Java 权限框架 Shiro 实战一:理论基础

    Apache Shiro 官网地址:http://shiro.apache.org/ Apache Shiro is a powerful and easy-to-use Java security ...

  2. 关于 ImageLoader 说的够细了。。。

    简介ImageLoader(一) 分类: android 开源及第三方项目2014-05-30 12:14 14126人阅读 评论(0) 收藏 举报 ImageLoader 使用该开源项目的之前,先给 ...

  3. mysql-8 alter命令

    当我们需要修改数据表名或者修改数据表字段时,就需要用到Mysql alter命令. 查看表结构: -- 以下2个命令是通用的 show columns from test_alter_tbl; des ...

  4. web开发,click,touch,tap事件浅析

    一.click 和 tap 比较 两者都会在点击时触发,但是在手机WEB端,click会有 200~300 ms,所以请用tap代替click作为点击事件. singleTap和doubleTap 分 ...

  5. selenium+python在mac环境上的搭建

    前言 mac自带了python2.7的环境,所以在mac上安装selenium环境是非常简单的,输入2个指令就能安装好 需要安装的软件: 1.pip 2.selenium2.53.6 3.Firefo ...

  6. xsigo systems

    继虚拟化管理软件巨头VMware以12.6亿美元收购云计算网络虚拟化厂商Nicira后,昨日,有报道称,甲骨文也不甘示弱,宣布收购了另一家网络虚拟化技术厂商Xsigo,为这股SDN(软件定义网络)热潮 ...

  7. django多对多数据库建立 以及数据的传输 并进行增加 删除 修改

    多对多数据库的建立 class Host(models.Model): nid = models.AutoField(primary_key=True) #自增id hostname = models ...

  8. Angular2中Input和Output

    @Input @Input是用来定义模块的输入的,用来让父模块往子模块传递内容: @Output 子模块自定义一些event传递给父模块用@Output. 对于angular2中的Input和Outp ...

  9. localStorage,sessionStorage

    h5的本地存储. 现代浏览器普遍开始支持H5本地存储,localStorage.sessionStorage.可以用来代替cookie的一部分存储功能,他比cookie存储量更大.比较实用. 两者用法 ...

  10. 【318】C# 学习笔记

    笔记 01:C# 数据结构 引用类型(Reference types) 引用类型不包含存储在变量中的实际数据,但它们包含对变量的引用. 换句话说,它们指的是一个内存位置.使用多个变量时,引用类型可以指 ...