如何使MySQL正确处理UTF-8 [英] How to make MySQL handle UTF-8 properly

查看:65
本文介绍了如何使MySQL正确处理UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天问的一个问题建议我确保我的数据库可以正确处理UTF-8字符.我该如何使用MySQL?

One of the responses to a question I asked yesterday suggested that I should make sure my database can handle UTF-8 characters correctly. How I can do this with MySQL?

推荐答案

更新:

简短的回答-您几乎应该始终使用utf8mb4字符集和utf8mb4_unicode_ci归类.

Short answer - You should almost always be using the utf8mb4 charset and utf8mb4_unicode_ci collation.

要更改数据库:

ALTER DATABASE dbname CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

请参阅:

之间有什么区别utf8_general_ci和utf8_unicode_ci

转换指南: https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-conversion.html

原始答案:

MySQL 4.1及更高版本具有默认字符集UTF-8.您可以在my.cnf文件中对此进行验证,请记住同时设置两者客户端和服务器(default-character-setcharacter-set-server).

MySQL 4.1 and above has a default character set of UTF-8. You can verify this in your my.cnf file, remember to set both client and server (default-character-set and character-set-server).

如果您希望将现有数据转换为UTF-8,请转储数据库,然后将其作为UTF-8导入,确保:

If you have existing data that you wish to convert to UTF-8, dump your database, and import it back as UTF-8 making sure:

  • 在查询/插入数据库之前使用SET NAMES utf8
  • 在创建新表时使用DEFAULT CHARSET=utf8
  • 此时,您的MySQL客户端和服务器应位于UTF-8中(请参见my.cnf).请记住,您使用的任何语言(例如PHP)也必须是UTF-8.某些版本的PHP将使用他们自己的MySQL客户端库,该客户端库可能不支持UTF-8.
  • use SET NAMES utf8 before you query/insert into the database
  • use DEFAULT CHARSET=utf8 when creating new tables
  • at this point your MySQL client and server should be in UTF-8 (see my.cnf). remember any languages you use (such as PHP) must be UTF-8 as well. Some versions of PHP will use their own MySQL client library, which may not be UTF-8 aware.

如果要迁移现有数据,请记住先备份!当事情没有按计划进行时,可能会发生很多奇怪的数据砍伐事件!

If you do want to migrate existing data remember to backup first! Lots of weird choping of data can happen when things don't go as planned!

一些资源:

  • 完成UTF-8迁移(cdbaby.com)
  • 有关PHP函数已准备好UTF-8 (请注意其中的一些内容)信息已过时)
  • complete UTF-8 migration (cdbaby.com)
  • article on UTF-8 readiness of php functions (note some of this information is outdated)

这篇关于如何使MySQL正确处理UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