最近用一台LINUX机器做数据库服务器,上面装了MYSQL数据库,项目的数据库就全都放这台机器,但是默认的MYSQL编码都是latin1的,如果存中文就会有问题,解决方案如下:
#vi /etc/my.cnf
在里面相应的地方加上如下编码设置
[mysqld]
default-character-set=gbk
[mysql.server]
default-character-set=gbk
[mysqld_safe]
default-character-set=gbk
[client]
default-character-set=gbk
[mysql]
default-character-set=gbk
重启MYSQL即可。
如果不知道MYSQL在哪个地方,运行
#whereis mysql
即可找到MYSQL安装在哪个地方
如果想查设置是不是成功了,可以运行如下命令:
# mysql -u root -proot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 5.0.22
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | gbk |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
上面的就已经显示编码设置成功了。