今天安装了一个mysql在做数据迁移之后发现原来的字符变成了乱码.经过一翻折腾,终于搞定.
MySQL的字符集支持(Character Set Support)有两个方面:
字符集(Character set)和排序方式(Collation)。
对于字符集的支持细化到四个层次:
服务器(server),数据库(database),数据表(table)和连接(connection)。
mysql>show character set; \\显示系统支持的所有字符集 +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | dec8 | DEC West European | dec8_swedish_ci | 1 | | cp850 | DOS West European | cp850_general_ci | 1 | | hp8 | HP West European | hp8_english_ci | 1 | | koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 | | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 | | swe7 | 7bit Swedish | swe7_swedish_ci | 1 | | ascii | US ASCII | ascii_general_ci | 1 | | hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 | | koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 | | gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 | | greek | ISO 8859-7 Greek | greek_general_ci | 1 | | cp1250 | Windows Central European | cp1250_general_ci | 1 | | gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 | | latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 | | armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | cp866 | DOS Russian | cp866_general_ci | 1 | | keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 | | macce | Mac Central European | macce_general_ci | 1 | | macroman | Mac West European | macroman_general_ci | 1 | | cp852 | DOS Central European | cp852_general_ci | 1 | | latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 | | cp1251 | Windows Cyrillic | cp1251_general_ci | 1 | | cp1256 | Windows Arabic | cp1256_general_ci | 1 | | cp1257 | Windows Baltic | cp1257_general_ci | 1 | | binary | Binary pseudo charset | binary | 1 | | geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 | +----------+-----------------------------+---------------------+--------+ 28 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'character%'; //查看默认字符集 +--------------------------+---------------------------------+ | 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 | D:"mysql-5.0.37"share"charsets" | +--------------------------+---------------------------------+
mysql> SHOW VARIABLES LIKE 'collation_%';//字符集的排序方式 +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +----------------------+-----------------+
|
修改字符集:
如果你系统中支持的字符集中已有你要的字符集如UTF8,可以通过
mysql> SET character_set_client = utf8 ; mysql> SET character_set_connection = utf8 ; mysql> SET character_set_database = utf8 ; mysql> SET character_set_results = utf8 ; mysql> SET character_set_server = utf8 ;
mysql> SET collation_connection = utf8 ; mysql> SET collation_database = utf8 ; mysql> SET collation_server = utf8 ;
|
一般就算设置了表的默认字符集为utf8并且通过UTF-8编码发送查询,你会发现存入数据库的仍然是乱码。问题就出在这个connection连接层上。解决方法是在发送查询前执行一下下面这句
SET NAMES 'utf8';
它相当于下面的三句指令: SET character_set_client = utf8; SET character_set_results = utf8; SET character_set_connection = utf8;
|
如果你的系统支持字符集中没有你要的字符集,我的解决办法重新安装mysql在编译的时候加入
--with-extra-charsets=gb2312,gbk
下面是我编译mysql的时候所用的语句.
我的mysql版本是:5.0.51b-log Source distribution
# groupadd mysql # useradd mysql -g mysql -M -s /bin/false /nologin # ./configure –prefix=/opt/mysql \ 指定安装目录 --without-debug \去除debug模式 --with-extra-charsets=gb2312 \添加gb2312中文字符支持 --enable-assembler \使用一些字符函数的汇编版本 --without-isam \去掉isam表类型支持 现在很少用了 isam表是一种依赖平台的表 --without-innodb \去掉innodb表支持 innodb是一种支持事务处理的表,适合企业级应用 --with-pthread \强制使用pthread库(posix线程库) --enable-thread-safe-client \以线程方式编译客户端 --with-client-ldflags=-all-static \ --with-mysqld-ldflags=-all-static \以纯静态方式编译服务端和客户端 –with-raid \激活raid支持
# make # make install # scripts/mysql_install_db \生成mysql用户数据库和表文件 # cp support-files/my-medium.cnf /etc/my.cnf \copy配置文件,有large,medium,small三个环境下的,根据机器性能选择,如果负荷比较大,可修改里面的一些变量的内存使用值 # cp support-files/mysql.server /etc/init.d/mysqld //copy启动的mysqld文件 # chmod 700 /etc/init.d/mysqld //修改权限只能由ROOT用户操作 # cd /web # chmod 750 mysql -R # chgrp mysql mysql -R # chown mysql mysql/var -R
# chkconfig –add mysqld # chkconfig –level 345 mysqld on # service mysqld start # netstat -atln
|
阅读(1129) | 评论(0) | 转发(0) |