MySQL中mysqldum数据库的备份和恢复
在mysql中自带了 mysqldum命令可直接能数据库进行备份和恢复操作,下面我来详细的介绍关于MySQL中mysqldum命令的用法。
1、mysqldump
1.1 备份
mysqldump 是采用SQL级别的备份机制,它将数据表导成 SQL 脚本文件,在不同的 MySQL 版本之间升级时相对比较合适,这也是最常用的备份方法。现在来讲一下 mysqldump 的一些主要参数:
•--compatible=name
它告诉 mysqldump,导出的数据将和哪种数据库或哪个旧版本的 MySQL 服务器相兼容。值可以为 ansi、mysql323、mysql40、postgresql、oracle、mssql、db2、maxdb、no_key_options、no_tables_options、no_field_options 等,要使用几个值,用逗号将它们隔开。当然了,它并不保证能完全兼容,而是尽量兼容。
•--complete-insert,-c
导出的数据采用包含字段名的完整 INSERT 方式,也就是把所有的值都写在一行。这么做能提高插入效率,但是可能会受到 max_allowed_packet 参数的影响而导致插入失败。因此,需要谨慎使用该参数,至少我不推荐。
•--default-character-set=charset
指定导出数据时采用何种字符集,如果数据表不是采用默认的 latin1 字符集的话,那么导出时必须指定该选项,否则再次导入数据后将产生乱码问题。
•--disable-keys
告诉 mysqldump 在 INSERT 语句的开头和结尾增加 /*!40000 ALTER TABLE table DISABLE KEYS */; 和 /*!40000 ALTER TABLE table ENABLE KEYS */; 语句,这能大大提高插入语句的速度,因为它是在插入完所有数据后才重建索引的。该选项只适合 MyISAM 表。
•--extended-insert = true|false
默认情况下,mysqldump 开启 --complete-insert 模式,因此不想用它的的话,就使用本选项,设定它的值为 false 即可。
•--hex-blob
使用十六进制格式导出二进制字符串字段。如果有二进制数据就必须使用本选项。影响到的字段类型有 BINARY、VARBINARY、BLOB。
•--lock-all-tables,-x
在开始导出之前,提交请求锁定所有数据库中的所有表,以保证数据的一致性。这是一个全局读锁,并且自动关闭 --single-transaction 和 --lock-tables 选项。
•--lock-tables
它和 --lock-all-tables 类似,不过是锁定当前导出的数据表,而不是一下子锁定全部库下的表。本选项只适用于 MyISAM 表,如果是 Innodb 表可以用 --single-transaction 选项。
•--no-create-info,-t
只导出数据,而不添加 CREATE TABLE 语句。
•--no-data,-d
不导出任何数据,只导出数据库表结构。
•--opt
这只是一个快捷选项,等同于同时添加 --add-drop-tables --add-locking --create-option --disable-keys --extended-insert --lock-tables --quick --set-charset 选项。本选项能让 mysqldump 很快的导出数据,并且导出的数据能很快导回。该选项默认开启,但可以用 --skip-opt 禁用。注意,如果运行 mysqldump 没有指定 --quick 或 --opt 选项,则会将整个结果集放在内存中。如果导出大数据库的话可能会出现问题。
•--quick,-q
该选项在导出大表时很有用,它强制 mysqldump 从服务器查询取得记录直接输出而不是取得所有记录后将它们缓存到内存中。
•--routines,-R
导出存储过程以及自定义函数。
•--single-transaction
该选项在导出数据之前提交一个 BEGIN SQL语句,BEGIN 不会阻塞任何应用程序且能保证导出时数据库的一致性状态。它只适用于事务表,例如 InnoDB 和 BDB。本选项和 --lock-tables 选项是互斥的,因为 LOCK TABLES 会使任何挂起的事务隐含提交。要想导出大表的话,应结合使用 --quick 选项。
•--triggers
同时导出触发器。该选项默认启用,用 --skip-triggers 禁用它。
其他参数详情请参考手册,我通常使用以下 SQL 来备份 MyISAM 表:
代码如下 | 复制代码 |
/usr/local/mysql/bin/mysqldump -uyejr -pyejr --default-character-set=utf8 --opt --extended-insert=false --triggers -R --hex-blob -x db_name > db_name.sql |
使用以下 SQL 来备份 Innodb 表:
代码如下 | 复制代码 |
/usr/local/mysql/bin/mysqldump -uyejr -pyejr --default-character-set=utf8 --opt --extended-insert=false |
打开开始->运行->输入cmd进入命令行模式
代码如下 | 复制代码 |
c:>mysqldump -h localhost -u root -p mydb >e:mysqlmydb.sql |
然后输入密码,等待一会导出就成功了,可以到目标文件中检查是否成功。
2.将数据库mydb中的mytable导出到e:mysqlmytable.sql文件中:
代码如下 | 复制代码 |
c:>mysqldump -h localhost -u root -p mydb mytable >e:mysqlmytable.sql |
3.将数据库mydb的结构导出到e:mysqlmydb_stru.sql文件中:
代码如下 | 复制代码 |
c:>mysqldump -h localhost -u root -p mydb --add-drop-table >e:mysqlmydb_stru.sql |
//-h localhost可以省略,其一般在虚拟主机上用
1.2 还原
用 mysqldump 备份出来的文件是一个可以直接倒入的 SQL 脚本,有两种方法可以将数据导入。
从外部文件导入数据到数据库:
例如:
代码如下 | 复制代码 |
/usr/local/mysql/bin/mysql -uyejr -pyejr db_name |
从e:mysqlmydb2.sql中将文件中的SQL语句导入数据库中:
代码如下 | 复制代码 |
c:>mysql -h localhost -u root -p mydb2 |
•用 SOURCE 语法
其实这不是标准的 SQL 语法,而是 mysql 客户端提供的功能,例如:
代码如下 | 复制代码 |
SOURCE /tmp/db_name.sql; |
这里需要指定文件的绝对路径,并且必须是 mysqld 运行用户(例如 nobody)有权限读取的文件。
进入mysql数据库控制台 : 如 mysql -u root -p
代码如下 | 复制代码 |
mysql>use databasename |
然后使用source命令,后面参数为脚本文件(这里用到的是.sql)
代码如下 | 复制代码 |
mysql>source d:mydb.sql |
然后输入密码,就OK了
二.命令行进入MYSQL的方法:
代码如下 | 复制代码 |
C:>mysql -h hostname -u username -p |
按ENTER键,等待然后输入密码,这里hostname为服务器的名称,如localhost,username为MYSQL的用户名,如root.进入命令行就可以直接操作MYSQL了。
关于导入文件大小限制问题的解决:
默认情况下:mysql对导入的文件大小有限制的,最大为2M,所以当文件很大时,直接无法导入,解决列举如下:
1.在php.ini中修改相关参数:
影响Mysql导入文件大小的参数有三个:
代码如下 | 复制代码 |
memory_limit=128M, upload_max_filesize=2M, post_max_size=8M |
修改upload_max_filesize=200M这里修改满足你需要的大小,可以同时修改其他两项memory_limit=250M ,post_max_size=200M.这样就可以导入200M以下的.sql文件了。
上文是把mysql放置在系统路径下,其实不放也可以。如我的mysql安装目录为D:MySQL Server 5.0;则首先打开dos窗口,然后输入D:(没有'')回车;此时应该会出现D:>这样的标志,然后在其后输入D:MySQL Server 5.0bin回车;出现D:MySQL Server 5.0bin>接着输入mysqldump -u root -p 数据库名 >数据库名.sql(也可以输入路径); (具体参照上面)导入文件同样,只是改了'>'为'

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

