Mysqldump总结

Mysqldump总结

 

原理:其实 就是把数据从mysql库里以逻辑的sql语句的形式直接输出或者生成备份文件的过程。

source 命令也可以恢复,很简单,就是use数据库+ source +/path/all.sql

[root@mysql-double ~]# cat mysqldump.txt

1.mysqldump -uroot -p -S/data/3308/mysql.sock test > test.sql    备份

                                       库名   定向到哪

2.mysqldump -uroot -p111111 -S/data/3308/mysql.sock -Btest > B.sql  -B

                  -B 参数很重要  就是增加了create命令和use使用库和接多个库名

3.[root@mysql-double ~]# mysqldump -uroot -p111111 -S/data/3308/mysql.sock -B test | gzip> B.sql.gz     加上gzip压缩,减少空间,效率很高   解压 gzip �d  B.sql.gz

 

4.[root@mysql-double ~]# egrep -v "#|\*|--|^$" test.sql   可以过滤查看备份内容

 

5.[root@mysql-double tmp]# mysqldump -uroot -p111111 -S/data/3308/mysql.sock -B test tangbo mysql| gzip> double.sql.gz  备份多个库

 

6. [root@mysql-double tmp]# mysql-uroot -p'111111' -S /data/3308/mysql.sock -e "show databases;" | grep -Evi"Database|infor|perf" | sed -r 's#^([a-z].*$)#mysqldump -uroot-p'111111' -S /data/3308/mysql.sock --events -B \1 | gzip > /tmp/logs/\1.sql.gz#g' | bash

       分库备份: 一键备份多个库,在分库备份的时候使用这条命令

7.总结

  MyISAM

全备: mysql -uroot-p'111111' -S /data/3308/mysql.sock -A  -B -F �flush-privileges �master-data=2 �x -�events | gzip > /opt/all.sql.gz

 

InnoDB:推荐使用这种

 全备:mysql -uroot -p'111111' -S/data/3308/mysql.sock  -A  -B �F �flush-privileges �master-data=2 �single-transaction �events | gzip > /opt/all.sql.gz

 

上诉两种备份如果数据库有存储过程和触发器还得加两个参数:

  --triggers �routines  --hex-blob,一般公司无这三个参数。

   触发器   存储过程  如果你库中有blob字段,而你又没加这个参数 ,那你的blog大字段数据就会丢失

 

 

--print-defaults        Print the program argument list andexit.

--no-defaults           Don't read default options from anyoption file,

                        except for login file.

--defaults-file=#       Only read default options from the givenfile #.

--defaults-extra-file=# Read this fileafter the global files are read.

--defaults-group-suffix=#

                        Also read groups withconcat(group, suffix)

--login-path=#          Read this path from the login file.

  -A, --all-databases Dump  导出所有数据,一般加-B 共用+--events all the databases. This will be same as --databases

                      with all databasesselected.

  -Y,--all-tablespaces

                      Dump all the tablespaces.

  -y,--no-tablespaces

                      Do not dump anytablespace information.

 --add-drop-database Add a DROP DATABASE before each create.

 --add-drop-table    Add a DROPTABLE before each create.

                      (Defaults to on; use--skip-add-drop-table to disable.)

 --add-drop-trigger  Add a DROPTRIGGER before each create.

 --add-locks         Add locksaround INSERT statements.

                      (Defaults to on; use--skip-add-locks to disable.)

 --allow-keywords    Allow creationof column names that are keywords.

 --apply-slave-statements

                      Adds 'STOP SLAVE' priorto 'CHANGE MASTER' and 'START

                     SLAVE' to bottom ofdump.

 --bind-address=name IP address to bind to.

 --character-sets-dir=name

                      Directory for characterset files.

  -i,--comments      Write additionalinformation.

                      (Defaults to on; use--skip-comments to disable.)

 --compatible=name   Change thedump to be compatible with a given mode. By

                      default tables are dumpedin a format optimized for

                      MySQL. Legal modes are:ansi, mysql323, mysql40,

                      postgresql, oracle,mssql, db2, maxdb, no_key_options,

                      no_table_options,no_field_options. One can use several

                      modes separated bycommas. Note: Requires MySQL server

                      version 4.1.0 or higher. This option isignored with

                      earlier server versions.

  --compact          备份时加了该参数,可以减少输出没用的信息,生产环境一般不用

                    它会忽略一些信息,如droptable  加锁等。

  -c,--complete-insert

                      Use complete insertstatements.

  -C,--compress      Use compression inserver/client protocol.

  -a,--create-options

                      Include all MySQLspecific create options.

                      (Defaults to on; use--skip-create-options to disable.)

 

  -B, --databases    指定多个库名备份    直观看,加上-B参数作用是增加创建数据库和连接数据库的命令,生产环境备份必用。

