{"id":4852,"date":"2021-03-02T02:11:00","date_gmt":"2021-03-01T18:11:00","guid":{"rendered":"http:\/\/www.menglanglang.cn\/?p=4852"},"modified":"2021-04-02T16:48:19","modified_gmt":"2021-04-02T08:48:19","slug":"%e8%ae%be%e7%bd%ae-mysql-%e6%95%b0%e6%8d%ae%e5%ba%93%e7%bc%96%e7%a0%81%e4%b8%ba-utf8mb4","status":"publish","type":"post","link":"http:\/\/www.menglanglang.cn\/?p=4852","title":{"rendered":"\u8bbe\u7f6e MYSQL \u6570\u636e\u5e93\u7f16\u7801\u4e3a utf8mb4"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>utf-8\u7f16\u7801\u53ef\u80fd2\u4e2a\u5b57\u8282\u30013\u4e2a\u5b57\u8282\u30014\u4e2a\u5b57\u8282\u7684\u5b57\u7b26\uff0c\u4f46\u662fMySQL\u7684utf8\u7f16\u7801\u53ea\u652f\u63013\u5b57\u8282\u7684\u6570\u636e\uff0c\u800c\u79fb\u52a8\u7aef\u7684\u8868\u60c5\u6570\u636e\u662f4\u4e2a\u5b57\u8282\u7684\u5b57\u7b26\u3002\u5982\u679c\u76f4\u63a5\u5f80\u91c7\u7528utf-8\u7f16\u7801\u7684\u6570\u636e\u5e93\u4e2d\u63d2\u5165\u8868\u60c5\u6570\u636e\uff0cjava\u7a0b\u5e8f\u4e2d\u5c06\u62a5SQL\u5f02\u5e38\uff1a<\/p><p>java.sql.SQLException:&nbsp;Incorrect string value: \u2018\\xF0\\x9F\\x92\\x94\u2019 for column \u2018name\u2019 at row 1&nbsp;<\/p><p>utf8mb4\u7f16\u7801\u662futf8\u7f16\u7801\u7684\u8d85\u96c6\uff0c\u517c\u5bb9utf8\uff0c\u5e76\u4e14\u80fd\u5b58\u50a84\u5b57\u8282\u7684\u8868\u60c5\u5b57\u7b26\u3002&nbsp;<br \/>\u91c7\u7528utf8mb4\u7f16\u7801\u7684\u597d\u5904\u662f\uff1a\u5b58\u50a8\u4e0e\u83b7\u53d6\u6570\u636e\u7684\u65f6\u5019\uff0c\u4e0d\u7528\u518d\u8003\u8651\u8868\u60c5\u5b57\u7b26\u7684\u7f16\u7801\u4e0e\u89e3\u7801\u95ee\u9898\u3002<\/p><\/blockquote>\n\n\n\n<p><strong>1\u3001\u67e5\u8be2\u5f53\u524d\u6570\u636e\u5e93\u5b57\u7b26\u96c6<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';<\/p><\/blockquote>\n\n\n\n<p><strong>2\u3001\u8bbe\u7f6emysql\u914d\u7f6e\u6587\u4ef6\/etc\/my.cnf<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;client]&nbsp;\ndefault-character-set = utf8mb4&nbsp;\n&nbsp;\n&#91;mysql]&nbsp;\ndefault-character-set = utf8mb4&nbsp;\n&nbsp;\n&#91;mysqld]&nbsp;\ncharacter-set-client-handshake = FALSE&nbsp;\ncharacter-set-server = utf8mb4&nbsp;\ncollation-server = utf8mb4_general_ci&nbsp;\ninit_connect='SET NAMES utf8mb4'<\/code><\/pre>\n\n\n\n<p><strong>3\u3001\u91cd\u542fmysql\u670d\u52a1<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>systemctl restart mysqld.service<\/p><\/blockquote>\n\n\n\n<p><img decoding=\"async\" alt=\"\" src=\"\/\/images.weserv.nl\/?url=https:\/\/img-blog.csdnimg.cn\/20190517130843180.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Zpc2hpbmhvdXNl,size_16,color_FFFFFF,t_70\"\/><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>MySQL \u4e2d\u5b57\u7b26\u96c6\u76f8\u5173\u53d8\u91cf<\/strong><br \/>character_set_client\uff1a\u5ba2\u6237\u7aef\u8bf7\u6c42\u6570\u636e\u7684\u5b57\u7b26\u96c6<br \/>character_set_connection\uff1a\u4ece\u5ba2\u6237\u7aef\u63a5\u6536\u5230\u6570\u636e\uff0c\u7136\u540e\u4f20\u8f93\u7684\u5b57\u7b26\u96c6<br \/>character_set_database\uff1a\u9ed8\u8ba4\u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\uff0c\u65e0\u8bba\u9ed8\u8ba4\u6570\u636e\u5e93\u5982\u4f55\u6539\u53d8\uff0c\u90fd\u662f\u8fd9\u4e2a\u5b57\u7b26\u96c6\uff1b\u5982\u679c\u6ca1\u6709\u9ed8\u8ba4\u6570\u636e\u5e93\uff0c\u90a3\u5c31\u4f7f\u7528 character_set_server\u6307\u5b9a\u7684\u5b57\u7b26\u96c6\uff0c\u8fd9\u4e2a\u53d8\u91cf\u5efa\u8bae\u7531\u7cfb\u7edf\u81ea\u5df1\u7ba1\u7406\uff0c\u4e0d\u8981\u4eba\u4e3a\u5b9a\u4e49\u3002<br \/>character_set_filesystem\uff1a\u628a\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u7684\u6587\u4ef6\u540d\u8f6c\u5316\u6210\u6b64\u5b57\u7b26\u96c6\uff0c\u5373\u628a character_set_client\u8f6c\u6362character_set_filesystem\uff0c \u9ed8\u8ba4binary\u662f\u4e0d\u505a\u4efb\u4f55\u8f6c\u6362\u7684<br \/>character_set_results\uff1a\u7ed3\u679c\u96c6\u7684\u5b57\u7b26\u96c6<br \/>character_set_server\uff1a\u6570\u636e\u5e93\u670d\u52a1\u5668\u7684\u9ed8\u8ba4\u5b57\u7b26\u96c6<br \/>character_set_system\uff1a\u5b58\u50a8\u7cfb\u7edf\u5143\u6570\u636e\u7684\u5b57\u7b26\u96c6\uff0c\u603b\u662f utf8\uff0c\u4e0d\u9700\u8981\u8bbe\u7f6e<\/p><\/blockquote>\n\n\n\n<p><strong>4\u3001\u6570\u636e\u5e93\u94fe\u63a5\u53c2\u6570<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>characterEncoding=utf8mb4&amp;autoReconnect=true<\/p><\/blockquote>\n\n\n\n<p><strong>5\u3001\u5982\u679c\u5df2\u7ecf\u6709\u6570\u636e\u5e93\u548c\u8868\uff0c\u53ef\u4ee5\u4fee\u6539\u5b57\u7b26\u96c6\uff1a<\/strong><\/p>\n\n\n\n<p>\u66f4\u6539\u6570\u636e\u5e93\u7f16\u7801\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>ALTER DATABASE DATABASE_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;<\/p><\/blockquote>\n\n\n\n<p>\u66f4\u6539\u8868\u7f16\u7801\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;&nbsp;<\/p><\/blockquote>\n\n\n\n<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/blog.csdn.net\/tzhuwb\/article\/details\/114283883\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/blog.csdn.net\/tzhuwb\/article\/details\/114283883<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>utf-8\u7f16\u7801\u53ef\u80fd2\u4e2a\u5b57\u8282\u30013\u4e2a\u5b57\u8282\u30014\u4e2a\u5b57\u8282\u7684\u5b57\u7b26\uff0c\u4f46\u662fMySQL\u7684utf8\u7f16\u7801\u53ea\u652f\u63013\u5b57\u8282\u7684\u6570\u636e\uff0c\u800c\u79fb\u52a8\u7aef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[800],"tags":[186,297,840,799,402],"class_list":["post-4852","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-mysql","tag-utf8","tag-utf8mb4","tag-799","tag-402"],"_links":{"self":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/4852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4852"}],"version-history":[{"count":2,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/4852\/revisions"}],"predecessor-version":[{"id":4854,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/4852\/revisions\/4854"}],"wp:attachment":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4852"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}