测试的机器是ubuntu 12.04个别linux发行版可能略有不同。
登陆mysql查看当前字符集命令:
mysql> show variable 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/ |
+--------------------------+----------------------------+
查看当前状态
mysql> status
--------------
mysql Ver 14.14 Distrib 5.5.34, for debian-linux-gnu (x86_64) using readline 6.2
Connection id: 42
Current database:
Current user: test@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.34-0ubuntu0.12.04.1 (Ubuntu)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 46 min 52 sec
Threads: 4 Questions: 170 Slow queries: 0 Opens: 177 Flush tables: 1 Open tables: 42 Queries per second avg: 0.060
配置/etc/mysql/my.cnf文件:
找到[client]在[client]下面增加 default-character-set=utf8
[client] port = 3306 socket = /var/run/mysqld/mysqld.sock default-character-set = utf8
找到[mysqld] 下面增加:
character-set-server =utf8
init_connect='SET NAMES utf8'
[mysqld] # # * Basic Settings # user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp lc-messages-dir = /usr/share/mysql skip-external-locking character-set-server=utf8 init_connect='SET NAMES utf8'
最后在[mysql]下增加 default-character-set=utf8
[mysql] default-character-set=utf8
重启mysql就可以了
sudo /etc/init.d/mysql restart