插入中文数据变成问号,一般都是因为字符集没有设置成utf8的原因
1,查看字符集
- show variables like ‘character%’;
- show variables like ‘%char%’;
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)
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| 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)
2,修改配置文件my.cnf
- 在/usr/share/mysql/ 中找到my.cnf的配置文件,
- 拷贝其中的my-huge.cnf 到 /etc/ 并命名为my.cnf
- mysql 优先选中 /etc/ 下的配置文件
- MySQL5.7配置文件位置是/etc/my.cnf或者/etc/mysql/my.cnf,如果字符集不是utf-8直接进入配置文件修改即可。
[client]
default-character-set=utf8
[mysqld]
character_set_server=utf8
character_set_client=utf8
collation-server=utf8_general_ci
[mysql]
default-character-set=utf8
- 注意:安装MySQL完毕之后,第一件事就是修改字符集编码。
3,重新启动mysql
- 但是原库的设定不会发生变化,参数修改之对新建的数据库生效
4,已生成的库表字符集如何变更
- 修改数据库的字符集
mysql> alter database mytest character set 'utf8';
- 修改数据表的字符集
mysql> alter table user convert to character set 'utf8';
但是原有的数据如果是用非’utf8’编码的话,数据本身不会发生改变。