AI Hentai Generator
Generate AI Hentai for free.

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



Many friends don’t know how to recover diskgenius data, so the editor will share the relevant tutorials on diskgenius data recovery. Let’s take a look. I believe it will be helpful to everyone. First, in the hard disk partition diagram above the main interface of DiskGenius, you can directly select the target partition and right-click. Then, in the shortcut menu that pops up, find and click the "Deleted or formatted file recovery" menu item, as shown in the figure. In the second step, the recovery options window pops up and make sure to check the three options of "Recover Deleted Files", "Complete Recovery" and "Extra Scan for Known File Types". Step 3: Click the "Select File Type" button on the right and specify the files you need to recover in the pop-up window

Solution to the problem of PHP parameter loss In the process of developing PHP programs, we often encounter the problem of parameter loss. This may be caused by incomplete parameters passed by the front end or incorrect way of receiving parameters by the back end. In this article, we will provide some solutions to the problem of missing parameters in PHP, along with specific code examples. 1. Front-end parameter passing problem Use the GET method to pass parameters. When using the GET method to pass parameters, the parameters will be appended to the requested URL in the form of URL parameters. When receiving parameters in the backend

ThinkPHP6 data backup and recovery: ensuring data security With the rapid development of the Internet, data has become an extremely important asset. Therefore, the security of data is of great concern. In web application development, data backup and recovery are an important part of ensuring data security. In this article, we will introduce how to use the ThinkPHP6 framework for data backup and recovery to ensure data security. 1. Data backup Data backup refers to copying or storing the data in the database in some way. This way even if the data

Data backup and restoration of PHP applications through DockerCompose, Nginx and MariaDB. With the rapid development of cloud computing and containerization technology, more and more applications choose to use Docker to deploy and run. In the Docker ecosystem, DockerCompose is a very popular tool that can define and manage multiple containers through a single configuration file. This article will introduce how to use DockerCompose, Ng

Laravel is a popular PHP web application framework that provides many fast and easy ways to build efficient, secure and scalable web applications. When developing Laravel applications, we often need to consider the issue of data recovery, that is, how to recover data and ensure the normal operation of the application in the event of data loss or damage. In this article, we will introduce how to use Laravel middleware to implement data recovery functions and provide specific code examples. 1. What is Lara?

How to solve a broken hard disk sector? A broken hard disk sector is a common hardware failure, which may cause data loss and affect computer performance. It is very important to understand and solve the problem of bad hard drive sectors. This article will introduce the concept of hard disk sectors, discuss common causes of bad hard disk sectors and solutions. 1. What are hard disk sectors? Before introducing how to solve the problem of bad hard disk sectors, let’s first understand what hard disk sectors are. A hard disk sector is the smallest readable and writable unit on a hard drive. It is a small section of space on a hard drive. It is

How to deal with the data backup consistency problem in C++ big data development? In C++ big data development, data backup is a very important part. In order to ensure the consistency of data backup, we need to take a series of measures to solve this problem. This article will discuss how to deal with data backup consistency issues in C++ big data development and provide corresponding code examples. Using transactions for data backup Transactions are a mechanism to ensure the consistency of data operations. In C++, we can use the transaction concept in the database to implement data backup.

How to quickly recover from failures and errors encountered by MySQL database? MySQL is a widely used open source relational database management system that many applications and websites rely on to store and manage data. However, database failures and errors are inevitable, which may result in data loss or application failure to function properly. When encountering a MySQL database failure or error, it is very important to recover the database quickly and effectively. This article will introduce some methods to quickly restore MySQL database. Determine the type of fault and error before starting
