/ 中存储网

Ubuntu系统中设置MySQL数据库的默认字符集

2014-07-13 15:49:01 来源:中存储网

当我们在Ubuntu下安装完MySQL后,一般都需要设置数据库的默认字符集。

#先看看mysql中的默认字符集。

#登入mysql

mysql -h localhost -u root -p

#查看 character 相关的属性

SHOW VARIABLES LIKE '%character%';

#会得到如下的结果:

+--------------------------+----------------------------+

| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

#打开/etc/mysql/my.cnf

sudo gedit /etc/mysql/my.cnf

#在[client]内,添加 default-character-set 属性

[client]

...

...

default-character-set=utf8

#在[mysqld]内,添加default-character-set=utf8, character-set-server=utf8, default-collation=utf8_unicode_ci

[mysqld]

...

...

default-character-set=utf8

character-set-server=utf8

default-collation=utf8_unicode_ci

#重启 mysql

sudo service mysql restart

#再登进 mysql 看一下字符属性,得如下结果,ok,搞定了


+--------------------------+----------------------------+


| 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/ |
+--------------------------+----------------------------+