当前位置: > 数据库 > MySQL >

CentOS查看和修改MySQL字符集

时间:2015-04-22 16:00来源:linux.it.net.cn 作者:IT
客户提供了MySQL的环境,但是不支持中文,

通过以下命令查看了MySQL的字符集

连接上mysql服务,输入以下命令


mysql>show variables like 'character_set%';

显示如下:


01.+--------------------------+----------------------------+ 
02.| Variable_name            | Value                      | 
03.+--------------------------+----------------------------+ 
04.| character_set_client     | latin1                     | 
05.| character_set_connection | latin1                     | 
06.| character_set_database   | latin1                     | 
07.| character_set_filesystem | binary                     | 
08.| character_set_results    | latin1                     | 
09.| character_set_server     | latin1                     | 
10.| character_set_system     | utf8                       | 
11.| character_sets_dir       | /usr/share/mysql/charsets/ | 
12.+--------------------------+----------------------------+ 
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+


为了让MySQL支持中文,需要把字符集改变成UTF8,

方法如下:

1. 打开etc/my.cnf

2. 修改成如下内容,保存


[plain] view plaincopyprint?
01.[client] 
02.port=3306 
03.socket=/var/lib/mysql/mysql.sock 
04.default-character-set=utf8 
05. 
06.[mysqld] 
07.datadir=/var/lib/mysql 
08.socket=/var/lib/mysql/mysql.sock 
09.user=mysql 
10.# Disabling symbolic-links is recommended to prevent assorted security risks 
11.symbolic-links=0 
12.character-set-server=utf8 
13. 
14.[mysql] 
15.no-auto-rehash 
16.default-character-set=utf8 
17. 
18.[mysqld_safe] 
19.log-error=/var/log/mysqld.log 
20.pid-file=/var/run/mysqld/mysqld.pid 
[client]
port=3306
socket=/var/lib/mysql/mysql.sock
default-character-set=utf8

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character-set-server=utf8

[mysql]
no-auto-rehash
default-character-set=utf8

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid



3. 重启mysql服务:service mysqld restart

再次查看mysql编码,已经更新成如下信息


01.+--------------------------+----------------------------+ 
02.| Variable_name            | Value                      | 
03.+--------------------------+----------------------------+ 
04.| character_set_client     | utf8                       | 
05.| character_set_connection | utf8                       | 
06.| character_set_database   | utf8                       | 
07.| character_set_filesystem | binary                     | 
08.| character_set_results    | utf8                       | 
09.| character_set_server     | utf8                       | 
10.| character_set_system     | utf8                       | 
11.| character_sets_dir       | /usr/share/mysql/charsets/ | 
12.+--------------------------+----------------------------+  


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容