Ubuntu下设置MySQL字符集为utf8

Ubuntu下设置MySQL字符集为utf8

1.mysql配置文件地址
/etc/mysql/my.cnf

2.在[mysqld]在下方添加以下代码
[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

3.重启mysql服务
sudo service mysql restart

4.检测字符集是否更新成utf8.
进入mysql,mysql -u root -p,输入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/ |
+--------------------------+----------------------------+

注意事项:在修改字符集之前已经建立的数据库,character_set_database值不会发生改变,往数据库中插入中文数据仍然会显示乱码,所以最好在安装完MySQL后就将字符集改成utf8,否则后续修改会较麻烦。

相关推荐