对于不同的mysql版本有不同的修改方式
查看默认字符集
mysql> show variables like 'collation_%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| 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)
修改
找到mysql的配置文件
➜ find / -iname '*.cnf' -print
/usr/share/doc/mysql-server-5.5.54/my-small.cnf
/usr/share/doc/mysql-server-5.5.54/my-medium.cnf
/usr/share/doc/mysql-server-5.5.54/my-large.cnf
/usr/share/doc/mysql-server-5.5.54/my-huge.cnf
/usr/share/doc/mysql-server-5.5.54/my-innodb-heavy-4G.cnf
/usr/share/mysql/my-small.cnf
/usr/share/mysql/my-medium.cnf
/usr/share/mysql/my-large.cnf
/usr/share/mysql/my-huge.cnf
/usr/share/mysql/my-innodb-heavy-4G.cnf
/etc/my.cnf
/etc/pki/tls/openssl.cnf
mysql启动时被加载的是/etc/my.cnf
(有的系统是/etc/mysql/my.cnf
)
版本<=5.5
修改 /etc/my.cnf
(有的系统在/etc/mysql/my.cnf
)
在[client]下添加
default-character-set=utf8
在[mysqld]下添加
default-character-set=utf8
版本>= 5.5
修改 /etc/my.cnf
(有的系统在/etc/mysql/my.cnf
)
在[mysqld]下添加
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
mysql> show variables like 'collation_%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| 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)
参考链接
[1] 关于查看字符集问题:http://www.cnblogs.com/donqiang/articles/2057972.html
[2] 关于版本问题:http://stackoverflow.com/questions/3513773/change-mysql-default-character-set-to-utf-8-in-my-cnf#comment28172456_10866836