{"id":808,"date":"2025-01-11T00:26:37","date_gmt":"2025-01-10T16:26:37","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=808"},"modified":"2025-01-11T00:26:37","modified_gmt":"2025-01-10T16:26:37","slug":"%e5%a6%82%e4%bd%95%e4%bf%ae%e5%a4%8d-mysql-%e4%b8%ad%e7%9a%84%e5%ad%97%e7%ac%a6%e6%98%be%e7%a4%ba%e9%97%ae%e9%a2%98%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/11\/%e5%a6%82%e4%bd%95%e4%bf%ae%e5%a4%8d-mysql-%e4%b8%ad%e7%9a%84%e5%ad%97%e7%ac%a6%e6%98%be%e7%a4%ba%e9%97%ae%e9%a2%98%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u4fee\u590d MySQL \u4e2d\u7684\u5b57\u7b26\u663e\u793a\u95ee\u9898\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 MySQL \u4e2d\uff0c\u5b57\u7b26\u663e\u793a\u95ee\u9898\u901a\u5e38\u662f\u7531\u4e8e\u6570\u636e\u5e93\u3001\u8868\u6216\u5217\u7684\u5b57\u7b26\u96c6\uff08charset\uff09\u548c\u6392\u5e8f\u89c4\u5219\uff08collation\uff09\u8bbe\u7f6e\u4e0d\u4e00\u81f4\u6216\u4e0d\u9002\u5f53\u9020\u6210\u7684\u3002\u4f8b\u5982\uff0c\u63d2\u5165\u975e ASCII \u5b57\u7b26\u65f6\uff0c\u5982\u679c\u5b57\u7b26\u96c6\u6ca1\u6709\u8bbe\u7f6e\u4e3a\u652f\u6301\u8fd9\u4e9b\u5b57\u7b26\uff08\u5982 UTF-8\uff09\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e71\u7801\u6216\u5b57\u7b26\u663e\u793a\u95ee\u9898\u3002<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u4fee\u590d MySQL \u4e2d\u7684\u5b57\u7b26\u663e\u793a\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u66f4\u6539\u6570\u636e\u5e93\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u68c0\u67e5\u5f53\u524d\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u4f60\u9700\u8981\u68c0\u67e5\u5f53\u524d\u6570\u636e\u5e93\u3001\u8868\u6216\u5217\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b SQL \u67e5\u8be2\u6765\u67e5\u770b\u5b83\u4eec\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME\nFROM information_schema.SCHEMATA\nWHERE SCHEMA_NAME = 'your_database_name';<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u770b\u5f53\u524d\u8868\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW TABLE STATUS WHERE Name = 'your_table_name';<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u770b\u67d0\u4e2a\u5217\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW FULL COLUMNS FROM your_table_name;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u66f4\u6539\u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\u8bbe\u7f6e\u4e0d\u6b63\u786e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b SQL \u8bed\u53e5\u4fee\u6539\u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\u3002\u8fd9\u91cc\u4ee5 UTF-8 \u4e3a\u4f8b\uff0c\u5b83\u652f\u6301\u591a\u79cd\u8bed\u8a00\u5b57\u7b26\uff0c\u5e76\u5e7f\u6cdb\u7528\u4e8e\u73b0\u4ee3\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u66f4\u6539\u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER DATABASE your_database_name \n    CHARACTER SET = utf8mb4 \n    COLLATE = utf8mb4_unicode_ci;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>utf8mb4<\/code> \u662f UTF-8 \u7684\u6269\u5c55\u5b57\u7b26\u96c6\uff0c\u652f\u6301\u66f4\u591a\u5b57\u7b26\uff08\u5982 emoji \u548c\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\uff09\u3002<\/li>\n\n\n\n<li><code>utf8mb4_unicode_ci<\/code> \u662f\u4e00\u79cd\u5e38\u7528\u7684\u6392\u5e8f\u89c4\u5219\uff0c\u652f\u6301 Unicode \u5b57\u7b26\u7684\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u6392\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u66f4\u6539\u8868\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8868\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\u8bbe\u7f6e\u4e0d\u6b63\u786e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b SQL \u8bed\u53e5\u6765\u66f4\u6539\u8868\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE your_table_name \n    CONVERT TO CHARACTER SET utf8mb4 \n    COLLATE utf8mb4_unicode_ci;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>CONVERT TO CHARACTER SET<\/code> \u4f1a\u5c06\u8868\u4e2d\u7684\u6240\u6709\u5217\u8f6c\u6362\u4e3a\u65b0\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u66f4\u6539\u5355\u4e2a\u5217\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u53ea\u662f\u67d0\u4e9b\u5217\u51fa\u73b0\u5b57\u7b26\u663e\u793a\u95ee\u9898\uff0c\u53ef\u4ee5\u5355\u72ec\u66f4\u6539\u5217\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE your_table_name \n    MODIFY COLUMN your_column_name \n    VARCHAR(255) \n    CHARACTER SET utf8mb4 \n    COLLATE utf8mb4_unicode_ci;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u786e\u4fdd\u8fde\u63a5\u7684\u5b57\u7b26\u96c6\u8bbe\u7f6e\u6b63\u786e<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u66f4\u6539\u6570\u636e\u5e93\u3001\u8868\u548c\u5217\u7684\u5b57\u7b26\u96c6\u8bbe\u7f6e\u5916\uff0c\u8fd8\u8981\u786e\u4fdd\u6570\u636e\u5e93\u8fde\u63a5\u65f6\u4f7f\u7528\u7684\u5b57\u7b26\u96c6\u6b63\u786e\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5\u5f53\u524d\u8fde\u63a5\u7684\u5b57\u7b26\u96c6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW VARIABLES LIKE 'character_set%';<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>character_set_client<\/code>\uff1a\u5ba2\u6237\u7aef\u4f7f\u7528\u7684\u5b57\u7b26\u96c6\u3002<\/li>\n\n\n\n<li><code>character_set_connection<\/code>\uff1a\u8fde\u63a5\u4f7f\u7528\u7684\u5b57\u7b26\u96c6\u3002<\/li>\n\n\n\n<li><code>character_set_results<\/code>\uff1a\u67e5\u8be2\u7ed3\u679c\u4f7f\u7528\u7684\u5b57\u7b26\u96c6\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8fd9\u4e9b\u5b57\u7b26\u96c6\u8bbe\u7f6e\u4e0d\u4e00\u81f4\u6216\u4e0d\u652f\u6301\u6240\u9700\u7684\u5b57\u7b26\u96c6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8bbe\u7f6e\u8fde\u63a5\u7684\u5b57\u7b26\u96c6\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5728 MySQL \u8fde\u63a5\u65f6\u8bbe\u7f6e\u5b57\u7b26\u96c6\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SET NAMES 'utf8mb4';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u786e\u4fdd\u5ba2\u6237\u7aef\u3001\u8fde\u63a5\u548c\u67e5\u8be2\u7ed3\u679c\u4f7f\u7528 <code>utf8mb4<\/code> \u5b57\u7b26\u96c6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\uff08\u53ef\u9009\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u786e\u4fdd MySQL \u5728\u6bcf\u6b21\u542f\u52a8\u65f6\u90fd\u4f7f\u7528\u5408\u9002\u7684\u5b57\u7b26\u96c6\uff0c\u53ef\u4ee5\u4fee\u6539 MySQL \u7684\u914d\u7f6e\u6587\u4ef6\uff08\u901a\u5e38\u662f <code>my.cnf<\/code> \u6216 <code>my.ini<\/code>\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>[client]<\/code>\u3001<code>[mysqld]<\/code> \u548c <code>[mysql]<\/code> \u90e8\u5206\u6dfb\u52a0\u4ee5\u4e0b\u8bbe\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;client]\n  default-character-set = utf8mb4<\/code><\/pre>\n\n\n<p>[mysqld]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci<\/p>\n\n\n<p>[mysql]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">default-character-set = utf8mb4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fee\u6539\u5b8c\u914d\u7f6e\u6587\u4ef6\u540e\uff0c\u91cd\u542f MySQL \u670d\u52a1\u4f7f\u8bbe\u7f6e\u751f\u6548\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u63d2\u5165\u6570\u636e\u65f6\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u7b26\u96c6<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u63d2\u5165\u6570\u636e\u65f6\uff0c\u786e\u4fdd\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u4e0e\u6570\u636e\u5e93\u76f8\u540c\u7684\u5b57\u7b26\u96c6\u8fdb\u884c\u8fde\u63a5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4f7f\u7528 PHP \u548c MySQL\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8bbe\u7f6e\u8fde\u63a5\u5b57\u7b26\u96c6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysqli_set_charset($connection, 'utf8mb4');<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u4e0a\u8ff0\u63d0\u5230\u65b9\u6cd5\u5916\uff0c\u518d\u8865\u5145\u4e00\u4e9b\u7b56\u7565\u548c\u65b9\u6cd5\uff0c\u5b83\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u6570\u636e\u5e93\u7684\u5b57\u7b26\u5904\u7406\u80fd\u529b\uff0c\u786e\u4fdd\u4e0d\u540c\u8bed\u8a00\u5b57\u7b26\u548c\u7b26\u53f7\u80fd\u591f\u88ab\u6b63\u786e\u5b58\u50a8\u548c\u663e\u793a\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>\u4f7f\u7528 <code>utf8mb4<\/code> \u66ff\u4ee3 <code>utf8<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MySQL \u7684 <code>utf8<\/code> \u5b57\u7b26\u96c6\u5e76\u4e0d\u662f\u5b8c\u6574\u7684 Unicode \u652f\u6301\uff0c\u5b83\u53ea\u652f\u6301\u6700\u591a\u4e09\u4e2a\u5b57\u8282\u7684\u5b57\u7b26\uff08\u5982\u57fa\u672c\u7684\u591a\u8bed\u8a00\u5b57\u7b26\u96c6\uff09\uff0c\u800c\u4e0d\u652f\u6301\u50cf\u8868\u60c5\u7b26\u53f7\uff08emoji\uff09\u548c\u67d0\u4e9b\u7f55\u89c1\u5b57\u7b26\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>utf8mb4<\/code><\/strong> \u662f MySQL \u4e2d\u652f\u6301\u5b8c\u6574 Unicode \u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u96c6\uff0c\u5b83\u4f7f\u7528\u56db\u4e2a\u5b57\u8282\u6765\u8868\u793a\u5b57\u7b26\uff0c\u80fd\u591f\u5b8c\u6574\u652f\u6301\u6240\u6709 Unicode \u5b57\u7b26\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong> \u786e\u4fdd\u6570\u636e\u5e93\u3001\u8868\u3001\u5217\u3001\u8fde\u63a5\u7b49\u90fd\u4f7f\u7528 <code>utf8mb4<\/code> \u5b57\u7b26\u96c6\uff0c\u800c\u4e0d\u662f <code>utf8<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u521b\u5efa\u6570\u636e\u5e93\u65f6\u6307\u5b9a\u5b57\u7b26\u96c6\nCREATE DATABASE my_database CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n\n-- \u521b\u5efa\u8868\u65f6\u6307\u5b9a\u5b57\u7b26\u96c6\nCREATE TABLE my_table (\n    id INT PRIMARY KEY,\n    name VARCHAR(255)\n) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n\n-- \u4fee\u6539\u5df2\u6709\u8868\u4e3a utf8mb4\nALTER TABLE my_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">9. <strong>\u786e\u4fdd\u8868\u548c\u5217\u4f7f\u7528\u6b63\u786e\u7684\u6570\u636e\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u540c\u7684\u5b57\u7b26\u96c6\u53ef\u80fd\u9700\u8981\u4e0d\u540c\u7684\u5217\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>TEXT<\/code> \u548c <code>VARCHAR<\/code> \u7c7b\u578b\u901a\u5e38\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u5b57\u7b26\u6570\u636e\uff0c\u4f46\u5b83\u4eec\u7684\u6700\u5927\u957f\u5ea6\u548c\u5b58\u50a8\u65b9\u5f0f\u53ef\u80fd\u4e0d\u540c\u3002\u5982\u679c\u4f60\u5e0c\u671b\u80fd\u591f\u652f\u6301\u957f\u6587\u672c\u6570\u636e\u548c\u5404\u79cd\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>TEXT<\/code> \u7c7b\u578b\uff0c\u5e76\u786e\u4fdd\u5b83\u7684\u5b57\u7b26\u96c6\u4e3a <code>utf8mb4<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE my_table MODIFY COLUMN name TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">10. <strong>\u6279\u91cf\u6570\u636e\u8fc1\u79fb\u65f6\u7684\u5b57\u7b26\u96c6\u8f6c\u6362<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u9700\u8981\u4ece\u4e00\u4e2a\u65e7\u6570\u636e\u5e93\u8fc1\u79fb\u5230\u65b0\u7684\u6570\u636e\u5e93\uff0c\u6216\u8005\u4ece\u4e00\u4e2a\u5b57\u7b26\u96c6\u4e0d\u540c\u7684\u6570\u636e\u5e93\u8fc1\u79fb\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 MySQL \u7684 <code>CONVERT()<\/code> \u51fd\u6570\u8fdb\u884c\u5b57\u7b26\u96c6\u8f6c\u6362\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5047\u8bbe\u539f\u6570\u636e\u8868\u4f7f\u7528\u7684\u662f latin1\uff0c\u76ee\u6807\u8868\u4f7f\u7528 utf8mb4\nSELECT CONVERT(name USING utf8mb4) FROM old_table;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u5728\u6570\u636e\u8fc1\u79fb\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5c06\u539f\u59cb\u6570\u636e\u901a\u8fc7 <code>CONVERT()<\/code> \u51fd\u6570\u8f6c\u6362\u4e3a\u76ee\u6807\u5b57\u7b26\u96c6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">11. <strong>\u4fee\u590d\u5df2\u6709\u4e71\u7801\u6570\u636e<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u5728\u6570\u636e\u63d2\u5165\u8fc7\u7a0b\u4e2d\u5df2\u7ecf\u51fa\u73b0\u4e71\u7801\uff0c\u4e14\u6ca1\u6709\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u6216\u6392\u5e8f\u89c4\u5219\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8\u4fee\u590d\u5df2\u7ecf\u5b58\u50a8\u7684\u4e71\u7801\u6570\u636e\u3002\u8fd9\u901a\u5e38\u662f\u7531\u4e8e\u5b57\u7b26\u96c6\u4e0d\u5339\u914d\u6216\u63d2\u5165\u65f6\u5b57\u7b26\u7f16\u7801\u4e0d\u4e00\u81f4\u5bfc\u81f4\u7684\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u4fee\u590d\u4e71\u7801\u7684\u5e38\u7528\u65b9\u6cd5\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u67e5\u770b\u4e71\u7801\u6570\u636e\uff1a<\/strong><br>\u4f60\u53ef\u4ee5\u67e5\u8be2\u4e71\u7801\u7684\u6570\u636e\uff0c\u786e\u8ba4\u54ea\u4e9b\u5b57\u6bb5\u9700\u8981\u4fee\u590d\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   SELECT id, name FROM my_table WHERE name LIKE '%\u4e71\u7801%';<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>CONVERT()<\/code> \u51fd\u6570\u4fee\u590d\u4e71\u7801\uff1a<\/strong><br>\u4f7f\u7528 <code>CONVERT()<\/code> \u51fd\u6570\u5c06\u4e71\u7801\u7684\u6570\u636e\u4ece\u9519\u8bef\u7684\u5b57\u7b26\u96c6\u8f6c\u6362\u4e3a\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   UPDATE my_table \n   SET name = CONVERT(name USING utf8mb4) \n   WHERE name LIKE '%\u4e71\u7801%';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u901a\u8fc7\u4e0d\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u63d2\u5165\u6570\u636e\u540e\u9700\u8981\u4fee\u590d\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">12. <strong>\u8c03\u6574 MySQL \u670d\u52a1\u5668\u7684\u5b57\u7b26\u96c6\u914d\u7f6e<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574 MySQL \u670d\u52a1\u5668\u7684\u914d\u7f6e\u6765\u786e\u4fdd\u5b83\u5728\u5904\u7406\u5b57\u7b26\u65f6\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\u3002\u914d\u7f6e\u6587\u4ef6\uff08\u901a\u5e38\u662f <code>my.cnf<\/code> \u6216 <code>my.ini<\/code>\uff09\u4e2d\u7684\u4e00\u4e9b\u5173\u952e\u53c2\u6570\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;mysqld]\ncharacter-set-server = utf8mb4\ncollation-server = utf8mb4_unicode_ci\ninit_connect = 'SET NAMES utf8mb4'<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>character-set-server<\/code>\uff1a\u6307\u5b9a\u670d\u52a1\u5668\u7684\u9ed8\u8ba4\u5b57\u7b26\u96c6\u3002<\/li>\n\n\n\n<li><code>collation-server<\/code>\uff1a\u6307\u5b9a\u670d\u52a1\u5668\u7684\u9ed8\u8ba4\u6392\u5e8f\u89c4\u5219\u3002<\/li>\n\n\n\n<li><code>init_connect<\/code>\uff1a\u6bcf\u6b21\u8fde\u63a5\u65f6\uff0c\u786e\u4fdd\u8fde\u63a5\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\u540e\uff0c\u9700\u8981\u91cd\u542f MySQL \u670d\u52a1\u5668\u624d\u80fd\u751f\u6548\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">13. <strong>\u786e\u4fdd\u5ba2\u6237\u7aef\u4e0e MySQL \u670d\u52a1\u5668\u7684\u5b57\u7b26\u96c6\u4e00\u81f4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5ba2\u6237\u7aef\u548c MySQL \u670d\u52a1\u5668\u7684\u5b57\u7b26\u96c6\u8bbe\u7f6e\u5fc5\u987b\u4e00\u81f4\uff0c\u5426\u5219\u5b57\u7b26\u6570\u636e\u4f20\u8f93\u65f6\u53ef\u80fd\u4f1a\u4e22\u5931\u6216\u51fa\u73b0\u4e71\u7801\u3002\u5982\u679c\u5ba2\u6237\u7aef\u4f7f\u7528\u7684\u662f\u4e0d\u540c\u7684\u5b57\u7b26\u96c6\uff0c\u6570\u636e\u4f20\u8f93\u65f6\u53ef\u80fd\u4f1a\u53d1\u751f\u9519\u8bef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u786e\u4fdd\u5728\u8fde\u63a5\u5230 MySQL \u670d\u52a1\u5668\u65f6\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u3002\u4e0d\u540c\u7684\u6570\u636e\u5e93\u5ba2\u6237\u7aef\uff08\u4f8b\u5982 PHP\u3001Python\u3001Java\uff09\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u914d\u7f6e\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5728 PHP \u4e2d\u8bbe\u7f6e\u5b57\u7b26\u96c6\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>mysqli_set_charset($connection, 'utf8mb4');<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5728 Java \u4e2d\u8bbe\u7f6e\u5b57\u7b26\u96c6\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>String url = \"jdbc:mysql:\/\/localhost:3306\/my_database?useUnicode=true&amp;characterEncoding=utf8mb4\";\nConnection conn = DriverManager.getConnection(url, username, password);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5728 Python (MySQL Connector) \u4e2d\u8bbe\u7f6e\u5b57\u7b26\u96c6\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import mysql.connector\n\nconn = mysql.connector.connect(\n    host='localhost',\n    database='my_database',\n    user='root',\n    password='password',\n    charset='utf8mb4'\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">14. <strong>\u4f7f\u7528 Unicode \u7f16\u7801\u5b58\u50a8\u6587\u4ef6\u8def\u5f84<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u7684\u6570\u636e\u5e93\u5b58\u50a8\u4e86\u6587\u4ef6\u8def\u5f84\u6216\u5305\u542b\u7279\u6b8a\u7b26\u53f7\u7684\u5b57\u7b26\u4e32\uff0c\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u7684\u5b57\u7b26\u96c6\u4e5f\u80fd\u6b63\u786e\u5904\u7406\u975e ASCII \u5b57\u7b26\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5b58\u50a8\u6587\u4ef6\u8def\u5f84\u548cURL\u65f6\uff0c\u4f7f\u7528 UTF-8 \u7f16\u7801\u662f\u4e00\u4e2a\u597d\u7684\u5b9e\u8df5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">15. <strong>\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u4e2d\u7684\u5b57\u7b26\u96c6\u8bbe\u7f6e<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u7684\u5e94\u7528\u4f7f\u7528\u4e86\u6570\u636e\u5e93\u8fde\u63a5\u6c60\uff08\u4f8b\u5982\uff0cTomcat\u3001Spring Boot \u7b49\uff09\uff0c\u786e\u4fdd\u6c60\u4e2d\u6240\u6709\u7684\u8fde\u63a5\u90fd\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u3002\u901a\u5e38\u5728\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7684\u914d\u7f6e\u4e2d\uff0c\u53ef\u4ee5\u6307\u5b9a\u5b57\u7b26\u96c6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u5728 Spring Boot \u914d\u7f6e\u6587\u4ef6\uff08<code>application.properties<\/code>\uff09\u4e2d\u6307\u5b9a\u5b57\u7b26\u96c6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>spring.datasource.url=jdbc:mysql:\/\/localhost:3306\/my_database?useUnicode=true&amp;characterEncoding=utf8mb4<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">16. <strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5b57\u7b26\u96c6\u8f6c\u6362<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u5904\u7406\u591a\u79cd\u5b57\u7b26\u96c6\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u8fdb\u884c\u591a\u6b21\u5b57\u7b26\u96c6\u8f6c\u6362\uff0c\u56e0\u4e3a\u6bcf\u6b21\u8f6c\u6362\u90fd\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\uff0c\u751a\u81f3\u6570\u636e\u4e22\u5931\u3002\u786e\u4fdd\u6570\u636e\u4ece\u5934\u5230\u5c3e\u59cb\u7ec8\u4f7f\u7528\u76f8\u540c\u7684\u5b57\u7b26\u96c6\uff0c\u4ee5\u907f\u514d\u8f6c\u6362\u5f15\u8d77\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">17. <strong>\u4f7f\u7528\u4ee3\u7406\uff08Proxy\uff09\u6765\u5904\u7406\u5b57\u7b26\u96c6\u95ee\u9898<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u67d0\u4e9b\u9ad8\u5e76\u53d1\u7684\u73af\u5883\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5b57\u7b26\u96c6\u95ee\u9898\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7406\uff08\u5982 MySQL Proxy\uff09\u6765\u7edf\u4e00\u7ba1\u7406\u5b57\u7b26\u96c6\u3002\u4ee3\u7406\u53ef\u4ee5\u5728\u5ba2\u6237\u7aef\u548c\u6570\u636e\u5e93\u4e4b\u95f4\u505a\u5b57\u7b26\u96c6\u7684\u81ea\u52a8\u8f6c\u6362\uff0c\u786e\u4fdd\u6240\u6709\u7684\u6570\u636e\u4f20\u8f93\u90fd\u9075\u5faa\u7edf\u4e00\u7684\u5b57\u7b26\u96c6\u89c4\u8303\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u57fa\u672c\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\u914d\u7f6e\u5916\uff0c\u4ee5\u4e0b\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3 MySQL \u4e2d\u7684\u5b57\u7b26\u663e\u793a\u95ee\u9898\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>utf8mb4<\/code> \u66ff\u4ee3 <code>utf8<\/code><\/strong>\uff0c\u786e\u4fdd\u652f\u6301\u5b8c\u6574\u7684 Unicode \u5b57\u7b26\u3002<\/li>\n\n\n\n<li><strong>\u8c03\u6574\u8868\u548c\u5217\u7684\u6570\u636e\u7c7b\u578b<\/strong>\uff0c\u4f8b\u5982\u4f7f\u7528 <code>TEXT<\/code> \u7c7b\u578b\u6765\u5b58\u50a8\u957f\u6587\u672c\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u6279\u91cf\u6570\u636e\u8fc1\u79fb\u65f6\u4f7f\u7528 <code>CONVERT()<\/code><\/strong>\uff0c\u5904\u7406\u5b57\u7b26\u96c6\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u4fee\u590d\u4e71\u7801\u6570\u636e<\/strong>\uff0c\u901a\u8fc7 <code>CONVERT()<\/code> \u51fd\u6570\u6765\u4fee\u590d\u5df2\u7ecf\u5b58\u50a8\u7684\u4e71\u7801\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u8c03\u6574 MySQL \u670d\u52a1\u5668\u7684\u5b57\u7b26\u96c6\u914d\u7f6e<\/strong>\uff0c\u786e\u4fdd\u6570\u636e\u5e93\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\u3002<\/li>\n\n\n\n<li><strong>\u786e\u4fdd\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u5b57\u7b26\u96c6\u4e00\u81f4<\/strong>\uff0c\u907f\u514d\u5b57\u7b26\u96c6\u4e0d\u4e00\u81f4\u5bfc\u81f4\u7684\u4e71\u7801\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5b57\u7b26\u96c6\u8f6c\u6362<\/strong>\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u907f\u514d\u6570\u636e\u4e22\u5931\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u89e3\u51b3 MySQL \u4e2d\u7684\u5b57\u7b26\u663e\u793a\u95ee\u9898\uff0c\u786e\u4fdd\u652f\u6301\u591a\u8bed\u8a00\u5b57\u7b26\u548c\u7b26\u53f7\u7684\u6b63\u786e\u5b58\u50a8\u548c\u663e\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MySQL \u4e2d\uff0c\u5b57\u7b26\u663e\u793a\u95ee\u9898\u901a\u5e38\u662f\u7531\u4e8e\u6570\u636e\u5e93\u3001\u8868\u6216\u5217\u7684\u5b57\u7b26\u96c6\uff08charset\uff09\u548c\u6392\u5e8f\u89c4\u5219\uff08collatio [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-808","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=808"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/808\/revisions"}],"predecessor-version":[{"id":809,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/808\/revisions\/809"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}