[root@mysql-double ~]# mysqldump -uroot -p111111 -S/data/3308/mysql.sock -B test > B.sql

 

[root@mysql-double ~]# diff test.sql B.sql

18a19,26

> -- Current Database: `test`

> --

>

> CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test` /*!40100 DEFAULTCHARACTER SET utf8 */;

>

> USE `test`;

>

> --

50c58

< -- Dump completed on 2016-01-05 14:58:32

---

> -- Dump completed on 2016-01-05 15:20:36

[root@mysql-double ~]#

 

  -#,--debug[=#]     This is a non-debugversion. Catch this and exit.

 --debug-check       Check memoryand open file usage at exit.

 --debug-info        Print somedebug info at exit.

 --default-character-set=name

                      Set the default characterset.

 --delayed-insert    Insert rowswith INSERT DELAYED.

 --delete-master-logs

                      Delete logs on masterafter backup. This automatically

                      enables --master-data.

  -K,--disable-keys  '/*!40000 ALTER TABLEtb_name DISABLE KEYS */; and

                      '/*!40000 ALTER TABLEtb_name ENABLE KEYS */; will be put

                      in the output.

                      (Defaults to on; use--skip-disable-keys to disable.)

 --dump-slave[=#]    This causesthe binary log position and filename of the

                      master to be appended tothe dumped data output. Setting

                      the value to 1, willprintit as a CHANGE MASTER command

                     in the dumped dataoutput; if equal to 2, that command

                      will be prefixed with acomment symbol. This option will

                      turn --lock-all-tableson, unless --single-transaction is

                      specified too (in whichcase a global read lock is only

                      taken a short time at thebeginning of the dump - don't

                      forget to read about--single-transaction below). In all

                      cases any action on logswill happen at the exact moment

                      of the dump.Optionautomatically turns --lock-tables off.

  -E, --events        Dump events.

  -e,--extended-insert

                      Use multiple-row INSERTsyntax that include several

                      VALUES lists.

                      (Defaults to on; use--skip-extended-insert to disable.)

 --fields-terminated-by=name

                      Fields in the output fileare terminated by the given

                      string.

 --fields-enclosed-by=name

                      Fields in the output fileare enclosed by the given

                      character.

 --fields-optionally-enclosed-by=name

                      Fields in the output fileare optionally enclosed by the

                      given character.

 --fields-escaped-by=name

                      Fields in the output fileare escaped by the given

                      character.

  -F, --flush-logs     刷新,切割binlog  Flush logs file in server before startingdump. Note that

                      if you dumpmany databases at once (using the option

                     --databases= or --all-databases), the logs will be

                      flushed foreach database dumped. The exception is when

                      using --lock-all-tables or --master-data: inthis case

                      the logswill be flushed only once, corresponding to the

                      moment alltables are locked. So if you want your dump

                      and the logflush to happen at the same exact moment you

                      should use--lock-all-tables or --master-data with

                     --flush-logs.

 --flush-privileges  Emit a FLUSHPRIVILEGES statement after dumping the mysql

                      database.  This option should be used any time the dump

                      contains the mysqldatabase and any other database that

                      depends on the data inthe mysql database for proper

                      restore.

  -f,--force         Continue even if we getan SQL error.

  -?,--help          Display this help messageand exit.

 --hex-blob          Dump binarystrings (BINARY, VARBINARY, BLOB) in

                      hexadecimal format.

  -h,--host=name     Connect to host.

 --ignore-table=name Do not dump the specified table. To specify morethan one

                      table to ignore, use thedirective multiple times, once

                      for each table.  Each table must be specified with both

                      database and table names,e.g.,

                     --ignore-table=database.table.

 --include-master-host-port

                      Adds'MASTER_HOST=<host>, MASTER_PORT=<port>' to 'CHANGE

                      MASTER TO..' in dumpproduced with --dump-slave.

 --insert-ignore     Insert rowswith INSERT IGNORE.

 --lines-terminated-by=name

                      Lines in the output fileare terminated by the given

                      string.

 -x,     锁表 --lock-all-tables

                      Locks all tables acrossall databases. This is achieved

                      by taking a global readlock for the duration of the

                      whole dump. Automaticallyturns --single-transaction and

                      --lock-tables off.

  -l, --lock-tables  只读锁表 Lock all tables forread.

                      (Defaults to on; use--skip-lock-tables to disable.)

 --log-error=name    Appendwarnings and errors to given file.

 --master-data[=#]    自动找到binlog的位置  This causes the binary log position andfilename to be

   如:--master-data=1    可以不用刷新binlog了,做增量备份的时候很有用  

       --master-data=2,后会加个注释,好找些

 --max-allowed-packet=#

                      The maximum packet lengthto send to or receive from

                      server.

 --net-buffer-length=#

                      The buffer size forTCP/IP and socket communication.

 --no-autocommit     Wrap tableswith autocommit/commit statements.

  -n,--no-create-db  Suppress the CREATE DATABASE... IF EXISTS statement that

                      normally is output foreach dumped database if

                      --all-databases or--databases is given.

  -t,--no-create-info     如果希望只导出表数据

                      Don't write tablecreation info.

  -d, --no-data           只备份表结构No row information.

  -N,--no-set-names  Same as--skip-set-charset.

 --opt               Same as--add-drop-table, --add-locks, --create-options,

                      --quick, --extended-insert, --lock-tables,--set-charset,

                      and --disable-keys.Enabled by default, disable with

                      --skip-opt.

 --order-by-primary  Sorts eachtable's rows by primary key, or first unique

                     key, if such a keyexists.  Useful when dumping a MyISAM

                      table to be loaded intoan InnoDB table, but will make

                      the dump itself takeconsiderably longer.

  -p,--password[=name]

                      Password to use when connecting to server. Ifpassword is

                      not given it's solicitedon the tty.

  -P,--port=#        Port number to use forconnection.

 --protocol=name     The protocolto use for connection (tcp, socket, pipe,

                      memory).

  -q,--quick         Don't buffer query, dumpdirectly to stdout.

                      (Defaults to on; use--skip-quick to disable.)

  -Q,--quote-names   Quote table and columnnames with backticks (`).

                      (Defaults to on; use --skip-quote-names todisable.)

 --replace           Use REPLACEINTO instead of INSERT INTO.

  -r,--result-file=name

                      Direct output to a givenfile. This option should be used

                      in systems (e.g., DOS,Windows) that use carriage-return

                      linefeed pairs (\r\n) toseparate text lines. This option

                      ensures that only asingle newline is used.

  -R,--routines      Dump stored routines(functions and procedures).

 --set-charset       Add 'SET NAMESdefault_character_set' to the output.

                      (Defaults to on; use--skip-set-charset to disable.)

 --set-gtid-purged[=name]

                      Add 'SET@@GLOBAL.GTID_PURGED' to the output. Possible

                      values for this optionare ON, OFF and AUTO. If ON is

                      used and GTIDs are notenabled on the server, an error is

                      generated. If OFF isused, this option does nothing. If

                      AUTO is used and GTIDsare enabled on the server, 'SET

                      @@GLOBAL.GTID_PURGED' isadded to the output. If GTIDs

                      are disabled, AUTO does nothing.If no value is supplied

                      then the default (AUTO)value will be considered.

  --single-transaction    MySIM直接选-x参数锁表,InnoDB选择这个参数来保证备份

