Table of Contents
mysqldump:数据库备份程序
SELECT...INTO OUTFILE
LOAD DATA INFILE
Home Database Mysql Tutorial mysqldump:数据库备份程序

mysqldump:数据库备份程序

Jun 07, 2016 pm 03:29 PM
backup database program

mysqldump:数据库备份程序 有 3 种方式来调用 mysqldump : mysqldump [ options ] db_name [ tables ] mysqldump [ options ] -- -database DB1 [DB2 DB3...] mysqldump [ options ] -- all--database 如果没有指定任何表或使用了 ---database 或 --all--d

mysqldump:数据库备份程序

 

3种方式来调用mysqldump

mysqldump <span>[</span><span>options</span><span>]</span> <span>db_name</span> <span>[</span><span>tables</span><span>]</span><span>
mysqldump </span><span>[</span><span>options</span><span>]</span> <span>--</span><span>-database DB1 [DB2 DB3...]</span>
mysqldump <span>[</span><span>options</span><span>]</span> <span>--</span><span>all--database</span>
Copy after login

如果没有指定任何表或使用了---database--all--database选项,则转储整个数据库。

 

备份一个数据库.

mysqldump <span>-</span>uroot <span>-</span>p123456 mysql <span>></span> mysql_backup.sql
Copy after login

这里备份了database mysql的结构和数据,生成的sql文件不会有创建database mysql的语句。

 

可以用一个命令备份mysql,test多个数据库:

mysqldump <span>-</span>u root <span>-</span>p123456 <span>--</span><span>database mysql test > my_databases.sql</span>
Copy after login

生成的sql文件有创建database mysql和test的语句

 

备份所有数据库

mysqldump <span>-</span>u root <span>-</span>p123456  <span>--</span><span>all-databases > all_databases.sql</span>
Copy after login

 

导出mysql这个数据库的结构

 mysqldump <span>-</span>u root <span>-</span>p123456 <span>-</span>d <span>--</span><span>add-drop-table mysql > mysql_define.sql</span>
Copy after login

 

导出一个数据所有数据并用gz压缩

mysqldump <span>-</span>u root <span>-</span>p123456 mysql <span>|</span> gzip <span>></span> mysql.sql.gz
Copy after login

 

 

可以这样将转储文件读回到服务器:

mysql <span>db_name</span> <span> <span>backup</span><span>-</span><span>file</span><span>.sql
mysql </span><span>-</span>e "source <span>/</span>path<span>-</span><span>to</span><span>--</span><span>backup/backup-file.sql" db_name</span></span>
Copy after login

或者从gz文件中还原

gunzip <span>-</span>f <span> mysql.sql.gz <span>|</span> mysql <span>-</span>u root <span>-</span>p123456 test</span>
Copy after login

 更多参考 http://dev.mysql.com/doc/refman/5.1/zh/client-side-scripts.html#mysqldump

 

SELECT...INTO OUTFILE

SELECT...INTO OUTFILE 'file_name'形式的SELECT可以把被选择的行写入一个文件中,该文件被创建到服务器主机上。

SELECT...INTO OUTFILELOAD DATA INFILE的补语;用于语句的exort_options部分的语法包括部分FIELDSLINES子句,这些子句与LOAD DATA INFILE语句同时使用。

在下面的例子中,生成一个文件,各值用逗号隔开。这种格式可以被许多程序使用

<span>SELECT</span> <span>*</span> <span>INTO</span> OUTFILE <span>'</span><span>/tmp/result.txt</span><span>'</span><span> 
FIELDS TERMINATED </span><span>BY</span> <span>'</span><span>,</span><span>'</span><span> 
OPTIONALLY ENCLOSED </span><span>BY</span> <span>'</span><span>"</span><span>'</span><span> 
LINES TERMINATED </span><span>BY</span> <span>'</span><span>\n</span><span>'</span>
<span>FROM</span> mysql.<span>user</span>;
Copy after login

将mysql数据库的user表的数据导出到/tmp/result.txt

SELECT...INTO OUTFILE只能导出数据,不能导出结构,一般和load data联合使用。

更多参考 http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#select

 

LOAD DATA INFILE

LOAD DATA INFILE语句用于高速地从一个文本文件中读取行,并装入一个表中。文件名称必须为一个文字字符串。

character_set_database系统变量指示的字符集被用于解释文件中的信息。

<span>LOAD</span> DATA LOCAL INFILE <span>'</span><span>/tmp/result.txt' INTO TABLE test.user
FIELDS TERMINATED BY </span><span>'</span>,<span>'</span><span> 
OPTIONALLY ENCLOSED BY </span><span>'</span>"<span>'</span><span> 
LINES TERMINATED BY </span><span>'</span>\n<span>'</span>
Copy after login

把/tmp/result.txt的数据导入到test数据库的user表。

 

更多参考 http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#load-data


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

Can't allow access to camera and microphone in iPhone Can't allow access to camera and microphone in iPhone Apr 23, 2024 am 11:13 AM

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

See all articles