官术网_书友最值得收藏!

Setting backups

In this recipe, we will learn how to back up the MySQL database.

Getting ready

You will need administrative access to the MySQL database.

How to do it…

Follow these steps to set up the backups:

  1. Backing up the MySQL database is the same as exporting data from the server. Use the mysqldump tool to back up the MySQL database as follows:
    $ mysqldump -h localhost -u admin -p mydb > mydb_backup.sql
    
  2. You will be prompted for the admin account password. After providing the password, the backup process will take time depending on the size of the database.
  3. To back up all databases, add the --all-databases flag to the preceding command:
    $ mysqldump --all-databases -u admin -p alldb_backup.sql
    
  4. Next, we can restore the backup created with the mysqldump tool with the following command:
    $ mysqladmin -u admin -p create mydb
    $ mysql -h localhost -u admin -p mydb < mydb_backup.sql
    
  5. To restore all databases, skip the database creation part:
    $ mysql -h localhost -u admin -p < alldb_backup.sql
    

How it works…

MySQL provides a very general tool, mysqldump, to export all data from the database server. This tool can be used with any type of database engine, be it MyISAM or InnoDB or any other. To perform an online backup of InnoDB tables, mysqldump provides the --single-transaction option. With this option set, InnoDB tables will not be locked and will be available to other applications while backup is in progress.

Oracle provides the MySQL Enterprise backup tool for MySQL Enterprise edition users. This tool includes features such as incremental and compressed backups. Alternatively, Percona provides an open source utility known as Xtrabackup. It provides incremental and compressed backups and many more features.

Some other backup methods include copying MySQL table files and the mysqlhotcopy script for InnoDB tables. For these methods to work, you may need to pause or stop the MySQL server before backup.

You can also enable replication to mirror all data to the other server. It is a mechanism to maintain multiple copies of data by automatically copying data from one system to another. In this case, the primary server is called Master and the secondary server is called Slave. This type of configuration is known as Master-Slave replication. Generally, applications communicate with the Master server for all read and write requests. The Slave is used as a backup if the Master goes down. Many times, the Master-Slave configuration is used to load balance database queries by routing all read requests to the Slave server and write requests to the Master server. Replication can also be configured in Master-Master mode, where both servers receive read-write requests from clients.

See also

主站蜘蛛池模板: 个旧市| 蓝田县| 望奎县| 台州市| 铜鼓县| 安达市| 沁水县| 古蔺县| 象州县| 东辽县| 松潘县| 宜昌市| 宁化县| 历史| 南江县| 崇文区| 鄱阳县| 库尔勒市| 长沙市| 巴彦淖尔市| 阿合奇县| 行唐县| 阿克陶县| 固安县| 抚州市| 确山县| 西平县| 平湖市| 抚顺县| 吉林省| 海伦市| 宜兰县| 琼中| 茂名市| 西充县| 普安县| 贞丰县| 青龙| 平凉市| 怀集县| 兴安盟|