澳门在线威尼斯官方 > 电脑数据库 > mysqldump数据库备份参数详解

原标题:mysqldump数据库备份参数详解

浏览次数:128 时间:2019-09-07

这是客户端连接mysql服务器的指令,比较全的写法是下面两种

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

  If you use a -p or --password option and specify the password value, there must be no space between -p or --password= and the password following it.

       ·  --version, -V

如果你明确指定了-p或者--password的值,那么-p或者--password和密码值之间是不能有空格的。

          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.

具体就是:对于password选项,此选项是可选的

          Write dump output as well-formed XML.

第一个是全拼,第二个是第一个的缩写

          Dump data from the MySQL server on the given host. The default host
          is localhost.

还有一点就是各个参数之间是否有空格的问题,-u后面可以有也可以省略空格,对于-p后面的参数我要单独说一下

          Continue even if an SQL error occurs during a table dump.

for password options, the password value is optional:

          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.

当然命令行连接数据库还有其它参数,这里主要介绍几个经常使用的,其它请参考

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

一般在使用中,我们会省略-h参数,mysql会自动默认为本地连接

       ·  --delete-master-logs

 

mysqldump备份:

这是我从mysql官方文档拷过来的内容

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

也就是说在命令行中,你的mysql密码和-p或者--password参数之间有空格,mysql会认为你输入的是登录mysql后自动选择的数据库,而不是你所期望的密码

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

For mysql, the first nonoption argument is taken as the name of the default database. If there is no such option, mysql does not select a default database.

          See the description for the --comments option.

mysql --host=localhost --user=myname --password=password mydb

       ·  --port=port_num, -P port_num

使用此命令首先确保你的mysql运行环境已经搭建好

          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.

对于MySQL,第一个非选项参数被当作默认数据库的名称。如果没有这样的选项,MySQL就不会选择默认数据库。

       ·  --xml, -X

mysql -h localhost -u myname -ppassword mydb

       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.

  If you use a -p or --password option but do not specify the password value, the client program prompts you to enter the password. The password is not displayed as you enter it. This is more secure than giving the password on the command line. Other users on your system may be able to see a password specified on the command line by executing a command such as ps auxw

          Deprecated, renamed to --lock-all-tables in MySQL 4.1.8.

如果你使用了-p或者--password选项但是没有给出password值,客户端程序提示您输入密码。

          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.

          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.

复制代码 代码如下:

       ·  --single-transaction

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

       ·  --default-character-set=charset

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

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

          Use complete INSERT statements that include column names.

          Add a DROP TABLE statement before each CREATE TABLE statement.

          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.

       ·  max_allowed_packet

mysqldump导出表:

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.

       ·  --allow-keywords

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

       ·  --set-charset

       ·  --tab=path, -T path

       ·  --create-options

复制代码 代码如下:

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

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

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

       ·  --no-create-info, -t

mysqldump还原:

          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.

          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.

          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.

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

          Verbose mode. Print out more information on what the program does.

       ·  --disable-keys, -K

       Or like this:

       shell> mysql db_name < backup-file.sql

       ·  --extended-insert, -e

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

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

       ·  --socket=path, -S path

       ·  --quote-names, -Q

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

          The TCP/IP port number to use for the connection.

          Do not write CREATE TABLE statements that re-create each dumped
          table.

       ·  --no-data, -d

       ·  --quick, -q

       ·  --delayed-insert

       ·  --host=host_name, -h host_name

       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.

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

       ·  --add-locks

       ·  --all-databases, -A

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

          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.

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

          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.

       ·  --compress, -C

          Examples:

  --delayed-insert 在INSERT命令中加入DELAY选项

          Add a DROP DATABASE statement before each CREATE DATABASE statement.
          Added in MySQL 4.1.13.

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

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

          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.

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

       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 |
       +------+

          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.

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

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

          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.

案例:

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

          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.

       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.

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

          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”.

       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.

       ·  --no-create-db, -n

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

       For this table, mysqldump produces the following data output:

       ·  --add-drop-table

          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.

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

本文由澳门在线威尼斯官方发布于电脑数据库,转载请注明出处:mysqldump数据库备份参数详解

关键词:

上一篇:没有了

下一篇:MySQL表碎片整理,优化Mysql数据表的shell脚本