mysql数据库的备份和还原的总结_MySQL
bitsCN.com
平时对数据库的操作基本是对数据表以及表中的数据操作,对数据库的备份和还原用到的较少,今天在此总结下,当作是一个自我学习的过程。
1,数据库的备份
用mysqldump命令,原因:该工具在多个存储引擎上使用,此外其备份的数据库文件只要进行简单的修改便可在sql server和sybase上使用。
格式:mysqldump -h localhost(填数据所在的主机名) -u root(用户名) -p db_name(要备份的数据库名)>path(要备份的地址)
例如:打开dos窗输入 mysqldump -u root -p db_name>E:/11.sql;
如果只想单独备份数据库中的某个表就在数据库名后加表名就好
例如备份db_name数据库中的table1表:mysqldump -u root -p db_nametable1>E:/11.sql;
如果想备份多个数据库,则加入关键字--databases 如下:mysqldump -u root -p--databases db_name1db_name2>E:/db.sql; 实现了对db_name1和数据库db_name2的备份。
此外需要特别注意的一点时,不加--databases对数据库进行备份后,如过被备份的数据库直接被删除后,无法直接用备份进行恢复,因为 “mysqldump -u root -p db_name>E:/db.sql”只备份了db_name数据库下的所有表,文件中不包括创建数据库的CREATE DATABASE语句,因此如果把db_name删了使用db.sql还原时会出现:“No database selected”的错误信息,因此在还原前必须先创建db_name数据库,并用use db_name选中。或者在备份时,直接用mysqldump -u root -p --databases db_name>db.sql,这样不仅备份了所用数据库下的表结构,而且包括了创建数据库的语句。
2,数据的还原
如果没有登录mysql数据库,mysql -h localhost -u root -p db_name(数据库名) 如果已经登录了mysql里面则通过source语句还原:先在mysql中选择要还原的数据库,即use db_name; 然后执行source filename.sql(文件路径名) 3,表的导入导出 mysql关键的操作是对数据操作,而数据是存储在表中,因此对数据的直接操作是通过表的导入导出实现的,表的导入与导出有多种方法下面我们就总结一下: 一、已登录mysql,用select...into outfile导出文件,用load data infile导入文件 格式:use db_name 进入某个数据库 select * from table_name into outfile "filepath.txt"; 将表table_name导出filepath路径下的文件中 load data infile "filepath.txt" into table table_name; 将filepath.txt的数据导入到表table_name中; 上面这两条语句都有一些可选项[options]来规定导入、导出时数据的格式,如: select * from table_name into outfile "filepath.txt" fields terminated by ',' enclosed by ' /" ' escaped by ' /' ' lines terminated by '/r/n' staring by '>' fields下的terminated by ','表示导出的数据字段之间用,相隔,enclosed by ' /" ' 表示每个字段被双引号包围,escaped by ' /' '表示将转义字符定义为单引号。lines下的terminated by '/r/n'表示每行记录以回车换行符结尾,staring by '>'表示每一行以‘ > ’字符开始。例子如下: 不加option的导出数据格式为 1Tale of AAA Dickes28 1995novel 112EmmaT Jane lura40 1993drama 22 加了如上option的导出数据格式为 > "1","Tale of AAA",... 二 不登录mysql 用mysqldump命令导出文件,感觉跟备份数据库相似,命令如下: mysqldump -T path dbname table_name -u root -p path为导出的路径,dbname为数据库名 table_name为数据库中的表名。 也可像select ... into outfile 一样加option可选项如下: mysqldump -T path dbname table_name -u root -p --fields-terminated-by=,--fields-enclosed-by=/"--fields-escaped-by=?--lines-terminated-by=/r/n 用mysql命令导出数据库 mysql -u root -p --execute="select 语句" dbname>filename.txt 同样可以用mysql导出html格式的数据,使用--html选项即可 mysql -u root -p --html --execute="select 语句" dbname>filename.html mysql -u root -p --xml --execute="select 语句" dbname>filename.xml 用mysqlimport导入文本文件 mysqlimport -u root -p db_name filename.txt 也可以带有可选参数如mysqldump的参数。 mysqlimport -u root -p db_name filename.txt --fields-terminated-by=,--fields-enclosed-by=/"--fields-escaped-by=?--lines-terminated-by=/r/n
> "2","EmmaT",...

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.
