How to make MySQL handle UTF-8 properly

前端 未结 14 2562
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 06:38

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?

相关标签:
14条回答
  • 2020-11-22 07:14

    To change the character set encoding to UTF-8 for the database itself, type the following command at the mysql> prompt. USE ALTER DATABASE.. Replace DBNAME with the database name:

    ALTER DATABASE DBNAME CHARACTER SET utf8 COLLATE utf8_general_ci;
    

    This is a duplicate of this question How to convert an entire MySQL database characterset and collation to UTF-8?

    0 讨论(0)
  • 2020-11-22 07:20

    MySQL 4.1 and above has a default character set that it calls utf8 but which is actually only a subset of UTF-8 (allows only three-byte characters and smaller).

    Use utf8mb4 as your charset if you want "full" UTF-8.

    0 讨论(0)
  • 2020-11-22 07:22

    Set your database connection to UTF8:

      if($handle = @mysql_connect(DB_HOST, DB_USER, DB_PASS)){          
             //set to utf8 encoding
             mysql_set_charset('utf8',$handle);
      }
    
    0 讨论(0)
  • 2020-11-22 07:27

    SET NAMES UTF8

    This is does the trick

    0 讨论(0)
  • 2020-11-22 07:29

    I followed Javier's solution, but I added some different lines in my.cnf:

    [myslqd]
    skip-character-set-client-handshake
    collation_server=utf8_unicode_ci
    character_set_server=utf8 
    

    I found this idea here: http://dev.mysql.com/doc/refman/5.0/en/charset-server.html in the first/only user comment on the bottom of the page. He mentions that skip-character-set-client-handshake has some importance.

    0 讨论(0)
  • 2020-11-22 07:31

    To make this 'permanent', in my.cnf:

    [client]
    default-character-set=utf8
    [mysqld]
    character-set-server = utf8
    

    To check, go to the client and show some variables:

    SHOW VARIABLES LIKE 'character_set%';
    

    Verify that they're all utf8, except ..._filesystem, which should be binary and ..._dir, that points somewhere in the MySQL installation.

    0 讨论(0)
提交回复
热议问题