修改两个文件:
①
/etc/mysql/conf.d/mysql.cnf在[mysql]后添加:
[mysql]
default-character-set = utf8
②
/etc/mysql/mysql.conf.d/mysqld.cnf在相应位置添加:
[client] // 默认没有,添加
default-character-set=utf8
[mysqld]
character-set-server = utf8
查看字符集编码:
mysql> 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/ |
+--------------------------+----------------------------+
如果显示这样就没问题了, 另外,要重新建数据库哦,旧的数据库和表什么的均还是出现乱码的。
查看数据库编码:
show create database db_name;
查看表编码:
show create table tbl_name;
查看字段编码:
show full columns from tbl_name;
show full fields from tbl_name;
mysql> 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/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
character_set_client:客户端的字符集
character_set_results:结果字符集
character_set_connection:连接字符集
这三个系统参数的作用:
信息输入路径:client--connection--server (数据传送方向从左到右)
信息输出路径:server--connection--results (数据传送方向从左到右)
故这几个系统参数的值须相同,不然以不同的编码方式传送数据,若编码方式不兼容,则容易造成乱码的问题
编程用mysql_query查询时,如果插入中文出现乱码,则设置对应上面的编码格式如utf8:
mysql_query(pConn, "SET NAMES utf8");