Ubuntu下MysqlServer默认编码的设置
配置
以下列出了Linux下比较流行发行版Ubuntu的两个LTS版本基本配置,其他发行版可以参考其中的参数,配置文件路径可能有差异。
Ubuntu 14.04 环境下的配置
Ubuntu 14.04 下只需要修改一个文件: /etc/my.cnf
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8
[mysqld]
collation-server = utf8_generic_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
Ubuntu 16.04 环境下的配置
Ubuntu 16.04 中MysqlServer配置被分成了两个文件来维护,依次将相应的配置区(section)放到对应的配置文件里就可以了。
/etc/mysql/conf.d/mysql.cnf
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8
/etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
init-connect='SET NAMES utf8'
character-set-server = utf8
验证修改结果
配置完成后重启数Mysql服务器,检查数据库编码
mysql> show variables like '%char%';
配置正确的话会将所有编码设置成utf8
+--------------------------+----------------------------+
| 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.01 sec)