讓MYSQL徹底支援中文(slack10.1版)(轉)

amyz發表於2007-08-10
讓MYSQL徹底支援中文(slack10.1版)(轉)[@more@]

  提要:系統自帶的mysql預設字符集不是gbk,因此給資料庫的推廣應用以及中文程式的開發帶來極大的不便,在沒完沒了的GBK和UTF8的轉換過程中消耗了我們無數的精力以及系統的資源.經過摸索嘗試解決了中文支援問題,經驗與大家分享一下.

  先將讓slack支援中文,方法可以看"linuxsir.org 之Slackware討論區",然後重新編譯mysql,原來系統mysql的要先用解除安裝.

  login:root

  shell> removepkg mysql

shell> groupadd mysql

shell> cd /app

shell> mkdir mysql

shell> chmod 744 mysql

shell> useradd -G mysql -s /bin/bash –p /app/mysql mysql

shell> chown -R mysql.mysql mysql

  下載mysql4.x.x

  shell> gunzip < mysql-VERSION.tar.gz | tar -xvf -

shell> cd mysql-VERSION

shell> CFLAGS="-O3 -mcpu=pentium3"

CXX=gcc

XXFLAGS="-O3 -mcpu=pentium3 -felide-constructors"

./configure --prefix=/app/mysql

--with--charset=gbk

--with-extra-charsets="gbk gb2312 big5 utf8"

--with-unix-socket-path=/etc/mysql.sock

--enable-local-infile

--enable-thread-safe-client

--enable-assembler

--with-client-ldflags=-all-static

--with-mysqld-ldflags=-all-static

  //-mcpu 根據CPU型別最佳化編譯,可以讓你的mysq表現更好!可選專案很多: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

  再回上一步

  ------------------------------------------------------------

  shell> make install

shell> cp support-files/my-medium.cnf /etc/my.cnf

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 --default-character-set=gbk &

shell> ./bin/mysqladmin -u root password 'new-password'

shell> ./bin/mysql -u root --default-character-set=gbk -p

mysql> status

...

Server characterset: gbk

Db characterset: gbk

Client characterset: gbk

Conn. characterset: gbk

....

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

  ----------------------------------------------------------------------------------------------------

  在UTF8字符集的老資料庫中,建立中文支援表指令碼如下,對欄位加上字符集說明

  CREATE TABLE `test`.`cn` (

`zh` CHAR(10) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NOT NULL,

`中文` INT(11) NOT NULL

)

ENGINE = MYISAM;

  注:CHARACTER SET 寫GB2312 和gbk都可以,gbk優於gb2312.

  對於新建立的資料庫加個預設字符集宣告GBK,就不用建表時候每個欄位宣告瞭,這樣看起來簡單流暢,沒有特殊的痕跡,建議多采用這種方法.

  mysql> CREATE DATABASE dbCN DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

CREATE TABLE `dbCN`.`cn` (

`zh` CHAR(10) NOT NULL,

`中文` INT(11) NOT NULL

)

ENGINE = MYISAM;

mysql> insert into cn values("真的?",1);

  注:如果這些指令碼用bin/mysql命令列是輸入不了,可以 用mysql query Browser,或者程式設計執行.

  --------------------------------------------------------------------------------------------------------

  shell> bin/mysql -u root --default-character-set=gbk -p

mysql>use test

mysql> select * from cn;

+--------+------+

| zh | 中文 |

+--------+------+

| 真的?| 1 |

+--------+------+

1 row in set (0.00 sec)

---------------------------------------------

  最後修改啟動檔案/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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-941394/,如需轉載,請註明出處,否則將追究法律責任。

相關文章