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

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

主站蜘蛛池模板: 左贡县| 珲春市| 鹤壁市| 吉水县| 黔东| 咸丰县| 海伦市| 屏山县| 娄底市| 竹北市| 绥滨县| 冷水江市| 色达县| 和平区| 宜兴市| 开平市| 茂名市| 临武县| 工布江达县| 永春县| 怀化市| 平南县| 泌阳县| 河池市| 探索| 哈巴河县| 土默特右旗| 仙游县| 万载县| 土默特右旗| 保定市| 扎兰屯市| 深水埗区| 浦县| 二手房| 汨罗市| 阜平县| 社旗县| 岳阳市| 宁武县| 昭平县|