欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysqldump数据库备份参数详解

程序员文章站 2024-02-29 15:56:46
mysqldump备份: 复制代码 代码如下:mysqldump -u用户名 -p密码 -h主机 数据库 a -w “sql条件” –lock-all-tables &g...

mysqldump备份:

复制代码 代码如下:

mysqldump -u用户名 -p密码 -h主机 数据库 a -w “sql条件” –lock-all-tables > 路径

mysqldump还原:

复制代码 代码如下:

mysqldump -u用户名 -p密码 -h主机 数据库 < 路径

mysqldump按条件导出:

复制代码 代码如下:

mysqldump -u用户名 -p密码 -h主机 数据库 a –where “条件语句” –no-建表> 路径
mysqldump -uroot -p1234 dbname a –where “tag='88′” –no-create-info> c:\a.sql

mysqldump按条件导入:

复制代码 代码如下:

mysqldump -u用户名 -p密码 -h主机 数据库 < 路径

案例:

复制代码 代码如下:

mysql -uroot -p1234 db1 < c:\a.txt

mysqldump导出表:

复制代码 代码如下:

mysqldump -u用户名 -p密码 -h主机 数据库 表

案例:mysqldump -uroot -p sqlhk9 a –no-data

参数详解:

使用mysqldump
mysqldump -u root -p your-new-password databasename [tablename] > db.sql

比较大的表需要用优化的dump以节省内存:
mysqldump --opt database > backup-file.sql

mysqldump工具有大量的选项,部分选项如下表:

  选项/option 作用/action performed

  --add-drop-table

  这个选项将会在每一个表的前面加上drop table if exists语句,这样可以保证导回mysql数据库的时候不会出错,因为每次导回的时候,都会首先检查表是否存在,存在就删除

  --add-locks

  这个选项会在insert语句中捆上一个lock table和unlock table语句。这就防止在这些记录被再次导入数据库时其他用户对表进行的操作
  
  -c or - complete_insert

  这个选项使得mysqldump命令给每一个产生insert语句加上列(field)的名字。当把数据导出导另外一个数据库时这个选项很有用。

  --delayed-insert 在insert命令中加入delay选项

  -f or -flush-logs 使用这个选项,在执行导出之前将会刷新mysql服务器的log.

  -f or -force 使用这个选项,即使有错误发生,仍然继续导出

  --full 这个选项把附加信息也加到create table的语句中

  -l or -lock-tables 使用这个选项,导出表的时候服务器将会给表加锁。

  -t or -no-create- info

  这个选项使的mysqldump命令不创建create table语句,这个选项在您只需要数据而不需要ddl(数据库定义语句)时很方便。
  
  -d or -no-data 这个选项使的mysqldump命令不创建insert语句。


在您只需要ddl语句时,可以使用这个选项。

  --opt 此选项将打开所有会提高文件导出速度和创造一个可以更快导入的文件的选项。

  -q or -quick 这个选项使得mysql不会把整个导出的内容读入内存再执行导出,而是在读到的时候就写入导文件中。

  -t path or -tab = path 这个选项将会创建两个文件,一个文件包含ddl语句或者表创建语句,另一个文件包含数据。ddl文件被命名为table_name.sql,数据文件被命名为table_name.txt.路径名是存放这两个文件的目录。目录必须已经存在,并且命令的使用者有对文件的特权。
  
  -w "where clause" or -where = "where clause "

参考国外网站


name
       mysqldump - a database backup program


synopsis
       mysqldump [options] [db_name [tbl_name ...]]


