最近在CentOS下安装MySQL程序之后,导入数据发现有中文异常,将解决办法整理出来以便日后参考:
步骤如下:
第一、安装mysql之后默认的字符编码为latin1
登录mysql之后可以通过 show variables like '%char%'查看,显示如下
+----------------------+---------------------
| Variable_name | Value
+-----------------------+---------------------
| character_set_client | latin1
| character_set_connection | latin1
| character_set_database | latin1
| character_set_filesystem | binary
| character_set_results | latin1
| character_set_server | latin1
| character_set_system | utf8
| character_sets_dir | /usr/share/mysql/charsets/
+---------------------+----------------------
第二、进入/usr/share/mysql目录之后,拷贝my-medium.cnf 到/etc下
[root@linuxidc mysql]# cp my-medium.cnf /etc/my.cf
编辑my.cf
[root@linuxidc mysql]# vim my.cf
在最后添加:
[client]
default-character-set=utf8
[mysqld_safe]
default-character-set=utf8
[mysqld]
default-character-set=utf8
[mysql]
default-character-set=utf8
保存退出。
第三、重启mysql服务
[root@linuxidc mysql]# /etc/init.d/mysql restart
第四、登录mysql之后 再次通过show variables like '%char%'查看,显示如下
+--------------------------+----------------------------+
| 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 | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
OK 解决完毕!!
--------------------------------------分割线 --------------------------------------
Ubuntu 14.04下安装MySQL
Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL
Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群
Ubuntu 12.04下源代码安装MySQL5.6以及Python-MySQLdb
--------------------------------------分割线 --------------------------------------