Mysql uses as an import and Export command for backup restore _mysql

Source: Internet
Author: User
Tags create database import database

Mysqldump
command-line Export database:
1, into the MySQL directory under the Bin folder: cd MySQL to the Bin folder directory
As I entered the command line: CD C:\Program files\mysql\mysql Server 4.1\bin
2, Export database: Mysqldump-u user name-p database name > exported file name
As I entered the command line: Mysqldump-u root-p News > News.sql (Input will allow you to enter the password into MySQL)
(If you export a single table, enter the table name after the database name)
3, will see the file News.sql automatically generated to the bin file
Command line Import Database:
1, to move the imported. sql file to the bin file, this is a convenient path
2, the 1th step with the above export
3, enter mysql:mysql-u user name-P
As I entered the command line: Mysql-u root-p (Enter the same will let you enter the Ysql password)
4, create the database you want to build in Mysql-front, when it is an empty database, such as a new target database named News (Mysql>create DB News;)
5, enter: Mysql>use Target database name
As I entered the command line: Mysql>use news;
6, import file: Mysql>source import of the file name;
As I entered the command line: Mysql>source news.sql;

Instance:
Export

#> cd/usr/local/src
#>/usr/local/mysql/bin/mysqldump-uroot-p123456 cacti > cacti.sql;

So you see Cacti.sql exported to the/usr/local/src/directory.
1. Export the entire database

Mysqldump-u user name-p database name > exported filename
mysqldump-u root-p ABC > Abc.sql

2. Export a table

Mysqldump-u user name-P database name table name > exported filename
mysqldump-u root-p ABC users> abc_users.sql

3. Export a database structure

Mysqldump-u Root-p-D--add-drop-table ABC >/usr/local/abc_db.sql-
D No data--add-drop-table add a DRO before each create statement P table


Import

#>/usr/local/mysql/bin/mysql-uroot-p
mysql> CREATE database cacti;   The new database name is cacti
mysql> use cacti;              Select Target Database
mysql> source/usr/local/src/cacti.sql   Import Database

This will import the Cacti.sql file

Load data
(Windows System.. Table structure in the same case)
The command is executed in the DOS Mysql/bin directory:
Export:

SELECT * from AAA into outfile "E:/a.txt";

Import:

Load data local infile "e:/a.txt" into table AAA;

Backup: SELECT into outfile

SELECT * from Hx_9enjoy into outfile '/tmp/9enjoy.txt '

The tab splits the field by default, and the record uses/n split. You can customize three parameters.

SELECT * from Hx_9enjoy to outfile '/tmp/9enjoy_hx.txt ' FIELDS terminated by ', ' optionally enclosed by ' ' LINES termin Ated by ' n ';

Use, split field, double quotation mark to qualify field contents,/N split record.
You can also specify record output for certain conditions:

SELECT * FROM Hx_9enjoy WHERE ID < outfile '/tmp/9enjoy_100.txt '

Restore: LOAD DATA INFILE
Default condition:

LOAD DATA INFILE '/tmp/9enjoy.txt ' into TABLE hx_9enjoy

If there is a specified definition, add a description of the qualifier:

LOAD DATA INFILE '/tmp/9enjoy.txt ' into the TABLE Hx_9enjoy FIELDS terminated by ', ' optionally enclosed by ' "' LINES Terminat ED by '/n '

If DB is GBK or utf-8 encoding, you need to specify the character set parameter at restore time, otherwise the restored data is garbled.
Such as:

Load data infile '/tmp/9enjoy.txt ' into TABLE hx_9enjoy CHARACTER SET GBK;


Attention Matters
1.mysql must have permission to access the directory where the generated files
2. For security reasons, it is not possible to overwrite existing files, regardless of how the permissions of the file are allocated.
3. You cannot export a compressed file directly.

Related Article

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.