的一致性。相当于设置一个隔离级别,REPEATABLE READ ,以确保本次会话dump时,不会看到其它会话已经提交的数据。

                      Creates a consistentsnapshot by dumping all tables in a

                      single transaction. WorksONLY for tables stored in

                      storage engines whichsupport multiversioning (currently

                      only InnoDB does); thedump is NOT guaranteed to be

                      consistent for otherstorage engines. While a

                      --single-transaction dumpis in process, to ensure a

                      valid dump file (correcttable contents and binary log

                      position), no other connection shoulduse the following

                      statements: ALTER TABLE,DROP TABLE, RENAME TABLE,

                      TRUNCATE TABLE, asconsistent snapshot is not isolated

                      from them. Option automaticallyturns off --lock-tables.

 --dump-date         Put a dumpdate to the end of the output.

                      (Defaults to on; use--skip-dump-date to disable.)

 --skip-opt          Disable --opt.Disables --add-drop-table, --add-locks,

                     --create-options,--quick, --extended-insert,

                      --lock-tables,--set-charset, and --disable-keys.

  -S,--socket=name   The socket file to usefor connection.

 --secure-auth       Refuse clientconnecting to server if it uses old

                      (pre-4.1.1) protocol.

                      (Defaults to on; use--skip-secure-auth to disable.)

 --ssl               Enable SSL forconnection (automatically enabled with

                      other flags).

 --ssl-ca=name       CA file in PEMformat (check OpenSSL docs, implies

                      --ssl).

 --ssl-capath=name   CA directory(check OpenSSL docs, implies --ssl).

 --ssl-cert=name     X509 cert inPEM format (implies --ssl).

 --ssl-cipher=name   SSL cipher touse (implies --ssl).

 --ssl-key=name      X509 key inPEM format (implies --ssl).

 --ssl-crl=name      Certificaterevocation list (implies --ssl).

 --ssl-crlpath=name  Certificaterevocation list path (implies --ssl).

 --ssl-verify-server-cert

                      Verify server's"Common Name" in its cert against

                      hostname used whenconnecting. This option is disabled by

                      default.

  -T, --tab=name      可以把表结构和内容分开Create tab-separated textfile for each table to

