{"id":250,"date":"2023-02-24T06:08:25","date_gmt":"2023-02-23T22:08:25","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=250"},"modified":"2023-04-30T15:19:23","modified_gmt":"2023-04-30T07:19:23","slug":"mysql-import-data","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/24\/mysql-import-data\/","title":{"rendered":"MySQL \u5bfc\u5165\u6570\u636e"},"content":{"rendered":"<h2>mysql \u547d\u4ee4\u5bfc\u5165<\/h2>\n<p>\u4f7f\u7528<code>mysql<\/code>\u547d\u4ee4\u5bfc\u5165\u8bed\u6cd5\u683c\u5f0f\u4e3a\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql -u\u7528\u6237\u540d -p\u5bc6\u7801 &lt; \u8981\u5bfc\u5165\u7684\u6570\u636e\u5e93\u6570\u636e(appblog.sql)<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u5b9e\u4f8b\uff1a<\/p>\n<pre><code class=\"language-sql\"># mysql -uroot -p123456 &lt; appblog.sql<\/code><\/pre>\n<p>\u4ee5\u4e0a\u547d\u4ee4\u5c06\u5c06\u5907\u4efd\u7684\u6574\u4e2a\u6570\u636e\u5e93<code>appblog.sql<\/code>\u5bfc\u5165<\/p>\n<h2>source \u547d\u4ee4\u5bfc\u5165<\/h2>\n<p><code>source<\/code>\u547d\u4ee4\u5bfc\u5165\u6570\u636e\u5e93\u9700\u8981\u5148\u767b\u5f55\u5230\u6570\u5e93\u7ec8\u7aef\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; create database abc;      # \u521b\u5efa\u6570\u636e\u5e93\nmysql&gt; use abc;                  # \u4f7f\u7528\u5df2\u521b\u5efa\u7684\u6570\u636e\u5e93 \nmysql&gt; set names utf8;           # \u8bbe\u7f6e\u7f16\u7801\nmysql&gt; source \/home\/abc\/abc.sql  # \u5bfc\u5165\u5907\u4efd\u6570\u636e\u5e93<\/code><\/pre>\n<h2>\u4f7f\u7528 LOAD DATA \u5bfc\u5165\u6570\u636e<\/h2>\n<p>MySQL \u4e2d\u63d0\u4f9b\u4e86<code>LOAD DATA INFILE<\/code>\u8bed\u53e5\u6765\u63d2\u5165\u6570\u636e\u3002\u4ee5\u4e0b\u5b9e\u4f8b\u5c06\u4ece\u5f53\u524d\u76ee\u5f55\u4e2d\u8bfb\u53d6\u6587\u4ef6<code>dump.txt<\/code>\uff0c\u5c06\u8be5\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u63d2\u5165\u5230\u5f53\u524d\u6570\u636e\u5e93\u7684<code>mytbl<\/code>\u8868\u4e2d\u3002<\/p>\n<pre><code class=\"language-sql\">mysql&gt; LOAD DATA LOCAL INFILE &#039;dump.txt&#039; INTO TABLE mytbl;<\/code><\/pre>\n<p>\u5982\u679c\u6307\u5b9a<code>LOCAL<\/code>\u5173\u952e\u8bcd\uff0c\u5219\u8868\u660e\u4ece\u5ba2\u6237\u4e3b\u673a\u4e0a\u6309\u8def\u5f84\u8bfb\u53d6\u6587\u4ef6\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\uff0c\u5219\u6587\u4ef6\u5728\u670d\u52a1\u5668\u4e0a\u6309\u8def\u5f84\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<p>\u53ef\u4ee5\u660e\u786e\u5730\u5728<code>LOAD DATA<\/code>\u8bed\u53e5\u4e2d\u6307\u51fa\u5217\u503c\u7684\u5206\u9694\u7b26\u548c\u884c\u5c3e\u6807\u8bb0\uff0c\u4f46\u662f\u9ed8\u8ba4\u6807\u8bb0\u662f\u5b9a\u4f4d\u7b26\u548c\u6362\u884c\u7b26\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; LOAD DATA LOCAL INFILE &#039;dump.txt&#039; INTO TABLE mytbl\n  -&gt; FIELDS TERMINATED BY &#039;:&#039;\n  -&gt; LINES TERMINATED BY &#039;\\r\\n&#039;;<\/code><\/pre>\n<p>\u4e24\u4e2a\u547d\u4ee4\u7684<code>FIELDS<\/code>\u548c<code>LINES<\/code>\u5b50\u53e5\u7684\u8bed\u6cd5\u662f\u4e00\u6837\u7684\u3002\u4e24\u4e2a\u5b50\u53e5\u90fd\u662f\u53ef\u9009\u7684\uff0c\u4f46\u662f\u5982\u679c\u4e24\u4e2a\u540c\u65f6\u88ab\u6307\u5b9a\uff0c<code>FIELDS<\/code>\u5b50\u53e5\u5fc5\u987b\u51fa\u73b0\u5728<code>LINES<\/code>\u5b50\u53e5\u4e4b\u524d\u3002<\/p>\n<p>\u5982\u679c\u7528\u6237\u6307\u5b9a\u4e00\u4e2a<code>FIELDS<\/code>\u5b50\u53e5\uff0c\u5b83\u7684\u5b50\u53e5 \uff08<code>TERMINATED BY<\/code>\u3001<code>[OPTIONALLY] ENCLOSED BY<\/code>\u548c<code>ESCAPED BY<\/code>\uff09\u4e5f\u662f\u53ef\u9009\u7684\uff0c\u4e0d\u8fc7\uff0c\u7528\u6237\u5fc5\u987b\u81f3\u5c11\u6307\u5b9a\u5b83\u4eec\u4e2d\u7684\u4e00\u4e2a<\/p>\n<p><code>LOAD DATA<\/code>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u6309\u7167\u6570\u636e\u6587\u4ef6\u4e2d\u5217\u7684\u987a\u5e8f\u63d2\u5165\u6570\u636e\u7684\uff0c\u5982\u679c\u6570\u636e\u6587\u4ef6\u4e2d\u7684\u5217\u4e0e\u63d2\u5165\u8868\u4e2d\u7684\u5217\u4e0d\u4e00\u81f4\uff0c\u5219\u9700\u8981\u6307\u5b9a\u5217\u7684\u987a\u5e8f\u3002<\/p>\n<p>\u5982\uff0c\u5728\u6570\u636e\u6587\u4ef6\u4e2d\u7684\u5217\u987a\u5e8f\u662f<code>a,b,c<\/code>\uff0c\u4f46\u5728\u63d2\u5165\u8868\u7684\u5217\u987a\u5e8f\u4e3a<code>b,c,a<\/code>\uff0c\u5219\u6570\u636e\u5bfc\u5165\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; LOAD DATA LOCAL INFILE &#039;dump.txt&#039; \n    -&gt; INTO TABLE mytbl (b, c, a);<\/code><\/pre>\n<h2>\u4f7f\u7528 mysqlimport \u5bfc\u5165\u6570\u636e<\/h2>\n<p><code>mysqlimport<\/code>\u5ba2\u6237\u7aef\u63d0\u4f9b\u4e86<code>LOAD DATA INFILEQL<\/code>\u8bed\u53e5\u7684\u4e00\u4e2a\u547d\u4ee4\u884c\u63a5\u53e3\u3002<code>mysqlimport<\/code>\u7684\u5927\u591a\u6570\u9009\u9879\u76f4\u63a5\u5bf9\u5e94<code>LOAD DATA INFILE<\/code>\u5b50\u53e5\u3002<\/p>\n<p>\u4ece\u6587\u4ef6<code>dump.txt<\/code>\u4e2d\u5c06\u6570\u636e\u5bfc\u5165\u5230<code>mytbl<\/code>\u6570\u636e\u8868\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code class=\"language-sql\">$ mysqlimport -u root -p --local mytbl dump.txt\npassword *****<\/code><\/pre>\n<p><code>mysqlimport<\/code>\u547d\u4ee4\u53ef\u4ee5\u6307\u5b9a\u9009\u9879\u6765\u8bbe\u7f6e\u6307\u5b9a\u683c\u5f0f\uff0c\u547d\u4ee4\u8bed\u53e5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-sql\">$ mysqlimport -u root -p --local --fields-terminated-by=&quot;:&quot; \\\n    --lines-terminated-by=&quot;\\r\\n&quot; mytbl dump.txt\npassword *****<\/code><\/pre>\n<p><code>mysqlimport<\/code>\u8bed\u53e5\u4e2d\u4f7f\u7528<code>--columns<\/code>\u9009\u9879\u6765\u8bbe\u7f6e\u5217\u7684\u987a\u5e8f\uff1a<\/p>\n<pre><code class=\"language-sql\">$ mysqlimport -u root -p --local --columns=b,c,a \\\n    mytbl dump.txt\npassword *****<\/code><\/pre>\n<p><code>mysqlimport<\/code>\u7684\u5e38\u7528\u9009\u9879\u4ecb\u7ecd<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u9009\u9879<\/th>\n<th style=\"text-align: left;\">\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><code>-d or --delete<\/code><\/td>\n<td style=\"text-align: left;\">\u65b0\u6570\u636e\u5bfc\u5165\u6570\u636e\u8868\u4e2d\u4e4b\u524d\u5220\u9664\u6570\u636e\u6570\u636e\u8868\u4e2d\u7684\u6240\u6709\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>-f or --force<\/code><\/td>\n<td style=\"text-align: left;\">\u4e0d\u7ba1\u662f\u5426\u9047\u5230\u9519\u8bef\uff0c<code>mysqlimport<\/code>\u5c06\u5f3a\u5236\u7ee7\u7eed\u63d2\u5165\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>-i or --ignore<\/code><\/td>\n<td style=\"text-align: left;\"><code>mysqlimport<\/code>\u8df3\u8fc7\u6216\u8005\u5ffd\u7565\u90a3\u4e9b\u6709\u76f8\u540c\u552f\u4e00\u5173\u952e\u5b57\u7684\u884c\uff0c<br \/>\u5bfc\u5165\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u5c06\u88ab\u5ffd\u7565<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>-l or -lock-tables<\/code><\/td>\n<td style=\"text-align: left;\">\u6570\u636e\u88ab\u63d2\u5165\u4e4b\u524d\u9501\u4f4f\u8868\uff0c\u9632\u6b62\u5728\u66f4\u65b0\u6570\u636e\u5e93\u65f6\uff0c\u7528\u6237\u7684\u67e5\u8be2\u548c\u66f4\u65b0\u53d7\u5230\u5f71\u54cd<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>-r or -replace<\/code><\/td>\n<td style=\"text-align: left;\">\u8fd9\u4e2a\u9009\u9879\u4e0e<code>-i<\/code>\u9009\u9879\u7684\u4f5c\u7528\u76f8\u53cd\uff0c\u6b64\u9009\u9879\u5c06\u66ff\u4ee3\u8868\u4e2d\u6709\u76f8\u540c\u552f\u4e00\u5173\u952e\u5b57\u7684\u8bb0\u5f55<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>--fields-enclosed-by=char<\/code><\/td>\n<td style=\"text-align: left;\">\u6307\u5b9a\u6587\u672c\u6587\u4ef6\u4e2d\u6570\u636e\u7684\u8bb0\u5f55\u662f\u4ee5\u4ec0\u4e48\u62ec\u8d77\u7684\uff0c\u5f88\u591a\u60c5\u51b5\u4e0b\u6570\u636e\u4ee5\u53cc\u5f15\u53f7\u62ec\u8d77<br \/>\u9ed8\u8ba4\u7684\u60c5\u51b5\u4e0b\u6570\u636e\u662f\u6ca1\u6709\u88ab\u5b57\u7b26\u62ec\u8d77\u7684<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>--fields-terminated-by=char<\/code><\/td>\n<td style=\"text-align: left;\">\u6307\u5b9a\u5404\u4e2a\u6570\u636e\u7684\u503c\u4e4b\u95f4\u7684\u5206\u9694\u7b26\uff0c\u5728\u53e5\u53f7\u5206\u9694\u7684\u6587\u4ef6\u4e2d\uff0c\u5206\u9694\u7b26\u662f\u53e5\u53f7<br \/>\u53ef\u4ee5\u7528\u6b64\u9009\u9879\u6307\u5b9a\u6570\u636e\u4e4b\u95f4\u7684\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u7684\u5206\u9694\u7b26\u662f\u8df3\u683c\u7b26(Tab)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>--lines-terminated-by=str<\/code><\/td>\n<td style=\"text-align: left;\">\u6b64\u9009\u9879\u6307\u5b9a\u6587\u672c\u6587\u4ef6\u4e2d\u884c\u4e0e\u884c\u4e4b\u95f4\u6570\u636e\u7684\u5206\u9694\u5b57\u7b26\u4e32\u6216\u8005\u5b57\u7b26\uff0c\u9ed8\u8ba4\u7684\u60c5\u51b5\u4e0b<code>mysqlimport<\/code>\u4ee5<code>newline<\/code>\u4e3a\u884c\u5206\u9694\u7b26<br \/>\u53ef\u4ee5\u9009\u62e9\u7528\u4e00\u4e2a\u5b57\u7b26\u4e32\u6765\u66ff\u4ee3\u4e00\u4e2a\u5355\u4e2a\u7684\u5b57\u7b26\uff1a\u4e00\u4e2a\u65b0\u884c\u6216\u8005\u4e00\u4e2a\u56de\u8f66<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><code>mysqlimport<\/code>\u547d\u4ee4\u5e38\u7528\u7684\u9009\u9879\u8fd8\u6709<code>-v<\/code>\u663e\u793a\u7248\u672c(version)\uff0c<code>-p<\/code>\u63d0\u793a\u8f93\u5165\u5bc6\u7801(password)\u7b49<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql \u547d\u4ee4\u5bfc\u5165 \u4f7f\u7528mysql\u547d\u4ee4\u5bfc\u5165\u8bed\u6cd5\u683c\u5f0f\u4e3a\uff1a mysql -u\u7528\u6237\u540d -p\u5bc6\u7801 &lt; \u8981\u5bfc\u5165\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-250","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=250"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/250\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}