{"id":6152,"date":"2024-11-14T13:44:26","date_gmt":"2024-11-14T05:44:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6152\/"},"modified":"2024-11-14T13:44:26","modified_gmt":"2024-11-14T05:44:26","slug":"mysql%e5%a6%82%e4%bd%95%e5%ad%98%e5%82%a8%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6152\/","title":{"rendered":"mysql\u5982\u4f55\u5b58\u50a8\u56fe\u7247"},"content":{"rendered":"<blockquote><p>\n  mysql \u4e0d\u652f\u6301\u76f4\u63a5\u5b58\u50a8\u56fe\u7247\uff0c\u89e3\u51b3\u65b9\u6cd5\u6709\uff1a\u5b58\u50a8\u56fe\u7247\u7684\u8def\u5f84\uff0c\u4fdd\u6301\u6570\u636e\u5e93\u8f7b\u91cf\u7ea7\uff0c\u6613\u4e8e\u7ba1\u7406\u3002\u4f7f\u7528 blob \u7c7b\u578b\u5b58\u50a8\u56fe\u7247\uff0c\u6027\u80fd\u8f83\u597d\u3002\u6b65\u9aa4\uff1a\u521b\u5efa\u5e26\u6709 blob \u5b57\u6bb5\u7684\u8868\u3001\u4f7f\u7528 load_file() \u51fd\u6570\u52a0\u8f7d\u56fe\u7247\u3001\u4f7f\u7528 update \u8bed\u53e5\u63d2\u5165\u56fe\u7247\u3002\u4f7f\u7528\u6587\u4ef6\u7cfb\u7edf\u8868\u5f15\u64ce\uff0c\u652f\u6301 blob \u6570\u636e\u3002\u6b65\u9aa4\uff1a\u521b\u5efa\u4f7f\u7528\u6587\u4ef6\u7cfb\u7edf\u8868\u5f15\u64ce\u7684\u8868\u3001\u4f7f\u7528 load data infile \u8bed\u53e5\u5bfc\u5165\u56fe\u7247\u3002\u9009\u62e9\u65b9\u6cd5\u9700\u8003\u8651\u6027\u80fd\u3001\u7075\u6d3b\u6027\u3001\u517c\u5bb9\u6027\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/14\/2024061402482646004.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u5b58\u50a8\u56fe\u7247\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u5b58\u50a8\u56fe\u7247\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u5b58\u50a8\u56fe\u7247<\/strong><\/p>\n<p>MySQL \u662f\u4e00\u6b3e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u4e0d\u652f\u6301\u76f4\u63a5\u5b58\u50a8\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61\uff08BLOB\uff09\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ec\u56fe\u7247\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u51e0\u79cd\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u5b58\u50a8\u56fe\u7247\u7684\u8def\u5f84<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5c06\u56fe\u7247\u5b58\u50a8\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\uff0c\u5e76\u5728 MySQL \u4e2d\u5b58\u50a8\u5176\u8def\u5f84\u3002\u8fd9\u6837\u53ef\u4ee5\u4fdd\u6301\u6570\u636e\u5e93\u7684\u8f7b\u91cf\u7ea7\uff0c\u5e76\u4e14\u4fbf\u4e8e\u7ba1\u7406\u56fe\u7247\u3002<\/p>\n<p><strong>2. \u4f7f\u7528 BLOB \u7c7b\u578b<\/strong><\/p>\n<p>MySQL \u53ef\u4ee5\u901a\u8fc7 BLOB\uff08Binary Large OBject\uff09\u7c7b\u578b\u5b58\u50a8\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5305\u62ec\u56fe\u7247\u3002\u4f46\u662f\uff0c\u8868\u7ed3\u6784\u9700\u8981\u4fee\u6539\u4e3a\u5305\u542b\u4e00\u4e2a BLOB \u5b57\u6bb5\uff0c\u5e76\u4e14\u9700\u8981\u4f7f\u7528\u7279\u6b8a\u51fd\u6570\u6765\u5b58\u50a8\u548c\u68c0\u7d22\u56fe\u7247\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a\u5e26\u6709 BLOB \u5b57\u6bb5\u7684\u8868\uff1a<\/p>\n<pre>CREATE TABLE images (\nid INT NOT NULL AUTO_INCREMENT,\nimage BLOB,\nPRIMARY KEY (id)\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u4f7f\u7528 LOAD_FILE() \u51fd\u6570\u5c06\u56fe\u7247\u52a0\u8f7d\u5230 BLOB \u5b57\u6bb5\u4e2d\uff1a<\/p>\n<pre>LOAD_FILE('\/path\/to\/image.jpg') INTO OUTFILE @image_field;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u4f7f\u7528 UPDATE \u8bed\u53e5\u5c06 @image_field \u7684\u503c\u63d2\u5165\u5230 image \u5b57\u6bb5\uff1a<\/p>\n<pre>UPDATE images SET image = @image_field WHERE id = 1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>3. \u4f7f\u7528\u6587\u4ef6\u7cfb\u7edf\u8868\u5f15\u64ce<\/strong><\/p>\n<p>MySQL \u7684 InnoDB \u8868\u5f15\u64ce\u4e0d\u652f\u6301 BLOB \u7c7b\u578b\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u7cfb\u7edf\u8868\u5f15\u64ce\uff08\u5982 MyISAM\uff09\uff0c\u5b83\u652f\u6301 BLOB \u6570\u636e\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a\u4f7f\u7528\u6587\u4ef6\u7cfb\u7edf\u8868\u5f15\u64ce\u7684\u8868\uff1a<\/p>\n<pre>CREATE TABLE images (\nid INT NOT NULL AUTO_INCREMENT,\nimage BLOB,\nPRIMARY KEY (id)\n) ENGINE=MyISAM;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u4f7f\u7528 LOAD DATA INFILE \u8bed\u53e5\u5bfc\u5165\u56fe\u7247\uff1a<\/p>\n<pre>LOAD DATA INFILE '\/path\/to\/image.jpg' INTO TABLE images (image);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>\u9009\u62e9\u65b9\u6cd5\u7684\u8003\u8651\u56e0\u7d20\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u6027\u80fd\uff1a<\/strong> BLOB \u7c7b\u578b\u63d0\u4f9b\u4e86\u8f83\u597d\u7684\u6027\u80fd\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u5927\u578b\u56fe\u7247\u3002<\/li>\n<li> <strong>\u7075\u6d3b\u6027\uff1a<\/strong> \u5b58\u50a8\u56fe\u7247\u7684\u8def\u5f84\u66f4\u52a0\u7075\u6d3b\uff0c\u4fbf\u4e8e\u7ba1\u7406\u548c\u79fb\u52a8\u56fe\u7247\u3002<\/li>\n<li> <strong>\u517c\u5bb9\u6027\uff1a<\/strong> BLOB \u7c7b\u578b\u53ef\u80fd\u4e0e\u67d0\u4e9b MySQL \u5de5\u5177\u4e0d\u517c\u5bb9\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5b58\u50a8\u56fe\u7247\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql \u4e0d\u652f\u6301\u76f4\u63a5\u5b58\u50a8\u56fe\u7247\uff0c\u89e3\u51b3\u65b9\u6cd5\u6709\uff1a\u5b58\u50a8\u56fe\u7247\u7684\u8def\u5f84\uff0c\u4fdd\u6301\u6570\u636e\u5e93\u8f7b\u91cf\u7ea7\uff0c\u6613\u4e8e\u7ba1\u7406\u3002\u4f7f\u7528 blob \u7c7b\u578b\u5b58\u50a8\u56fe\u7247\uff0c\u6027\u80fd\u8f83\u597d\u3002\u6b65\u9aa4\uff1a\u521b\u5efa\u5e26\u6709 blob \u5b57\u6bb5\u7684\u8868\u3001\u4f7f\u7528 load_file() \u51fd\u6570\u52a0\u8f7d\u56fe\u7247\u3001\u4f7f\u7528 update \u8bed\u53e5\u63d2\u5165\u56fe\u7247\u3002\u4f7f\u7528\u6587\u4ef6\u7cfb\u7edf\u8868\u5f15\u64ce\uff0c\u652f\u6301 blob \u6570\u636e\u3002\u6b65\u9aa4\uff1a\u521b\u5efa\u4f7f\u7528\u6587\u4ef6\u7cfb\u7edf\u8868\u5f15\u64ce\u7684\u8868\u3001\u4f7f\u7528 load data infile \u8bed\u53e5\u5bfc\u5165\u56fe\u7247\u3002\u9009\u62e9\u65b9\u6cd5\u9700\u8003\u8651\u6027\u80fd\u3001\u7075\u6d3b\u6027\u3001\u517c\u5bb9\u6027\u3002 \u5982\u4f55\u4f7f\u7528 MySQL \u5b58\u50a8\u56fe\u7247 MySQL \u662f\u4e00\u6b3e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u4e0d\u652f\u6301\u76f4\u63a5\u5b58\u50a8\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61\uff08BLOB\uff09\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ec\u56fe\u7247\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u51e0\u79cd\u89e3\u51b3\u65b9\u6cd5\uff1a 1. \u5b58\u50a8\u56fe\u7247\u7684\u8def\u5f84 \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5c06\u56fe\u7247\u5b58\u50a8\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\uff0c\u5e76\u5728 MySQL \u4e2d\u5b58\u50a8\u5176\u8def\u5f84\u3002\u8fd9\u6837\u53ef\u4ee5\u4fdd\u6301\u6570\u636e\u5e93\u7684\u8f7b\u91cf\u7ea7\uff0c\u5e76\u4e14\u4fbf\u4e8e\u7ba1\u7406\u56fe\u7247\u3002 2. \u4f7f\u7528 BLOB \u7c7b\u578b MySQL \u53ef\u4ee5\u901a\u8fc7 BLOB\uff08Binary Large OBject\uff09\u7c7b\u578b\u5b58\u50a8\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5305\u62ec\u56fe\u7247\u3002\u4f46\u662f\uff0c\u8868\u7ed3\u6784\u9700\u8981\u4fee\u6539\u4e3a\u5305\u542b\u4e00\u4e2a BLOB \u5b57\u6bb5\uff0c\u5e76\u4e14\u9700\u8981\u4f7f\u7528\u7279\u6b8a\u51fd\u6570\u6765\u5b58\u50a8\u548c\u68c0\u7d22\u56fe\u7247\u3002 \u6b65\u9aa4\uff1a \u521b\u5efa\u4e00\u4e2a\u5e26\u6709 BLOB \u5b57\u6bb5\u7684\u8868\uff1a CREATE TABLE images ( id INT NOT NULL AUTO_INCREMENT, image BLOB, PRIMARY KEY (id) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6152","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6152"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6152\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}