{"id":448,"date":"2015-02-02T11:18:16","date_gmt":"2015-02-02T03:18:16","guid":{"rendered":"http:\/\/elon.852128.com\/blog\/?p=448"},"modified":"2024-12-18T08:21:43","modified_gmt":"2024-12-18T00:21:43","slug":"mysql%e5%ad%97%e7%ac%a6%e9%9b%86%e4%bf%ae%e6%94%b9%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/elon.852128.com\/blog\/2015\/02\/02\/mysql%e5%ad%97%e7%ac%a6%e9%9b%86%e4%bf%ae%e6%94%b9%e5%91%bd%e4%bb%a4\/","title":{"rendered":"MySQL\u5b57\u7b26\u96c6\u4fee\u6539\u547d\u4ee4"},"content":{"rendered":"<p>\u67e5\u770b\u7f16\u7801\uff1a<br \/>\nshow variables like &#8216;collation_%&#8217;;<br \/>\nshow variables like &#8216;character_set_%&#8217;; <\/p>\n<p>\u4fee\u6539\uff1a<br \/>\nMySQL\uff1a\u4fee\u6539\u9ed8\u8ba4\u5b57\u7b26\u96c6\uff0c\u8f6c\u6362\u5b57\u7b26\u96c6\uff08MySQL 5.X\uff09<br \/>\n1. \u4fee\u6539 MySQL \u6570\u636e\u5e93\u9ed8\u8ba4\u5b57\u7b26\u96c6\uff08mysql database default character set\uff09<br \/>\nalter database testdb default character set = gb2312;<br \/>\n2. \u4fee\u6539 MySQL \u6570\u636e\u8868\u9ed8\u8ba4\u5b57\u7b26\u96c6\uff08mysql table default character set\uff09<br \/>\nalter table ip_to_country default character set = gb2312;<br \/>\n\u6ce8\u610f\uff1a\u4fee\u6539 MySQL \u7684\u9ed8\u8ba4\u5b57\u7b26\u96c6\uff0c\u4e0d\u7ba1\u662f\u5728\u6570\u636e\u5e93\u7ea7\u522b\uff0c\u8fd8\u662f\u6570\u636e\u8868\u7ea7\u522b\uff0c\u5bf9\u5df2\u7ecf\u5b58\u50a8\u7684\u5b57\u7b26\u6570\u636e\u65e0\u4efb\u4f55\u6539\u53d8\u3002\u53ea\u662f\u65b0\u589e\u7684\u8868\u6216\u5217\uff0c\u5f00\u59cb\u4f7f\u7528\u65b0\u7684\u5b57\u7b26\u96c6\u3002 <\/p>\n<p>3. \u8f6c\u6362 MySQL \u6570\u636e\u8868\u5b57\u7b26\u96c6\uff08mysql table character set\uff09<br \/>\nMySQL \u4e2d\u6709\u4e00\u4e2a\u6570\u636e\u8868 ip_to_country\uff0c\u4ee5\u524d\u8be5\u8868\u5b57\u7b26\u5217\u6570\u636e\u90fd\u662f\u4ee5 utf8 \u7f16\u7801\u683c\u5f0f\u5b58\u653e\u7684\uff0c\u73b0\u5728\u60f3\u8f6c\u6362\u4e3a gb2312 \u7f16\u7801\u683c\u5f0f\u5b58\u653e\u3002\u9700\u8981\u4f7f\u7528 MySQL \u8f6c\u6362\u5b57\u7b26\u96c6\u547d\u4ee4\uff1aconvert to character set<br \/>\nalter table ip_to_country convert to character set gb2312;<br \/>\n\u63d0\u793a\uff1aconvert to \u4e5f\u4f1a\u628a\u6570\u636e\u8868 ip_to_country \u7684\u9ed8\u8ba4\u5b57\u7b26\u96c6\u4ece utf8 \u66f4\u6539\u4e3a gb2312\u3002 <\/p>\n<p>4. \u8f6c\u6362 MySQL \u6570\u636e\u8868\u4e2d\u67d0\u4e2a\u5217\u7684\u5b57\u7b26\u96c6\uff08mysql column character set\uff09<br \/>\nalter table ip_to_country modify country_name varchar(50) character set gb2312;<br \/>\n\u8fd9\u4e2a\u8bed\u53e5\u628a\u5355\u72ec\u4e00\u4e2a\u5217 country_name \u7684\u5b57\u7b26\u96c6\u8f6c\u6362\u4e3a gb2312\u3002\u8fd9\u5728\u7528\u6cd5\u5728\u5b9e\u9645\u4e2d\u6bd4\u8f83\u5c11\u89c1\uff0c\u56e0\u4e3a\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u90fd\u662f\u6539\u53d8\u6574\u4e2a\u8868\u7684\u5b57\u7b26\u96c6\u3002 <\/p>\n<p>MySQL\u5b57\u7b26\u96c6\u4fee\u6539\u547d\u4ee4 <\/p>\n<p>1.\u4fee\u6539\u670d\u52a1\u5668\u7ea7<br \/>\na. \u4e34\u65f6\u66f4\u6539\uff1a mysql>SET GLOBAL character_set_server=utf8;<br \/>\nb. \u6c38\u4e45\u66f4\u6539\uff1ashell>vi \/etc\/my.cnf[mysqld]default-character-set=utf8 <\/p>\n<p>2.\u4fee\u6539\u6570\u636e\u5e93\u7ea7<br \/>\na. \u4e34\u65f6\u66f4\u6539\uff1a mysql>SET GLOBAL character_set_database=utf8;<br \/>\nb. \u6c38\u4e45\u66f4\u6539\uff1a\u6539\u4e86\u670d\u52a1\u5668\u7ea7\u5c31\u53ef\u4ee5\u4e86 <\/p>\n<p>3.\u4fee\u6539\u8868\u7ea7<br \/>\nmysql>Alter TABLE table_name DEFAULT CHARSET utf8; \u66f4\u6539\u4e86\u540e\u6c38\u4e45\u751f\u6548 <\/p>\n<p>4.\u4fee\u6539\u5217\u7ea7\u4fee\u6539\u793a\u4f8b\uff1a<br \/>\nmysql>Alter TABLE `products` CHANGE `products_model` `products_model` VARCHAR( 20 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL; \u66f4 \u6539\u4e86\u540e\u6c38\u4e45\u751f\u6548 <\/p>\n<p>5.\u66f4\u6539\u8fde\u63a5\u5b57\u7b26\u96c6<br \/>\na. \u4e34\u65f6\u66f4\u6539\uff1amysql> SET NAMES utf8;<br \/>\nb.\u6c38\u4e45\u66f4\u6539\uff1a shell>vi \/etc\/my.cnf\u5728[client]\u4e2d\u589e\u52a0\uff1adefault-character-set=utf8<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u770b\u7f16\u7801\uff1a show variables like &#8216;&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-448","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/posts\/448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/comments?post=448"}],"version-history":[{"count":1,"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/posts\/448\/revisions"}],"predecessor-version":[{"id":449,"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/posts\/448\/revisions\/449"}],"wp:attachment":[{"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/media?parent=448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/categories?post=448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/tags?post=448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}