[匯出]8 x) T# i( I4 X/ z. b1 `
mysqldump -u userid -e -p db_Name > xxxxx.sql# z" i: p! d" b$ w2 ~: Q- N
匯出一個table:mysqldump -p -u userid dbname tablename > 52avtv_dzx20.201506242307.sql& l+ T: I* `8 P, y# v* A* L
匯出所有資料庫: mysqldump -p -u userid --all-databases > all-database.sql 8 d+ ]/ _2 i3 p! t& U4 |
但是,匯到目的資料庫的使用者帳號及密碼有問題,所以還是每個資料庫個別搬,使用者帳號也個別匯出比較安全
) m3 Z) b7 N8 [& j6 B' z7 C a[只匯出結構] 參考: https://iter01.com/206843.html
0 W4 E- i" @! vmysqldump --opt -d 資料庫名 -u root -p > xxx.sql
+ {6 H) u, B# `- s2 q: j2 M[只匯出資料]2 c4 f) x3 Z x( k# R, I; Q
mysqldump -t 資料庫名 -uroot -p > xxx.sql5 P& @; T" @3 T& t4 q/ T
6 M0 y" Q7 k, o) r+ M* i5 H" |2 Q- Z注意: 當以上述指令備份時造成網站出現 "502 bad gateway",影響到nginx,只要加參數 --single-transaction --quick 就好了,如下:2 A: S3 R! G# m! P' y( \& N1 m
mysqldump -u userid -e -p --single-transaction --quick db_Name > xxxxx.sql
) D4 J3 W7 P! u+ G: ~9 |2 {* ~( b問題:mysqldump: Got error: 1017: Can't find file: 'pre_forum_rsscache' (errno: 2) when using LOCK TABLES
8 E4 G* ]' v3 ?9 ~+ `, {6 DSol: 只要在mysqldump的時候加上--lock-tables=false就可以解決問題。6 U! a* ]# u7 M! c4 e! Q
接著又出現: mysqldump: Couldn't execute 'show create table `pre_forum_rsscache`': Can't find file: 'pre_forum_rsscache' (errno: 2) (1017),以phpmyadmin登入查看卻看到pre_forum_rsscache 使用中ls /var/lib/mysql/52avtv_dz/pre_forum_rsscache* 發現只有 pre_forum_rsscache.frm 1個檔案 ,正常應該要有3個檔:; l) `, Y( o. y9 ]1 U* i9 G& |" N
-rw-rw---- 1 mysql mysql 8852 Jun 3 16:05 pre_forum_rsscache.frm2 \* D& h5 A( H, t# _
-rw-rw---- 1 mysql mysql 0 Jun 3 16:05 pre_forum_rsscache.MYD
* B$ h8 S6 @7 ~) ]-rw-rw---- 1 mysql mysql 1024 Jun 3 16:05 pre_forum_rsscache.MYI
4 @* z+ b6 h$ _9 c: pSol: r/ ]9 H6 @, i
在phpmyadmin ==SQL 下指令:1 ]7 p Q9 [# r G
- DROP TABLE IF EXISTS pre_forum_rsscache;
- CREATE TABLE pre_forum_rsscache (
- lastupdate int(10) unsigned NOT NULL DEFAULT '0',
- fid mediumint(8) unsigned NOT NULL DEFAULT '0',
- tid mediumint(8) unsigned NOT NULL DEFAULT '0',
- dateline int(10) unsigned NOT NULL DEFAULT '0',
- forum char(50) NOT NULL DEFAULT '',
- author char(15) NOT NULL DEFAULT '',
- `subject` char(80) NOT NULL DEFAULT '',
- description char(255) NOT NULL DEFAULT '',
- guidetype char(10) NOT NULL DEFAULT '',
- UNIQUE KEY tid (tid),
- KEY fid (fid,dateline)
- ) ENGINE=MYISAM DEFAULT CHARSET=utf8
複製代碼 & ^* s4 \* v- f( R
再備份就正常了4 O8 W" l! u" F0 U2 u, D1 N: D
問題: 當使用mariadb 5.5.68 備份時出現error:mysqldump: Couldn't execute 'SHOW TRIGGERS LIKE 'pre\_portal\_topic\_pic'': Can't read dir of './db_name/' (errno: 24) (1018)! U. I& k! q' E6 R; A
Sol: 這是open_files_limit(default=962) 太小,改為4096,由於它屬於read-only,故加在/etc/my.cnf
5 ~/ P* O6 [' S3 K3 p2 T$ W( {# u然後 service mariadb restart# v- u+ E% i {, U
]' \! E; x. h[有條件匯出]
6 ?9 A7 d S7 Y" g, Dmysqldump -u帳號 -p密碼 -h主機 資料庫 資料表 -w "sql條件" > 出輸路徑及檔案% l9 E6 S3 _! e- d
例:
. z! n! P$ i$ G3 R5 [mysqldump -uroot -p123456 -hlocalhost -e AREA_UTF8 city -w "c_id<10 " > /home/web/a.txt
- t0 X* \4 Q% e+ y! m4 C" H* _8 ?* N * i5 f) d7 p1 @; S; y
--no-create-info,-t6 i7 K' E) f8 U4 K( ^! U
只導出資料,而不添加 CREATE TABLE 語句;如果導出格式為SQL語句,則只有insert into部分。* K% p9 ?: T% }" [ \
--no-data,-d5 K6 W5 r8 j6 R. V) H* |
不導出任何資料,只導出資料庫結構6 A9 _0 o& u" i1 b/ ?& K9 a
--quick,-q 3 H* R7 t5 ~' V) F; f
在導出大量資料很有用,強制從 MySQL Server 查詢取得記錄直接輸出,而不是取得所有記錄後存在記憶體中。
( i& Q. s. y1 ^( Q" I* B7 H5 ~2 M0 X9 }6 B5 L
" O x9 b5 y; y+ x; z[匯入]. O9 N# ]2 K+ u! r* s1 a
mysql -u userid -p [-h localhost] db_Name < xxxxx.sql
% f) N5 D) |- d' @2 a' ?8 O匯入所有資料庫: mysql -u userid -p < all-database.sql1 ?+ E$ J) x9 {
匯入一個table :0 o6 F! P8 a+ K& k X: N: Y
mysql -u userid -p -D dbname < cc5278_dzx20.ip2c.201506242307.sql# Z# S9 B( W& P9 m8 W& a
P.S如果匯入檔太大如7GB,則匯入檔案之前執行下列指令:
4 F8 v) [' k: I4 ~mysql -uroot -p -e "set global net_buffer_length=1000000; set global max_allowed_packet=1000000000;"0 p7 f' x0 N0 w1 H* \
: a( x! G% Q. k' x% t2 o+ e
[匯出後立即匯入到另一台主機]2 J: Z( e5 e2 H0 j( p8 ~
mysqldump -u [SOURCE_userid] -h [SOURCE_IP] -e -q --password=[SOURCE_PASSWORD] SOURCE_db_Name | mysql -u [DISTANCE_userid] --password=[DISTANCE_PASSWORD] -h [DISTANCE_IP] DISTANCE_db_Name
3 f: {% T8 g( a* u! u( P9 o[設定匯入上限(內定2Mb) by phpmyadmin]- @5 Y3 ]; V) y+ u2 g/ ^% [* t6 F: V
參考http://www.av4u.info/forum.php?m ... e=1&extra=#pid203488 i6 [) N8 W1 W/ f; f
9 K+ Z$ Y7 j3 b- d9 }7 L) |; s6 d, P1 b6 N
+ c8 k: u+ @7 v7 Y1 \6 a6 U5 |
- s9 z- [+ U, N% @
$ p+ N1 n$ U0 \- E( C- W& `9 D u4 U9 F
! g+ b7 W' n1 f8 s/ W! h
& j6 t7 \' W% Y; d
# O4 D& ~% F- ~# I$ b* W i- G2 U: ?/ L1 t8 t4 j7 U, B
: X( u# t& q/ u. b4 Q9 l9 U6 B% j7 ]( f- o( k# k p5 d
+ F$ Y0 ]9 i% Q" L3 w: V& }
0 ~ ~5 j" y! l8 S( K$ z |
|