Unreadable Code of MySQL

之前一直英文,没有意识到这个问题,直到今天碰到了中文才发现全是乱码。

UBUNTU:

# vi /etc/mysql/my.cnf

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

# service mysql restart

$ mysql -u root -p
> show variables like 'char%';

CentOS:

[# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf]
不是 my.ini,my.ini 是 win 下的格式。

# vi /etc/my.cnf

[client]
default-character-set=utf8

[mysqld]
default-character-set=utf8
init_connect='SET NAMES utf8'

测试:
>\s
Client characterset: utf8
Server characterset: utf8

> show variables like ‘char%’;
+————————–+—————————-+
| Variable_name            | Value                      |
+————————–+—————————-+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+————————–+—————————-+
8 rows in set (0.00 sec)

在创建数据库的时,以防万一,指定编码:

>CREATE DATABASE test DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

修改数据库编码:

> ALTER DATABASE 'test' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin

修改表的编码:

> ALTER TABLE 'category' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin

修改字段的编码:

>ALTER TABLE 'test' CHANGE 'dd' 'dd' VARCHAR( 45 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL

祝 utf8 一统江湖吧!

参考:
http://blog.csdn.net/songkexin/archive/2009/11/25/4867992.aspx

  • http://mucid.me Mucid

    嗯django也是utf-8