提要系统自带的mysql默认字符集不是gbk因此给数据库的推广应用以及中文程序的开发带来极大的不便在没完没了的GBK和UTF的转换过程中消耗了我们无数的精力以及系统的资源经过摸索尝试解决了中文支持问题经验与大家分享一下
先将让slack支持中文方法可以看 之Slackware讨论区然后重新编译mysql原来系统mysql的要先用卸载
login:root
shell> removepkg mysql
shell> groupadd mysql
shell> cd /app
shell> mkdir mysql
shell> chmod mysql
shell> useradd G mysql s /bin/bash –p /app/mysql mysql
shell> chown R mysqlmysql mysql
下载mysqlxx
shell> gunzip < mysqlVERSIONtargz | tar xvf
shell> cd mysqlVERSION
shell> CFLAGS=O mcpu=pentium \
CXX=gcc \
XXFLAGS=O mcpu=pentium felideconstructors \
/configure prefix=/app/mysql \
withcharset=gbk \
withextracharsets=gbk gb big utf\
withunixsocketpath=/etc/mysqlsock \
enablelocalinfile \
enablethreadsafeclient \
enableassembler \
withclientldflags=allstatic \
withmysqldldflags=allstatic
//mcpu 根据CPU类型优化编译可以让你的mysq表现更好!可选项目很多i i i i pentium pentiummmx pentiumpro pentium pentium pentium k k k athlon athlontbird athlon athlonxpathlonmpwinchipc winchip c
shell> make
错误时候
shell> make clean
再回上一步
shell> make install
shell> cp supportfiles/f /etc/f
shell> cd /app/mysql
shell> bin/mysql_install_db user=root
shell> chown R root
shell> chown R mysql var
shell> chgrp R mysql
shell> /bin/mysqld_safe user=root defaultcharacterset=gbk &
shell> /bin/mysqladmin u root password newpassword
shell> /bin/mysql u root defaultcharacterset=gbk p
mysql> status
Server characterset: gbk
Db characterset: gbk
Client characterset: gbk
Conn characterset: gbk
看见characterset全是gbk就算成功了
在UTF字符集的老数据库中创建中文支持表脚本如下对字段加上字符集说明
CREATE TABLE `test``cn` (
`zh` CHAR() CHARACTER SET gb COLLATE gb_chinese_ci NOT NULL
`中文` INT() NOT NULL
)
ENGINE = MYISAM;
注:CHARACTER SET 写GB 和gbk都可以gbk优于gb
对于新创建的数据库加个默认字符集声明GBK就不用建表时候每个字段声明了这样看起来简单流畅没有特殊的痕迹建议多采用这种方法
mysql> CREATE DATABASE dbCN DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
CREATE TABLE `dbCN``cn` (
`zh` CHAR() NOT NULL
`中文` INT() NOT NULL
)
ENGINE = MYISAM;
mysql> insert into cn values(真的?);
注:如果这些脚本用bin/mysql命令行是输入不了可以 用mysql query Browser或者编程执行
shell> bin/mysql u root defaultcharacterset=gbk p
mysql>use test
mysql> select * from cn;
+++
| zh | 中文 |
+++
| 真的?| |
+++
row in set ( sec)
最后修改启动文件/etc/rcd/rcmysqld
改路径/app/mysql/bin/mysqld_safe
改路径/app/mysql/var/webpid
/app/mysql/bin/mysqld_safe 后面加入 user=root defaultcharacterset=gbk