Use mysqldump to remotely back up a mysql database

Source: Internet
Author: User
Tags sql error

Use mysqldump to remotely back up a mysql database

 

Mysqldump is a good tool for backing up mysql databases. It is faster than using backup tools such as phpmyadmin and avoids restrictions such as php. ini. In windows, you can also use scheduled tasks to implement timed remote database backup.

The user who uses mysqldump to back up the database requires a relatively small amount of permissions. Only the select and lock table permissions are required. Therefore, users who can create a backup database can back up the database to avoid accidental damage to the database.

Before backing up the database, we have to make full preparations. First, we created the database user -- backup, and the password is 123456. Next, determine which database we want to back up and where to store it. Then, determine where the backup files of the database are to be stored.

Assume that our task is to back up a database named backup_test on the 192.168.1.2 server and put the backup file in the D: \ bak \ folder. Assuming that the operating system is windows xp, we can use the following methods to perform the backup operation:

We can use the command prompt that comes with windows (there are many methods to call the command prompt, the most common is to click the Start menu, select "run", enter the command of cmd to start ), enter the following command in the command prompt:

Mysqldump-ubackup-p123456-h 192.168.1.2 backup_test> D: \ bak \ bakcup. SQL

In this command:

Mysqldump is the program name of myqldump;

-U is the database user name, followed by the user name backup;

-P is the password of the database, followed by the password. Note that there must be no space between-p and the password;

-H indicates the address of the database. If this parameter is not set, it indicates that the local database is backed up;

Name of the backup_test database to be backed up;

> It indicates that the data warehouse is uploaded to the end of the file, which is followed by the address of the original file. You need to use the absolute file, and the suffix of the file can also be used. txt.

Once this command is executed, mysqldump immediately performs the backup operation. Remote backup of the database seems to be a network situation. Generally, remote backup takes some time.

If you write this command to process files in batches, You can regularly back up data through scheduled tasks in windows.

In windows, mysqldump is used to back up the database. Because there is no gzip compression engine, you cannot compress the backup database. Therefore, if there are too many backup files, you need to regularly clean up expired database backup files.

If the information room 1044 is provided, you do not have sufficient permissions to back up the database on the server.

 

The following describes the optional parameters of mysqldump in detail:

Basic usage:

Mysqldump [OPTIONS] database [tables]

If you do not specify any tables, the entire database will be exported.

Run mysqldump -- help to obtain the option table supported by your mysqldump version.

Note: If you run mysqldump without the -- quick or -- opt option, mysqldump loads the entire result set to the memory before the export result. If you are exporting a large database, this may be a problem.

Mysqldump supports the following options:

-- Add-locks

Add lock tables and unlock table before each TABLE is exported. (To make it faster to insert data to MySQL ).

-- Add-drop-table

Add a drop table before each create statement.

-- Allow-keywords

Names of columns allowed to be created as keywords. This is done by adding the table name before the column name.

-C, -- complete-insert

Use the complete insert Statement (with the column name ).

-C, -- compress

If both the client and server support compression, all information is compressed between the two.

-- Delayed

Use the insert delayed command to INSERT rows.

-E, -- extended-insert

Use the new multiline INSERT syntax. (A more compact and faster insert statement is provided)

-#, -- Debug [= option_string]

Tracking Program usage (for debugging ).

-- Help

Displays a help message and exits.

-- Fields-terminated-by =...

-- Fields-enclosed-by =...

-- Fields-optionally-enclosed-by =...

-- Fields-escaped-by =...

-- Fields-terminated-by =...

These options are used with-T options and have the same meaning as the load data infile clause.

Load data infile syntax.

-F, -- flush-logs

Wash Out the log files on the MySQL server before starting the export.

-F, -- force,

Even if we get an SQL error while exporting a table, continue.

-H, -- host = ..

Export data from the MySQL server on the named host. The default host is localhost.

-L, -- lock-tables.

Lock all tables for start export.

-T, -- no-create-info

Create table statement)

-D, -- no-data

No row information is written to the table. If you only want to export the structure of a table, this is very useful!

-- Opt

Same as -- quick -- add-drop-table -- add-locks -- extended-insert -- lock-tables.

You should be given the fastest export possible for reading a MySQL server.

-Pyour_pass, -- password [= your_pass]

The password used to connect to the server. If you do not specify "= your_pass", mysqldump requires a password from the terminal.

-P port_num, -- port = port_num

The TCP/IP Port used to connect to a host. (This is used to connect to a host other than localhost because it uses Unix sockets .)

-Q, -- quick

Directly export the data to stdout without buffering the query; Use mysql_use_result () to do it.

-S/path/to/socket, -- socket =/path/to/socket

The socket file used when connecting to localhost (which is the default host.

-T, -- tab = path-to-some-directory

For each given table, CREATE a table_name. SQL file that contains the SQL CREATE command and a table_name.txt file that contains data. Note: This only works when mysqldump runs on the same machine where the mysqld daemon is running .. The format of the txt file is determined by the options -- fields-xxx and -- lines -- xxx.

-U user_name, -- user = user_name

The username used by MySQL to connect to the server. The default value is your Unix login name.

-O var = option, -- set-variable var = option

Set the value of a variable. Possible variables are listed below.

-V, -- verbose

Lengthy mode. Print out more information about the program.

-V, -- version

Print the version information and exit.

-W, -- where = 'where-condition'

Only selected records are exported. Note that the quotation marks are mandatory!

"-- Where = user = 'jimf '"-wuserid> 1 ""-wuserid <1"

The most common use of mysqldump may make a backup of the entire database:

Mysqldump -- opt database> backup-file. SQL

However, it is also useful for enriching another MySQL database with information from one database:

Mysqldump -- opt database mysql -- host = remote-host-C database

Mysqldump exports complete SQL statements, so it is easy to import data into mysql client programs:

Mysqladmin create target_db_name

Mysql target_db_name <backup-file. SQL

 

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.