ump is run on the same machine as themysqld

                      server.

 --tables            Overridesoption --databases (-B).

 --triggers          Dump triggersfor each dumped table.

                      (Defaults to on; use--skip-triggers to disable.)

 --tz-utc            SETTIME_ZONE='+00:00' at top of dump to allow dumping of

                      TIMESTAMP data when aserver has data in different time

                      zones or data is beingmoved between servers with

                      different time zones.

                      (Defaults to on; use--skip-tz-utc to disable.)

  -u,--user=name     User for login if notcurrent user.

  -v,--verbose       Print info about thevarious stages.

  -V,--version       Output versioninformation and exit.

  -w,--where=name    Dump only selectedrecords. Quotes are mandatory.

  -X,--xml           Dump a database as wellformed XML.

 --plugin-dir=name   Directory forclient-side plugins.

 --default-auth=name Default authentication client-side plugin to use.

 

Variables (--variable-name=value)

and boolean options {FALSE|TRUE}  Value (after reading options)

--------------------------------- ----------------------------------------

all-databases                     FALSE

all-tablespaces                   FALSE

no-tablespaces                    FALSE

add-drop-database                 FALSE

add-drop-table                    TRUE

add-drop-trigger                  FALSE

add-locks                         TRUE

allow-keywords                    FALSE

apply-slave-statements            FALSE

bind-address                      (No default value)

character-sets-dir                (No default value)

comments                          TRUE

compatible                        (No default value)

compact                           FALSE

complete-insert                   FALSE

compress                          FALSE

create-options                    TRUE

databases                         FALSE

debug-check                       FALSE

debug-info                        FALSE

default-character-set             utf8

delayed-insert                    FALSE

delete-master-logs                FALSE

disable-keys                      TRUE

dump-slave                        0

events                            FALSE

extended-insert                   TRUE

fields-terminated-by              (No default value)

fields-enclosed-by                (No default value)

fields-optionally-enclosed-by     (No default value)

fields-escaped-by                 (No default value)

flush-logs                        FALSE

flush-privileges                  FALSE

force                             FALSE

hex-blob                          FALSE

host                              (No default value)

include-master-host-port          FALSE

insert-ignore                     FALSE

lines-terminated-by               (No default value)

lock-all-tables                   FALSE

lock-tables                       TRUE

log-error                         (No default value)

master-data                       0

max-allowed-packet                25165824

net-buffer-length                 1046528

no-autocommit                     FALSE

no-create-db                      FALSE

no-create-info                    FALSE

no-data                           FALSE

order-by-primary                  FALSE

port                              0

quick                             TRUE

quote-names                       TRUE

replace                           FALSE

routines                          FALSE

set-charset                       TRUE

single-transaction                FALSE

dump-date                         TRUE

socket                            (No default value)

secure-auth                       TRUE

ssl                               FALSE

ssl-ca                            (No default value)

ssl-capath                        (No default value)

ssl-cert                          (No default value)

ssl-cipher                        (No default value)

ssl-key                           (No default value)

ssl-crl                           (No default value)

ssl-crlpath                       (No default value)

ssl-verify-server-cert            FALSE

tab                               (No default value)

triggers                          TRUE

tz-utc                            TRUE

user                              (No defaultvalue)

verbose                           FALSE

where                             (No default value)

plugin-dir                        (No default value)

default-auth                      (No default value)

[root@mysql-double ~]#

本文出自 “山猫” 博客,谢绝转载!

你可能感兴趣的:(mysq,dump)