创建数据库的时候用的以下语句:
CREATE DATABASE dbname DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;在本地测试没问题,但是上传到VPS执行之后出现了乱码问题。
网上看到说执行show variables like 'character%'; 结果如下:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
网上提到可以执行以下代码将编码改过来,但是这种方法在mysql服务重启后就失效了。
mysql> set character_set_client=utf8 ;永久修改编码的方法应该是修改/etc/mysql/my.cnf
mysql> set character_set_connection=utf8 ;
mysql> set character_set_database=utf8 ;
mysql> set character_set_database=utf8 ;
mysql> set character_set_results=utf8 ;
mysql> set character_set_server=utf8 ;
mysql> set character_set_system=utf8 ;
在[client]下添加
default-character-set=utf8在[mysqld]下添加(5.5版本):
character-set-server=utf8
collation-server=utf8_general_ci
或添加(旧版本):
default-character-set=utf8然后退出,执行 service mysql restart 重启mysql即可