Home > Database > Mysql Tutorial > body text

解析MySQL完全中文化

WBOY
Release: 2016-06-07 16:07:05
Original
892 people have browsed it

由于系统自带的MySQL默认字符集不是gbk,因此给数据库的推广应用以及中文程序的开发带来极大的不便,在没完没了的GBK和UTF8的转换过程中消耗了我们无数的精力以及系统的资源。 先将slack支持中文,然后重新编译MySQL,原来系统MySQL的要先卸载: login:root

由于系统自带的MySQL默认字符集不是gbk,因此给数据库的推广应用以及中文程序的开发带来极大的不便,在没完没了的GBK和UTF8的转换过程中消耗了我们无数的精力以及系统的资源。

先将slack支持中文,然后重新编译MySQL,原来系统MySQL的要先卸载:

login:root<br>shell> removepkg mysql<br>shell> groupadd mysql<br>shell> cd /app<br>shell> mkdir mysql<br>shell> chmod 744 mysql<br>shell> useradd -G mysql -s <br>/bin/bash –p /app/mysql mysql<br>shell> chown -R mysql.mysql mysql
Copy after login

再下载MySQL4.x.x:

shell> gunzip | tar -xvf -<br>shell> cd mysql-VERSION<br>shell> CFLAGS="-O3 -mcpu=pentium3" \<br>CXX=gcc \<br>XXFLAGS="-O3 -mcpu=pentium3 <br>-felide-constructors" \<br>./configure --prefix=/app/mysql \<br>--with--charset=gbk \<br>--with-extra-charsets="gbk gb2312 <br>big5 utf8"\<br>--with-unix-socket-path=/etc/mysql.sock \<br>--enable-local-infile \<br>--enable-thread-safe-client \<br>--enable-assembler \<br>--with-client-ldflags=-all-static \<br>--with-mysqld-ldflags=-all-static
Copy after login

//-mcpu 根据CPU类型优化编译,可以让你的MySQL表现更好。可选项目很多:i386, i486, i586, i686, pentium, pentium-mmx, pentiumpro, pentium2, pentium3, pentium4, k6, k6-2, k6-3, athlon, athlon-tbird, athlon-4, athlon-xp,athlon-mp,winchip-c6, winchip2 , c3。执行:shell> make。错误时候执行:shell> make clean。

再回上一步:

<p>shell> make install<br>shell> cp support-files/my-medium.cnf /etc/my.cnf<br>shell> cd /app/mysql<br>shell> bin/mysql_install_db --user=root<br>shell> chown -R root .<br>shell> chown -R mysql var<br>shell> chgrp -R mysql .<br>shell> ./bin/mysqld_safe --user=root <br>--default-character-set=gbk &<br>shell> ./bin/mysqladmin -u root <br>password 'new-password'<br>shell> ./bin/mysql -u root <br>--default-character-set=gbk -p<br>mysql> status<br>...<br>Server characterset: gbk<br>Db characterset: gbk<br>Client characterset: gbk<br>Conn. characterset: gbk<br>....</p>
Copy after login

看见characterset全是gbk就成功了。

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

<p>CREATE TABLE `test`.`cn` (<br>`zh` CHAR(10) CHARACTER SET <br>gb2312 COLLATE gb2312_chinese_ci NOT NULL,<br>`中文` INT(11) NOT NULL<br>)<br>ENGINE = MYISAM;</p>
Copy after login

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

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

<p>mysql> CREATE DATABASE dbCN DEFAULT <br>CHARACTER SET gbk COLLATE gbk_chinese_ci;<br>CREATE TABLE `dbCN`.`cn` (<br>`zh` CHAR(10) NOT NULL,<br>`中文` INT(11) NOT NULL<br>)<br>ENGINE = MYISAM;<br>mysql> insert into cn values("真的?",1);</p>
Copy after login

注:如果这些脚本用bin/mysql命令行是输入不了,可以用mysql query Browser或者编程执行。

shell> bin/mysql -u root --default-character-set=gbk -p<br>mysql>use test<br>mysql> select * from cn;<br>+--------+------+<br>| zh | 中文 |<br>+--------+------+<br>| 真的?| 1 |<br>+--------+------+<br>1 row in set (0.00 sec)<br>-----------------------------
Copy after login

最后修改启动文件/etc/rc.d/rc.mysqld,改路径/app/mysql/bin/mysqld_safe,改路径/app/mysql/var/web.pid最后/app/mysql/bin/mysqld_safe 后面加入 --user=root --default-character-set=gbk。

【相关文章】

  • 提高MySQL查询效率的三个技巧
  • MySQL占有率不断增加 日下载超五万次
  • 无法远程登入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