description
       the mysqldump client can be used to dump a database or a collection of
       databases for backup or for transferring the data to another sql server
       (not necessarily a mysql server). the dump contains sql statements to
       create the table and/or populate the table.

       if you are doing a backup on the server, and your tables all are myisam
       tables, you could consider using the mysqlhotcopy instead since faster
       backups and faster restores can be accomplished with the latter. see
       mysqlhotcopy(1).

       there are three general ways to invoke mysqldump:

       shell> mysqldump [options] db_name [tables]
       shell> mysqldump [options] --databases db1 [db2 db3...]
       shell> mysqldump [options] --all-databases

       if you do not name any tables or use the --databases or --all-databases
       option, entire databases are dumped.

       to get a list of the options your version of mysqldump supports,
       execute mysqldump --help.

       if you run mysqldump without the --quick or --opt option, mysqldump
       loads the whole result set into memory before dumping the result. this
       probably is a problem if you are dumping a big database. as of mysql
       4.1, --opt is enabled by default, but can be disabled with --skip-opt.

       if you are using a recent copy of the mysqldump program to generate a
       dump to be reloaded into a very old mysql server, you should not use
       the --opt or -e options.

       before mysql 4.1.2, out-of-range numeric values such as -inf and inf,
       as well as nan (not-a-number) values are dumped by mysqldump as null.
       you can see this using the following sample table:

       mysql> create table t (f double);
       mysql> insert into t values(1e+111111111111111111111);
       mysql> insert into t values(-1e111111111111111111111);
       mysql> select f from t;
       +------+
       | f    |
       +------+
       |  inf |
       | -inf |
       +------+

       for this table, mysqldump produces the following data output:

       --
       -- dumping data for table ‘t‘
       --
       insert into t values (null);
       insert into t values (null);

       the significance of this behavior is that if you dump and restore the
       table, the new table has contents that differ from the original
       contents. this problem is fixed as of mysql 4.1.2; you cannot insert
       inf in the table, so this mysqldump behavior is only relevant when you
       deal with old servers.

       mysqldump supports the following options:

       ·  --help, -?

          display a help message and exit.

       ·  --add-drop-database

          add a drop database statement before each create database statement.
          added in mysql 4.1.13.

       ·  --add-drop-table

          add a drop table statement before each create table statement.

       ·  --add-locks

          surround each table dump with lock tables and unlock tables
          statements. this results in faster inserts when the dump file is
          reloaded. see section 2.13, “speed of insert statements”.

       ·  --all-databases, -a

          dump all tables in all databases. this is the same as using the
          --databases option and naming all the databases on the command line.

       ·  --allow-keywords

          allow creation of column names that are keywords. this works by
          prefixing each column name with the table name.

       ·  --comments[={0|1}]

          if set to 0, suppresses additional information in the dump file such
          as program version, server version, and host.  --skip-comments has
          the same effect as --comments=0. the default value is 1, which
          includes the extra information. added in mysql 4.0.17.

       ·  --compact

          produce less verbose output. this option suppresses comments and
          enables the --skip-add-drop-table, --no-set-names,
          --skip-disable-keys, and --skip-add-locks options. added in mysql
          4.1.2.

       ·  --compatible=name

          produce output that is more compatible with other database systems
          or with older mysql servers. the value of name can be ansi,
          mysql323, mysql40, postgresql, oracle, mssql, db2, maxdb,
          no_key_options, no_table_options, or no_field_options. to use
          several values, separate them by commas. these values have the same
          meaning as the corresponding options for setting the server sql
          mode. see the section called “the server sql mode”.

          this option does not guarantee compatibility with other servers. it
          only enables those sql mode values that are currently available for
          making dump output more compatible. for example, --compatible=oracle
          does not map data types to oracle types or use oracle comment
          syntax.

          this option requires a server version of 4.1.0 or higher. with older
          servers, it does nothing.

       ·  --complete-insert, -c

          use complete insert statements that include column names.

       ·  --compress, -c

          compress all information sent between the client and the server if
          both support compression.

       ·  --create-options

          include all mysql-specific table options in the create table
          statements. before mysql 4.1.2, use --all instead.

       ·  --databases, -b

          dump several databases. normally, mysqldump treats the first name
          argument on the command line as a database name and following names
          as table names. with this option, it treats all name arguments as
          database names.  create database if not exists db_name and use
          db_name statements are included in the output before each new
          database.

       ·  --debug[=debug_options], -# [debug_options]

          write a debugging log. the debug_options string is often
          ´d:t:o,file_name'.

       ·  --default-character-set=charset

          use charset as the default character set. see section 7.1, “the
          character set used for data and sorting”. if not specified,
          mysqldump from mysql 4.1.2 or later uses utf8, and earlier versions
          use latin1.

       ·  --delayed-insert

          insert rows using insert delayed statements.

       ·  --delete-master-logs

          on a master replication server, delete the binary logs after
          performing the dump operation. this option automatically enables
          --first-slave before mysql 4.1.8 and enables --master-data
          thereafter. it was added in mysql 3.23.57 (for mysql 3.23) and mysql
          4.0.13 (for mysql 4.0).

       ·  --disable-keys, -k

          for each table, surround the insert statements with /*!40000 alter
          table tbl_name disable keys */; and /*!40000 alter table tbl_name
          enable keys */; statements. this makes loading the dump file into a
          mysql 4.0 or newer server faster because the indexes are created
          after all rows are inserted. this option is effective for myisam
          tables only.

       ·  --extended-insert, -e

          use multiple-row insert syntax that include several values lists.
          this results in a smaller dump file and speeds up inserts when the
          file is reloaded.

       ·  --fields-terminated-by=..., --fields-enclosed-by=...,
          --fields-optionally-enclosed-by=..., --fields-escaped-by=...,
          --lines-terminated-by=...

          these options are used with the -t option and have the same meaning
          as the corresponding clauses for load data infile. see section 2.5,
          “load data infile syntax”.

       ·  --first-slave, -x

          deprecated, renamed to --lock-all-tables in mysql 4.1.8.

       ·  --flush-logs, -f

          flush the mysql server log files before starting the dump. this
          option requires the reload privilege. note that if you use this
          option in combination with the --all-databases (or -a) option, the
          logs are flushed for each database dumped. the exception is when
          using --lock-all-tables or --master-data: in this case, the logs are
          flushed only once, corresponding to the moment that all tables are
          locked. if you want your dump and the log flush to happen at exactly
          the same moment, you should use --flush-logs together with either
          --lock-all-tables or --master-data.

       ·  --force, -f

          continue even if an sql error occurs during a table dump.

       ·  --host=host_name, -h host_name

          dump data from the mysql server on the given host. the default host
          is localhost.

       ·  --hex-blob

          dump binary string columns using hexadecimal notation (for example,
          ´abc' becomes 0x616263). the affected columns are binary, varbinary,
          and blob in mysql 4.1 and up, and char binary, varchar binary, and
          blob in mysql 4.0. this option was added in mysql 4.0.23 and 4.1.8.

       ·  --lock-all-tables, -x

          lock all tables across all databases. this is achieved by acquiring
          a global read lock for the duration of the whole dump. this option
          automatically turns off --single-transaction and --lock-tables.
          added in mysql 4.1.8.

       ·  --lock-tables, -l

          lock all tables before starting the dump. the tables are locked with
          read local to allow concurrent inserts in the case of myisam tables.
          for transactional tables such as innodb and bdb,
          --single-transaction is a much better option, because it does not
          need to lock the tables at all.

          please note that when dumping multiple databases, --lock-tables
          locks tables for each database separately. so, this option does not
          guarantee that the tables in the dump file are logically consistent
          between databases. tables in different databases may be dumped in
          completely different states.

       ·  --master-data[=value]

          this option causes the binary log position and filename to be
          written to the output. this option requires the reload privilege and
          the binary log must be enabled. if the option value is equal to 1,
          the position and filename are written to the dump output in the form
          of a change master statement that makes a slave server start from
          the correct position in the master's binary logs if you use this sql
          dump of the master to set up a slave. if the option value is equal
          to 2, the change master statement is written as an sql comment. this
          is the default action if value is omitted.  value may be given as of
          mysql 4.1.8; before that, do not specify an option value.

          the --master-data option turns on --lock-all-tables, unless
          --single-transaction also is specified (in which case, a global read
          lock is only acquired a short time at the beginning of the dump. see
          also the description for --single-transaction. in all cases, any
          action on logs happens at the exact moment of the dump. this option
          automatically turns off --lock-tables.

       ·  --no-create-db, -n

          this option suppresses the create database /*!32312 if not exists*/
          db_name statements that are otherwise included in the output if the
          --databases or --all-databases option is given.

       ·  --no-create-info, -t

          do not write create table statements that re-create each dumped
          table.

       ·  --no-data, -d

          do not write any row information for the table. this is very useful
          if you want to get a dump of only the structure for a table.

       ·  --opt

          this option is shorthand; it is the same as specifying
          --add-drop-table --add-locks --create-options --disable-keys
          --extended-insert --lock-tables --quick --set-charset. it should
          give you a fast dump operation and produce a dump file that can be
          reloaded into a mysql server quickly.  as of mysql 4.1, --opt is on
          by default, but can be disabled with --skip-opt. to disable only
          certain of the options enabled by --opt, use their --skip forms; for
          example, --skip-add-drop-table or --skip-quick.

       ·  --password[=password], -p[password]

          the password to use when connecting to the server. if you use the
          short option form (-p), you cannot have a space between the option
          and the password. if you omit the password value following the
          --password or -p option on the command line, you are prompted for
          one.

       ·  --port=port_num, -p port_num

          the tcp/ip port number to use for the connection.

       ·  --protocol={tcp | socket | pipe | memory}

          the connection protocol to use. added in mysql 4.1.

       ·  --quick, -q

          this option is useful for dumping large tables. it forces mysqldump
          to retrieve rows for a table from the server a row at a time rather
          than retrieving the entire row set and buffering it in memory before
          writing it out.

       ·  --quote-names, -q

          quote database, table, and column names within ‘‘' characters. if
          the server sql mode includes the ansi_quotes option, names are
          quoted within ‘"' characters. as of mysql 4.1.1, --quote-names is on
          by default. it can be disabled with --skip-quote-names, but this
          option should be given after any option such as --compatible that
          may enable --quote-names.

       ·  --result-file=file, -r file

          direct output to a given file. this option should be used on
          windows, because it prevents newline ‘n' characters from being
          converted to ‘rn' carriage return/newline sequences.

       ·  --set-charset

          add set names default_character_set to the output. this option is
          enabled by default. to suppress the set names statement, use
          --skip-set-charset. this option was added in mysql 4.1.2.

       ·  --single-transaction

          this option issues a begin sql statement before dumping data from
          the server. it is useful only with transactional tables such as
          innodb and bdb, because then it dumps the consistent state of the
          database at the time when begin was issued without blocking any
          applications.

          when using this option, you should keep in mind that only innodb
          tables are dumped in a consistent state. for example, any myisam or
          heap tables dumped while using this option may still change state.

          the --single-transaction option was added in mysql 4.0.2. this
          option is mutually exclusive with the --lock-tables option, because
          lock tables causes any pending transactions to be committed
          implicitly.

          to dump big tables, you should combine this option with --quick.

       ·  --socket=path, -s path

          the socket file to use when connecting to localhost (which is the
          default host).

       ·  --skip-comments

          see the description for the --comments option.

       ·  --tab=path, -t path

          produce tab-separated data files. for each dumped table, mysqldump
          creates a tbl_name.sql file that contains the create table statement
          that creates the table, and a tbl_name.txt file that contains its
          data. the option value is the directory in which to write the files.

          by default, the .txt data files are formatted using tab characters
          between column values and a newline at the end of each line. the
          format can be specified explicitly using the --fields-xxx and
          --lines--xxx options.

          note: this option should be used only when mysqldump is run on the
          same machine as the mysqld server. you must have the file privilege,
          and the server must have permission to write files in the directory
          that you specify.

       ·  --tables

          override the --databases or -b option. all arguments following the
          option are regarded as table names.

       ·  --user=user_name, -u user_name

          the mysql username to use when connecting to the server.

       ·  --verbose, -v

          verbose mode. print out more information on what the program does.

       ·  --version, -v

          display version information and exit.

       ·  --where=�����where-condition�����, -w �����where-condition�����

          dump only records php/select">selected by the given where condition. note that
          quotes around the condition are mandatory if it contains spaces or
          characters that are special to your command interpreter.

          examples:

          "--where=user='jimf'"
          "-wuserid>1"
          "-wuserid<1"

       ·  --xml, -x

          write dump output as well-formed xml.

       you can also set the following variables by using --var_name=value
       options:

       ·  max_allowed_packet

          the maximum size of the buffer for client/server communication. the
          value of the variable can be up to 16mb before mysql 4.0, and up to
          1gb from mysql 4.0 on.

       ·  net_buffer_length

          the initial size of the buffer for client/server communication. when
          creating multiple-row-insert statements (as with option
          --extended-insert or --opt), mysqldump creates rows up to
          net_buffer_length length. if you increase this variable, you should
          also ensure that the net_buffer_length variable in the mysql server
          is at least this large.

       it is also possible to set variables by using
       --set-variable=var_name=value or -o var_name=value syntax. however,
       this syntax is deprecated as of mysql 4.0.

       the most common use of mysqldump is probably for making a backup of an
       entire database:

       shell> mysqldump --opt db_name > backup-file.sql

       you can read the dump file back into the server like this:

       shell> mysql db_name < backup-file.sql

       or like this:

       shell> mysql -e "source /path-to-backup/backup-file.sql" db_name

       mysqldump is also very useful for populating databases by copying data
       from one mysql server to another:

       shell> mysqldump --opt db_name | mysql --host=remote_host -c db_name

       it is possible to dump several databases with one command:

       shell> mysqldump --databases db_name1 [db_name2 ...] > my_databases.sql

       if you want to dump all databases, use the --all-databases option:

       shell> mysqldump --all-databases > all_databases.sql

       if tables are stored in the innodb storage engine, mysqldump provides a
       way of making an online backup of these (see command below). this
       backup just needs to acquire a global read lock on all tables (using
       flush tables with read lock) at the beginning of the dump. as soon as
       this lock has been acquired, the binary log coordinates are read and
       lock is released. so if and only if one long updating statement is
       running when the flush...  is issued, the mysql server may get stalled
       until that long statement finishes, and then the dump becomes
       lock-free. so if the mysql server receives only short (in the sense of
       "short execution time") updating statements, even if there are plenty
       of them, the initial lock period should not be noticeable.

       shell> mysqldump --all-databases --single-transaction > all_databases.sql

       for point-in-time recovery (also known as “roll-forward”, when you need
       to restore an old backup and replay the changes which happened since
       that backup), it is often useful to rotate the binary log (see
       section 8.4, “the binary log”) or at least know the binary log
       coordinates to which the dump corresponds:

       shell> mysqldump --all-databases --master-data=2 > all_databases.sql
       or
       shell> mysqldump --all-databases --flush-logs --master-data=2 > all_databases.sql

       the simultaneous use of --master-data and --single-transaction works as
       of mysql 4.1.8. it provides a convenient way to make an online backup
       suitable for point-in-time recovery if tables are stored in the innodb
       storage engine.

       for more information on making backups, see section 6.1, “database
       backups”.


see also
       isamchk(1), isamlog(1), msql2mysql(1), myisamchk(1), myisamlog(1),
       myisampack(1), mysql(1), mysql.server(1), mysql_config(1),
       mysql_fix_privilege_tables(1), mysql_zap(1), mysqlaccess(1),
       mysqladmin(1), mysqlbinlog(1), mysqlcheck(1), mysqld(1),
       mysqld_multi(1), mysqld_safe(1), mysqlhotcopy(1), mysqlimport(1),
       mysqlshow(1), pack_isam(1), perror(1), replace(1), safe_mysqld(1)

       for more information, please refer to the mysql reference manual, which
       may already be installed locally and which is also available online at
       http://dev.mysql.com/doc/.


author
       mysql ab (http://www.mysql.com/).  this software comes with no
       warranty.