Home > Database > Mysql Tutorial > 实现MySQL支持中文该如何操作?

实现MySQL支持中文该如何操作?

WBOY
Release: 2016-06-07 16:13:48
Original
1249 people have browsed it

以下的文章主要介绍的是实现MySQL支持中文的实际操作步骤,我们大家都知道系统自带的MySQL数据库默认字符集并不是gbk,所以给数据库推广应用与中文程序的开发过程中带来极大的不便。 在没完没了的GBK和UTF8的转换过程中消耗了我们无数的精力以及系统的资源.经

以下的文章主要介绍的是实现MySQL支持中文的实际操作步骤,我们大家都知道系统自带的MySQL数据库默认字符集并不是gbk,所以给数据库推广应用与中文程序的开发过程中带来极大的不便。

在没完没了的GBK和UTF8的转换过程中消耗了我们无数的精力以及系统的资源.经过摸索尝试解决了中文MySQL支持问题,经验与大家分享一下.

先将让slack支持中文,方法可以看"linuxsir.org 之Slackware讨论区",然后重新编译MySQL(和PHP搭配之最佳组合),原来系统MySQL(和PHP搭配之最佳组合)的要先用卸载.

login:root

shell> removepkg MySQL(和PHP搭配之最佳组合)

shell> groupadd MySQL(和PHP搭配之最佳组合)

shell> cd /app

shell> mkdir MySQL(和PHP搭配之最佳组合)

shell> chmod 744 MySQL(和PHP搭配之最佳组合)

shell> useradd -G MySQL(和PHP搭配之最佳组合) -s /bin/bash –p /app/MySQL(和PHP搭配之最佳组合) MySQL(和PHP搭配之最佳组合)

shell> chown -R MySQL(和PHP搭配之最佳组合).MySQL(和PHP搭配之最佳组合) MySQL(和PHP搭配之最佳组合)

下载MySQL(和PHP搭配之最佳组合)4.x.x

<ol class="dp-xml">
<li class="alt"><span><span>shell</span><span class="tag">></span><span> gunzip </span><span class="tag"><span> </span></span>MySQL<span><span>(和PHP搭配之最佳组合)-VERSION.tar.gz | tar -xvf -  </span></span></span></li>
<li>
<span>shell</span><span class="tag">></span><span> cd </span>MySQL<span>(和PHP搭配之最佳组合)-VERSION  </span>
</li>
<li class="alt">
<span>shell</span><span class="tag">></span><span> </span><span class="attribute">CFLAGS</span><span>=</span><span class="attribute-value">"-O3 -mcpu=pentium3"</span><span> \  </span>
</li>
<li>
<span class="attribute">CXX</span><span>=</span><span class="attribute-value">gcc</span><span> \  </span>
</li>
<li class="alt">
<span class="attribute">XXFLAGS</span><span>=</span><span class="attribute-value">"-O3 -mcpu=pentium3 -felide-constructors"</span><span> \  </span>
</li>
<li>
<span>./configure </span><span class="attribute">--prefix</span><span>=/app/</span>MySQL<span>(和PHP搭配之最佳组合) \  </span>
</li>
<li class="alt">
<span class="attribute">--with--charset</span><span>=</span><span class="attribute-value">gbk</span><span> \  </span>
</li>
<li>
<span class="attribute">--with-extra-charsets</span><span>=</span><span class="attribute-value">"gbk gb2312 big5 utf8"</span><span>\  </span>
</li>
<li class="alt">
<span class="attribute">--with-unix-socket-path</span><span>=/etc/</span>MySQL<span>(和PHP搭配之最佳组合).sock \  </span>
</li>
<li><span>--enable-local-infile \  </span></li>
<li class="alt"><span>--enable-thread-safe-client \  </span></li>
<li><span>--enable-assembler \  </span></li>
<li class="alt">
<span class="attribute">--with-client-ldflags</span><span>=-all-static \  </span>
</li>
<li>
<span>--with-</span>MySQL<span>(和PHP搭配之最佳组合)</span><span class="attribute">d-ldflags</span><span>=-all-static </span>
</li>
</ol>
Copy after login

mcpu 根据CPU类型优化编译,可以让你的mysq表现更好!可选项目很多:i386, i486, i586, i686, pentium, pentium-mmx, pentiumpro, pentium2, pentium3, pentium4, k6, k6-2, k6-3,

<ol class="dp-xml">
<li class="alt"><span><span>athlon, athlon-tbird, athlon-4, athlon-xp,athlon-mp,winchip-c6, winchip2 , c3.   </span></span></li>
<li>
<span>shell</span><span class="tag">></span><span> make  </span>
</li>
</ol>
Copy after login

错误时候

shell> make clean

再回上一步

