Debian Etch Linux上成功安裝了MySQL 5.0.27 icc版本,并使用JDBC測(cè)試中文成功!
中文問題的關(guān)鍵是全部使用UTF-8編碼,MySQL 5發(fā)布版支持UTF-8,但默認(rèn)為latin1,Java在內(nèi)部使用的全部是Unicode,因此要保證JSP頁面以UTF-8編碼,JDBC驅(qū)動(dòng)采用官方Connector4J 5.0.4,在Resin 3.1/Spring 2.0/Hibernate 3.2環(huán)境下測(cè)試中文正常。
1. 下載"mysql-standard-5.0.27-linux-i686-icc-glibc23.tar.gz",推薦ICC版本,據(jù)稱比GCC性能提高10-20%
2. 復(fù)制到/usr/local/,解壓:tar zxvf mysql-standard-5.x....tar.gz
3. 添加用戶和組mysql:
groupadd mysql useradd -g mysql mysql |
4. 創(chuàng)建符號(hào)連接:/usr/local # ln -s mysql-standard-5.x... mysql
5. cd mysql,當(dāng)前目錄改為/usr/local/mysql/
6. 運(yùn)行腳步初始化數(shù)據(jù)庫:./scripts/mysql_install_db --user=mysql
7. 設(shè)置權(quán)限:
/usr/local/mysql # chown -R root . /usr/local/mysql # chown -R mysql data /usr/local/mysql # chgrp -R mysql |
8. 根據(jù)需要?jiǎng)?chuàng)建并修改/etc/my.cnf,參考配置:
[mysqld]
# 設(shè)置默認(rèn)為INNODB表,支持事務(wù):
default-storage-engine=INNODB
# 設(shè)置默認(rèn)的字符集UTF-8:
character-set-server=utf8 |
skip-bdb
9. 啟動(dòng)MySQL:
/usr/local/mysql/bin # ./mysqld_safe --user=mysql &
10. 初始化root口令:
/usr/local/mysql/bin # ./mysqladmin -u root -p password "password-of-root" Enter password: <輸入舊口令,直接按Enter> |
/usr/local/mysql # ./mysql -u root -p Enter password: password-of-root |
mysql> create user test identified by 'test-password'; |
mysql> create database testdb default character set utf8 default collate utf8_general_ci; |
14. 賦予test用戶從localhost訪問testdb的權(quán)限,并使用口令:
mysql> grant all on testdb.* to test@localhost identified by 'test-password'; |
15. 停止MySQL服務(wù)器:
/usr/local/mysql/bin # ./mysqladmin -u root -p shutdown Enter password: password-of-root STOPPING server from pid file /usr/local/mysql/data/debian.pid xxx mysqld ended |
/etc/my.cnf中所有與編碼相關(guān)的設(shè)置均要設(shè)定為UTF-8,參考步驟8。
按以下步驟調(diào)試:
mysql> status;
注意characterset相關(guān)值,必須保證全部為utf8,否則,修改/etc/my.cnf,參考步驟8。
mysql> show variables like '%char%';
除了character_set_filesystem顯示為binary外,其余應(yīng)該全部顯示為utf8,若非utf8,修改/etc/my.cnf,參考步驟8。
mysql> show variables like '%collation%';
顯示應(yīng)該全部為utf8_general_ci,否則,修改/etc/my.cnf,參考步驟8。
mysqlshow -u root -p -i database-name
作用:顯示數(shù)據(jù)庫詳細(xì)信息,如果你沒有看到utf8_general_ci,而是latin_swedish_ci,說明數(shù)據(jù)庫編碼非UTF-8,中文肯定不正常,刪之,然后參考步驟13以UTF-8重新創(chuàng)建數(shù)據(jù)庫。