How to import and export MySQL Databases

Source: Internet
Author: User
We all know that there are two types of MySQL Database Import in both windows and linux environments: 1) Export the database SQL script and import it again; 2) directly copy the database directory and files.

We all know that there are two types of MySQL Database Import in both windows and linux environments: 1) Export the database SQL script and import it again; 2) directly copy the database directory and files.

Procedure


1. Go to the bin folder under the MySQL Directory: directory from cd MySQL to the bin folder
For example, the command line I entered is cd C: Program FilesMySQLMySQL Server 4.1bin.
2. Export Database: mysqldump-u username-p Database Name> exported file name
For example, the command line I entered is mysqldump-u root-p news> news. SQL (after entering the command, you will be asked to enter the MySQL password)
(If you export a single table, enter the table name after the database name)
3. The file news. SQL is automatically generated under the binfile.

Command Line import database:

1. The. SQL file to be imported is moved to the binfile, which is convenient.
2. Step 1 of the above-mentioned export
3. Enter MySQL: mysql-u username-p
For example, the command line I entered is mysql-u root-p (after entering the same command, you will be asked to enter the ySQL password)
4. create the database you want to create in MySQL-Front. An empty database is used, for example, create a target database named news (mysql> create database news ;)
5. Enter the mysql> use target database name.
For example, the command line I entered is mysql> use news;
6. Import file: the file name imported from mysql> source;
For example, the command line I entered is mysql> source news. SQL;

Example

1. Export the entire database

The Code is as follows:
Mysqldump-u username-p Database Name> exported file name
Mysqldump-u wcnc-p smgp_rj_wcnc> wcnc. SQL

2. Export a table
 

The Code is as follows:
Mysqldump-u user name-p database name Table Name> exported file name
Mysqldump-u wcnc-p smgp_rj_wcnc users> wcnc_users. SQL

3. Export a database structure
 

The Code is as follows:

Mysqldump-u wcnc-p-d -- add-drop-table smgp_apps_wcnc> d: wcnc_db. SQL
-D no data -- add-drop-table adds a drop table before each create statement.

Import Database

Common source commands
Go to the mysql Database Console,
For example, mysql-u root-p
Mysql> use Database
Then run the source command. The following parameter is the script file (for example,. SQL used here)
Mysql> source d: wcnc_db. SQL

In Linux:

2. Method 1: SQL script
The procedure is as follows:
2.1. Export an SQL script
On the original database server, you can use the phpMyAdmin tool or the mysqldump command (the mysqldump command is located in the mysql/bin/directory) command line to export the SQL script.
2.1.1 phpMyAdmin
Select "structure" and "data" to export. Do not add the "drop database" and "drop table" options.
Select the "Save As file" option. If there is more data, select the "gzipped" option.
Save the exported SQL file.
2.1.2 use mysqldump command line
Command Format
Mysqldump-u username-p Database Name> database name. SQL
Example:
Mysqldump-uroot-p abc> abc. SQL
(Export the database abc to the abc. SQL file)
Enter the password of the Database User Name When prompted.
2.2. Create an empty Database
Create a database on the master page/control panel. Assume that the database name is abc and the database full user is abc_f.
2.3. Import and execute SQL scripts
You can use either phpMyAdmin or mysql command line.
2.3.1 use phpMyAdmin
From the control panel, select the created empty database and click "manage" to go to the management tools page.
In the "SQL" menu, browse and select the exported SQL file, and click "execute" to load and execute the file.
Note: phpMyAdmin has a limit on the size of the uploaded file, and php itself has a limit on the size of the uploaded file. If the original SQL File
Relatively large, you can use gzip to compress it. For text files such as SQL files, you can obtain a compression rate of or higher.
Gzip usage:
# Gzip xxxxx. SQL
Get
Xxxxx. SQL .gz file.
2.3.2 use mysql Command Line
Command Format
Mysql-u username-p database name <database name. SQL
Example:
Mysql-uabc_f-p abc <abc. SQL
(Import database abc from abc. SQL file)
Enter the password of the Database User Name When prompted.
3 method 2 Direct Copy
If the database is large, you can use the direct copy method. However, different versions and operating systems may be incompatible with each other, so use it with caution.
3.1 prepare the original file
Package A file with tar
3.2 Create an empty Database
3.3 decompress
Decompress the package in the temporary directory, for example:
Cd/tmp
Tar zxf mydb.tar.gz
3.4 copy
Copy the extracted database file to the relevant directory.
Cd mydb/
Cp */var/lib/mysql/mydb/
For FreeBSD:
Cp */var/db/mysql/mydb/
3.5 permission settings
Change the owner of the copied files to mysql: mysql and the permission to 660.

The Code is as follows:
Chown mysql: mysql/var/lib/mysql/mydb /*
Chmod 660/var/lib/mysql/mydb /*

Modify the data from mysqldump. In addition to adding the table) TYPE = MyISAM character set gbk;, add a set names gbk statement below;
In this way, the data can be smoothly imported to 4.1, without the need for garbled characters.


Windows:

DOS window
Start> RUN> cmd
Export to: mysqldump-u username-p-opt database username> database name. SQL
Enter your database password!
Import to: mysql-u username-p database username <database name. SQL (to operate in the current directory)

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.