<ol class="dp-xml">
<li class="alt"><span><span>shell</span><span class="tag">></span><span> make install  </span></span></li>
<li>
<span>shell</span><span class="tag">></span><span> cp support-files/my-medium.cnf /etc/my.cnf  </span>
</li>
<li class="alt">
<span>shell</span><span class="tag">></span><span> cd /app/</span>MySQL<span>(和PHP搭配之最佳组合)  </span>
</li>
<li>
<span>shell</span><span class="tag">></span><span> bin/</span>MySQL<span>(和PHP搭配之最佳组合)_install_db </span><span class="attribute">--user</span><span>=</span><span class="attribute-value">root</span><span> </span>
</li>
<li class="alt">
<span>shell</span><span class="tag">></span><span> chown -R root .  </span>
</li>
<li>
<span>shell</span><span class="tag">></span><span> chown -R </span>MySQL<span>(和PHP搭配之最佳组合) var  </span>
</li>
<li class="alt">
<span>shell</span><span class="tag">></span><span> chgrp -R </span>MySQL<span>(和PHP搭配之最佳组合) .  </span>
</li>
<li>
<span>shell</span><span class="tag">></span><span> ./bin/</span>MySQL<span>(和PHP搭配之最佳组合)d_safe </span><span class="attribute">--user</span><span>=</span><span class="attribute-value">root</span><span> </span><span class="attribute">--default-character-set</span><span>=</span><span class="attribute-value">gbk</span><span> &  </span>
</li>
<li class="alt">
<span>shell</span><span class="tag">></span><span> ./bin/</span>MySQL<span>(和PHP搭配之最佳组合)admin -u root password 'new-password'  </span>
</li>
<li>
<span>shell</span><span class="tag">></span><span> ./bin/</span>MySQL<span>(和PHP搭配之最佳组合) -u root </span><span class="attribute">--default-character-set</span><span>=</span><span class="attribute-value">gbk</span><span> -p  </span>
</li>
<li class="alt">MySQL<span>(和PHP搭配之最佳组合)</span><span class="tag">></span><span> status  </span>
</li>
<li><span>...  </span></li>
<li class="alt"><span>Server characterset: gbk  </span></li>
<li><span>Db characterset: gbk  </span></li>
<li class="alt"><span>Client characterset: gbk  </span></li>
<li><span>Conn. characterset: gbk  </span></li>
<li class="alt"><span>.... </span></li>
</ol>
Copy after login

看见characterset全是gbk就算成功了.

在UTF8字符集的老数据库中,创建中文MySQL支持表脚本如下,对字段加上字符集说明

<ol class="dp-xml">
<li class="alt"><span><span>CREATE TABLE `test`.`cn` (  </span></span></li>
<li><span>`zh` CHAR(10) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NOT NULL,  </span></li>
<li class="alt"><span>`中文` INT(11) NOT NULL  </span></li>
<li><span>)  </span></li>
<li class="alt">
<span class="attribute">ENGINE</span><span> = </span><span class="attribute-value">MYISAM</span><span>; </span>
</li>
</ol>
Copy after login

注:CHARACTER SET 写GB2312 和gbk都可以,gbk优于gb2312.

对于新创建的数据库加个默认字符集声明GBK,就不用建表时候每个字段声明了,这样看起来简单流畅,没有特殊的痕迹,建议多采用这种方法.

<ol class="dp-xml">
<li class="alt">MySQL<span>(和PHP搭配之最佳组合)</span><span>> CREATE DATABASE dbCN DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;  </span>
</li>
<li><span>CREATE TABLE `dbCN`.`cn` (  </span></li>
<li class="alt"><span>`zh` CHAR(10) NOT NULL,  </span></li>
<li><span>`中文` INT(11) NOT NULL  </span></li>
<li class="alt"><span>)  </span></li>
<li>
<span class="attribute">ENGINE</span><span> = </span><span class="attribute-value">MYISAM</span><span>;  </span>
</li>
<li class="alt">MySQL<span>(和PHP搭配之最佳组合)</span><span class="tag">></span><span> insert into cn values("真的?",1); </span>
</li>
</ol>
Copy after login

注:如果这些脚本用bin/MySQL(和PHP搭配之最佳组合)命令行是输入不了,可以 用MySQL(和PHP搭配之最佳组合) query Browser,或者编程执行.

<ol class="dp-xml">
<li class="alt">shell> bin/MySQL<span>(和PHP搭配之最佳组合) -u root </span><span class="attribute">--default-character-set</span><span>=</span><span>gbk -p  </span>
</li>
<li>MySQL<span>(和PHP搭配之最佳组合)</span><span class="tag">></span><span>use test  </span>
</li>
<li class="alt">MySQL<span>(和PHP搭配之最佳组合)</span><span class="tag">></span><span> select * from cn;  </span>
</li>
<li><span>+--------+------+  </span></li>
<li class="alt"><span>| zh | 中文 |  </span></li>
<li><span>+--------+------+  </span></li>
<li class="alt"><span>| 真的?| 1 |  </span></li>
<li><span>+--------+------+  </span></li>
<li class="alt"><span>1 row in set (0.00 sec)  </span></li>
<li><span>--------------------------------------------- </span></li>
</ol>
Copy after login

最后修改启动文件/etc/rc.d/rc.MySQL(和PHP搭配之最佳组合)

改路径/app/MySQL(和PHP搭配之最佳组合)/bin/MySQL(和PHP搭配之最佳组合)d_safe

改路径/app/MySQL(和PHP搭配之最佳组合)/var/web.pid

/app/MySQL(和PHP搭配之最佳组合)/bin/MySQL(和PHP搭配之最佳组合)d_safe 后面加入 --user=root --default-character-set=gbk

以上的相关内容就是对MySQL支持中文的介绍,望你能有所收获。


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template