{"id":4968,"date":"2022-04-01T21:19:42","date_gmt":"2022-04-01T13:19:42","guid":{"rendered":"https:\/\/egonlin.com\/?p=4968"},"modified":"2022-04-26T14:29:41","modified_gmt":"2022-04-26T06:29:41","slug":"11-%e6%b0%b8%e8%bf%9c%e4%b8%8d%e8%a6%81%e5%9c%a8mysql%e4%b8%ad%e4%bd%bf%e7%94%a8utf-8","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=4968","title":{"rendered":"11 \u6c38\u8fdc\u4e0d\u8981\u5728MySQL\u4e2d\u4f7f\u7528UTF-8"},"content":{"rendered":"<blockquote>\n<p>\u8bd1\u6587:<a href=\"http:\/\/suo.im\/4zBuvs\">http:\/\/suo.im\/4zBuvs<\/a><br \/>\n\u6765\u81ea:<a href=\"http:\/\/ju.outofmemory.cn\">http:\/\/ju.outofmemory.cn<\/a><\/p>\n<\/blockquote>\n<p>\u6700\u8fd1\u6211\u9047\u5230\u4e86\u4e00\u4e2abug\uff0c\u6211\u8bd5\u7740\u901a\u8fc7Rails\u5728\u4ee5\u201cutf8\u201d\u7f16\u7801\u7684MariaDB\u4e2d\u4fdd\u5b58\u4e00\u4e2aUTF-8\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u51fa\u73b0\u4e86\u4e00\u4e2a\u79bb\u5947\u7684\u9519\u8bef\uff1a<\/p>\n<pre><code class=\"language-mysql\">Incorrectstring value:\u2018\\xF0\\x9F\\x98\\x83 &lt;\u2026\u2019 for column \u2018summary\u2019 at row 1<\/code><\/pre>\n<p>\u6211\u7528\u7684\u662fUTF-8\u7f16\u7801\u7684\u5ba2\u6237\u7aef\uff0c\u670d\u52a1\u5668\u4e5f\u662fUTF-8\u7f16\u7801\u7684\uff0c\u6570\u636e\u5e93\u4e5f\u662f\uff0c\u5c31\u8fde\u8981\u4fdd\u5b58\u7684\u8fd9\u4e2a\u5b57\u7b26\u4e32\u201c &lt;\u2026\u201d\u4e5f\u662f\u5408\u6cd5\u7684UTF-8\u3002<\/p>\n<p>\u95ee\u9898\u7684\u75c7\u7ed3\u5728\u4e8e\uff0cMySQL\u7684\u201cutf8\u201d\u5b9e\u9645\u4e0a\u4e0d\u662f\u771f\u6b63\u7684UTF-8\u3002<\/p>\n<p><code>utf8<\/code>\u53ea\u652f\u6301\u6bcf\u4e2a\u5b57\u7b26\u6700\u591a\u4e09\u4e2a\u5b57\u8282\uff0c\u800c\u771f\u6b63\u7684UTF-8\u662f\u6bcf\u4e2a\u5b57\u7b26\u6700\u591a\u56db\u4e2a\u5b57\u8282\u3002<\/p>\n<p>MySQL\u4e00\u76f4\u6ca1\u6709\u4fee\u590d\u8fd9\u4e2abug\uff0c\u4ed6\u4eec\u57282010\u5e74\u53d1\u5e03\u4e86\u4e00\u4e2a\u53eb\u4f5c<code>utf8mb4<\/code>\u7684\u5b57\u7b26\u96c6\uff0c\u7ed5\u8fc7\u4e86\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u4ed6\u4eec\u5e76\u6ca1\u6709\u5bf9\u65b0\u7684\u5b57\u7b26\u96c6\u5e7f\u800c\u544a\u4e4b\uff08\u53ef\u80fd\u662f\u56e0\u4e3a\u8fd9\u4e2abug\u8ba9\u4ed6\u4eec\u89c9\u5f97\u5f88\u5c34\u5c2c\uff09\uff0c\u4ee5\u81f4\u4e8e\u73b0\u5728\u7f51\u7edc\u4e0a\u4ecd\u7136\u5728\u5efa\u8bae\u5f00\u53d1\u8005\u4f7f\u7528<code>utf8<\/code>\uff0c\u4f46\u8fd9\u4e9b\u5efa\u8bae\u90fd\u662f\u9519\u8bef\u7684\u3002<\/p>\n<p>\u7b80\u5355\u6982\u62ec\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>MySQL\u7684<code>utf8mb4<\/code>\u662f\u771f\u6b63\u7684<code>UTF-8<\/code>\u3002<\/li>\n<li>MySQL\u7684<code>utf8<\/code>\u662f\u4e00\u79cd\u201c\u4e13\u5c5e\u7684\u7f16\u7801\u201d\uff0c\u5b83\u80fd\u591f\u7f16\u7801\u7684Unicode\u5b57\u7b26\u5e76\u4e0d\u591a\u3002<\/li>\n<\/ul>\n<p>\u6211\u8981\u5728\u8fd9\u91cc\u6f84\u6e05\u4e00\u4e0b\uff1a\u6240\u6709\u5728\u4f7f\u7528<code>utf8<\/code>\u7684MySQL\u548cMariaDB\u7528\u6237\u90fd\u5e94\u8be5\u6539\u7528<code>utf8mb4<\/code>\uff0c\u6c38\u8fdc\u90fd\u4e0d\u8981\u518d\u4f7f\u7528<code>utf8<\/code>\u3002<\/p>\n<h1>\u90a3\u4e48\u4ec0\u4e48\u662f\u7f16\u7801\uff1f\u4ec0\u4e48\u662fUTF-8\uff1f<\/h1>\n<p>\u6211\u4eec\u90fd\u77e5\u9053\uff0c\u8ba1\u7b97\u673a\u4f7f\u75280\u548c1\u6765\u5b58\u50a8\u6587\u672c\u3002\u6bd4\u5982\u5b57\u7b26\u201cC\u201d\u88ab\u5b58\u6210\u201c01000011\u201d\uff0c\u90a3\u4e48\u8ba1\u7b97\u673a\u5728\u663e\u793a\u8fd9\u4e2a\u5b57\u7b26\u65f6\u9700\u8981\u7ecf\u8fc7\u4e24\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u8ba1\u7b97\u673a\u8bfb\u53d6\u201c01000011\u201d\uff0c\u5f97\u5230\u6570\u5b5767\uff0c\u56e0\u4e3a67\u88ab\u7f16\u7801\u6210\u201c01000011\u201d\u3002<\/li>\n<li>\u8ba1\u7b97\u673a\u5728Unicode\u5b57\u7b26\u96c6\u4e2d\u67e5\u627e67\uff0c\u627e\u5230\u4e86\u201cC\u201d\u3002<\/li>\n<\/ul>\n<p>\u540c\u6837\u7684\uff1a<\/p>\n<ul>\n<li>\u6211\u7684\u7535\u8111\u5c06\u201cC\u201d\u6620\u5c04\u6210Unicode\u5b57\u7b26\u96c6\u4e2d\u768467\u3002<\/li>\n<li>\u6211\u7684\u7535\u8111\u5c0667\u7f16\u7801\u6210\u201c01000011\u201d\uff0c\u5e76\u53d1\u9001\u7ed9Web\u670d\u52a1\u5668\u3002<\/li>\n<\/ul>\n<p>\u51e0\u4e4e\u6240\u6709\u7684\u7f51\u7edc\u5e94\u7528\u90fd\u4f7f\u7528\u4e86Unicode\u5b57\u7b26\u96c6\uff0c\u56e0\u4e3a\u6ca1\u6709\u7406\u7531\u4f7f\u7528\u5176\u4ed6\u5b57\u7b26\u96c6\u3002<\/p>\n<p>Unicode\u5b57\u7b26\u96c6\u5305\u542b\u4e86\u4e0a\u767e\u4e07\u4e2a\u5b57\u7b26\u3002\u6700\u7b80\u5355\u7684\u7f16\u7801\u662fUTF-32\uff0c\u6bcf\u4e2a\u5b57\u7b26\u4f7f\u752832\u4f4d\u3002\u8fd9\u6837\u505a\u6700\u7b80\u5355\uff0c\u56e0\u4e3a\u4e00\u76f4\u4ee5\u6765\uff0c\u8ba1\u7b97\u673a\u5c0632\u4f4d\u89c6\u4e3a\u6570\u5b57\uff0c\u800c\u8ba1\u7b97\u673a\u6700\u5728\u884c\u7684\u5c31\u662f\u5904\u7406\u6570\u5b57\u3002\u4f46\u95ee\u9898\u662f\uff0c\u8fd9\u6837\u592a\u6d6a\u8d39\u7a7a\u95f4\u4e86\u3002<\/p>\n<p>UTF-8\u53ef\u4ee5\u8282\u7701\u7a7a\u95f4\uff0c\u5728UTF-8\u4e2d\uff0c\u5b57\u7b26\u201cC\u201d\u53ea\u9700\u89818\u4f4d\uff0c\u4e00\u4e9b\u4e0d\u5e38\u7528\u7684\u5b57\u7b26\uff0c\u6bd4\u5982\u201c\u201d\u9700\u898132\u4f4d\u3002\u5176\u4ed6\u7684\u5b57\u7b26\u53ef\u80fd\u4f7f\u752816\u4f4d\u621624\u4f4d\u3002\u4e00\u7bc7\u7c7b\u4f3c\u672c\u6587\u8fd9\u6837\u7684\u6587\u7ae0\uff0c\u5982\u679c\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u5360\u7528\u7684\u7a7a\u95f4\u53ea\u6709UTF-32\u7684\u56db\u5206\u4e4b\u4e00\u5de6\u53f3\u3002<\/p>\n<p>MySQL\u7684\u201cutf8\u201d\u5b57\u7b26\u96c6\u4e0e\u5176\u4ed6\u7a0b\u5e8f\u4e0d\u517c\u5bb9\uff0c\u5b83\u6240\u8c13\u7684\u201c\u201d\uff0c\u53ef\u80fd\u771f\u7684\u662f\u4e00\u5768\u2026\u2026<\/p>\n<h1><strong>MySQL\u7b80\u53f2<\/strong><\/h1>\n<p>\u4e3a\u4ec0\u4e48MySQL\u5f00\u53d1\u8005\u4f1a\u8ba9\u201cutf8\u201d\u5931\u6548\uff1f\u6211\u4eec\u6216\u8bb8\u53ef\u4ee5\u4ece\u63d0\u4ea4\u65e5\u5fd7\u4e2d\u5bfb\u627e\u7b54\u6848\u3002<\/p>\n<p>MySQL\u4ece4.1\u7248\u672c\u5f00\u59cb\u652f\u6301UTF-8\uff0c\u4e5f\u5c31\u662f2003\u5e74\uff0c\u800c\u4eca\u5929\u4f7f\u7528\u7684UTF-8\u6807\u51c6\uff08RFC 3629\uff09\u662f\u968f\u540e\u624d\u51fa\u73b0\u7684\u3002<\/p>\n<p>\u65e7\u7248\u7684UTF-8\u6807\u51c6\uff08RFC 2279\uff09\u6700\u591a\u652f\u6301\u6bcf\u4e2a\u5b57\u7b266\u4e2a\u5b57\u8282\u30022002\u5e743\u670828\u65e5\uff0cMySQL\u5f00\u53d1\u8005\u5728\u7b2c\u4e00\u4e2aMySQL 4.1\u9884\u89c8\u7248\u4e2d\u4f7f\u7528\u4e86RFC 2279\u3002<\/p>\n<p>\u540c\u5e749\u6708\uff0c\u4ed6\u4eec\u5bf9MySQL\u6e90\u4ee3\u7801\u8fdb\u884c\u4e86\u4e00\u6b21\u8c03\u6574\uff1a\u201cUTF8\u73b0\u5728\u6700\u591a\u53ea\u652f\u63013\u4e2a\u5b57\u8282\u7684\u5e8f\u5217\u201d\u3002<\/p>\n<p>\u662f\u8c01\u63d0\u4ea4\u4e86\u8fd9\u4e9b\u4ee3\u7801\uff1f\u4ed6\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u505a\uff1f\u8fd9\u4e2a\u95ee\u9898\u4e0d\u5f97\u800c\u77e5\u3002\u5728\u8fc1\u79fb\u5230Git\u540e\uff08MySQL\u6700\u5f00\u59cb\u4f7f\u7528\u7684\u662fBitKeeper\uff09\uff0cMySQL\u4ee3\u7801\u5e93\u4e2d\u7684\u5f88\u591a\u63d0\u4ea4\u8005\u7684\u540d\u5b57\u90fd\u4e22\u5931\u4e86\u30022003\u5e749\u6708\u7684\u90ae\u4ef6\u5217\u8868\u4e2d\u4e5f\u627e\u4e0d\u5230\u53ef\u4ee5\u89e3\u91ca\u8fd9\u4e00\u53d8\u66f4\u7684\u7ebf\u7d22\u3002<\/p>\n<p>\u4e0d\u8fc7\u6211\u53ef\u4ee5\u8bd5\u7740\u731c\u6d4b\u4e00\u4e0b\u3002<\/p>\n<p>2002\u5e74\uff0cMySQL\u505a\u51fa\u4e86\u4e00\u4e2a\u51b3\u5b9a\uff1a\u5982\u679c\u7528\u6237\u53ef\u4ee5\u4fdd\u8bc1\u6570\u636e\u8868\u7684\u6bcf\u4e00\u884c\u90fd\u4f7f\u7528\u76f8\u540c\u7684\u5b57\u8282\u6570\uff0c\u90a3\u4e48MySQL\u5c31\u53ef\u4ee5\u5728\u6027\u80fd\u65b9\u9762\u6765\u4e00\u4e2a\u5927\u63d0\u5347\u3002\u4e3a\u6b64\uff0c\u7528\u6237\u9700\u8981\u5c06\u6587\u672c\u5217\u5b9a\u4e49\u4e3a\u201cCHAR\u201d\uff0c\u6bcf\u4e2a\u201cCHAR\u201d\u5217\u603b\u662f\u62e5\u6709\u76f8\u540c\u6570\u91cf\u7684\u5b57\u7b26\u3002\u5982\u679c\u63d2\u5165\u7684\u5b57\u7b26\u5c11\u4e8e\u5b9a\u4e49\u7684\u6570\u91cf\uff0cMySQL\u5c31\u4f1a\u5728\u540e\u9762\u586b\u5145\u7a7a\u683c\uff0c\u5982\u679c\u63d2\u5165\u7684\u5b57\u7b26\u8d85\u8fc7\u4e86\u5b9a\u4e49\u7684\u6570\u91cf\uff0c\u540e\u9762\u8d85\u51fa\u90e8\u5206\u4f1a\u88ab\u622a\u65ad\u3002<\/p>\n<p>MySQL\u5f00\u53d1\u8005\u5728\u6700\u5f00\u59cb\u5c1d\u8bd5UTF-8\u65f6\u4f7f\u7528\u4e86\u6bcf\u4e2a\u5b57\u7b266\u4e2a\u5b57\u8282\uff0cCHAR(1)\u4f7f\u75286\u4e2a\u5b57\u8282\uff0cCHAR(2)\u4f7f\u752812\u4e2a\u5b57\u8282\uff0c\u5e76\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<p>\u5e94\u8be5\u8bf4\uff0c\u4ed6\u4eec\u6700\u521d\u7684\u884c\u4e3a\u624d\u662f\u6b63\u786e\u7684\uff0c\u53ef\u60dc\u8fd9\u4e00\u7248\u672c\u4e00\u76f4\u6ca1\u6709\u53d1\u5e03\u3002\u4f46\u662f\u6587\u6863\u4e0a\u5374\u8fd9\u4e48\u5199\u4e86\uff0c\u800c\u4e14\u5e7f\u4e3a\u6d41\u4f20\uff0c\u6240\u6709\u4e86\u89e3UTF-8\u7684\u4eba\u90fd\u8ba4\u540c\u6587\u6863\u91cc\u5199\u7684\u4e1c\u897f\u3002<\/p>\n<p>\u4e0d\u8fc7\u5f88\u663e\u7136\uff0cMySQL\u5f00\u53d1\u8005\u6216\u5382\u5546\u62c5\u5fc3\u4f1a\u6709\u7528\u6237\u505a\u8fd9\u4e24\u4ef6\u4e8b\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528CHAR\u5b9a\u4e49\u5217\uff08\u5728\u73b0\u5728\u770b\u6765\uff0cCHAR\u5df2\u7ecf\u662f\u8001\u53e4\u8463\u4e86\uff0c\u4f46\u5728\u90a3\u65f6\uff0c\u5728MySQL\u4e2d\u4f7f\u7528CHAR\u4f1a\u66f4\u5feb\uff0c\u4e0d\u8fc7\u4ece2005\u5e74\u4ee5\u540e\u5c31\u4e0d\u662f\u8fd9\u6837\u5b50\u4e86\uff09\u3002<\/li>\n<li>\u5c06CHAR\u5217\u7684\u7f16\u7801\u8bbe\u7f6e\u4e3a\u201cutf8\u201d\u3002<\/li>\n<\/ul>\n<p>\u6211\u7684\u731c\u6d4b\u662fMySQL\u5f00\u53d1\u8005\u672c\u6765\u60f3\u5e2e\u52a9\u90a3\u4e9b\u5e0c\u671b\u5728\u7a7a\u95f4\u548c\u901f\u5ea6\u4e0a\u53cc\u8d62\u7684\u7528\u6237\uff0c\u4f46\u4ed6\u4eec\u641e\u7838\u4e86\u201cutf8\u201d\u7f16\u7801\u3002<\/p><div id=\"rml_readmorelogin_placeholder\" style=\"position:relative;\"><div id=\"rml_fade_content\" style=\"position: absolute;\r\ntop:-10em;\r\nwidth:100%;\r\nheight:10em;\r\nbackground: -webkit-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -moz-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -o-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -ms-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\"><\/div><div class=\"wpf-controller aru_rml_from_in_post\" style=\"background-color:#eeeeee;border:5px solid #cce6ff;\" id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><h2 id=\"Header\">\u67e5\u770b\u66f4\u591a<\/h2><div id=\"Message\"><p>\u8054\u7cfb\u7ba1\u7406\u5458\u5fae\u4fe1tutu19192010\uff0c\u6ce8\u518c\u8d26\u53f7<\/p>\n<\/div><div id=\"StatusBarHeader\"><\/div><form id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><input name=\"post_id\" value=\"4968\" type=\"hidden\"\/><input name=\"_init_callback\" value=\"InitLogin\" type=\"hidden\"\/><input name=\"post_id\" value=\"4968\" type=\"hidden\"\/><input name=\"rt_ype\" value=\"1\" type=\"hidden\"\/><input name=\"nonce\" value=\"9e40fd2a87\" type=\"hidden\"\/><input name=\"_wpnonce\" value=\"a09431a0fd\" type=\"hidden\"\/><input name=\"_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_proxy_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_view\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginView\" type=\"hidden\"\/><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\" width=\"400px\"><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Username:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"username\" value=\"\" type=\"text\"\/><\/td><\/tr><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Password:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"password\" value=\"\" type=\"password\"\/><\/td><\/tr><\/tbody><\/table><p class=\"wpf-table-placeholder submit\"><button class=\"wp_plugin_framework_ajax_button\" type=\"button\" style=\"background-color:#4D90FE;;color:#ffffff;;border:1px solid #3079ed;\" name=\"_event\" value=\"ButtonLogin\">Log in<\/button><\/p><\/td><td class=\"wpf-table-placeholder-input\"><\/td><\/tr><\/tbody><\/table><\/form><div id=\"ButtonStartRegister\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=register\">Register<\/a><\/div><div id=\"Link1\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=lostpassword\">Forgotten username or password?<\/a><\/div><div id=\"StatusBarFooter\"><\/div><\/div><\/div><div id=aru_remaining_content><\/div>","protected":false},"excerpt":{"rendered":"<p>\u8bd1\u6587:http:\/\/suo.im\/4zBuvs \u6765\u81ea:http:\/\/ju.outofmemory.cn \u6700\u8fd1\u6211 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":4985,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[371,378],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4968"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4968"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4968\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/media\/4985"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}