Ubuntu修改mysql默认编码的方法

ubuntu使用apt-get安装mysql后,server的默认编码是latin1,下面把server的编码修改成utf8。

编码相关信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> 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/ |
+--------------------------+----------------------------+

可以看到character_set_database 和 character_set_server 的编码都是latin1。

修改mysql的编码

打开mysql的配置文件/etc/mysql/my.cnf

1
vim /etc/mysql/my.cnf

在[mysqld]里追加

1
character-set-server=utf8

保存后,重启mysql。

1
/etc/init.d/mysql restart

编码就正确了。

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+

完成。

上一篇:数据库 MySQL安装图解


下一篇:MySQL安装常见问题(找不到文件,系统服务无法启动...)