默認登錄mysql之後show variables like ‘%character%’, 出現
mysql》 show variables like ‘%character%’;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
1. 找到mysql的配置文件,拷貝到etc目錄下,第一步很重要
使用find命令查找cnf文件。記得不要去找my.cnf因為它不一定存在,而應該找*.cnf。
[root@linuxserver classes] find / -iname *.cnf -print
/usr/share/doc/mysql-server-4.1.12/my-medium.cnf
/usr/share/doc/mysql-server-4.1.12/my-huge.cnf
/usr/share/doc/mysql-server-4.1.12/my-innodb-heavy-4G.cnf
/usr/share/doc/mysql-server-4.1.12/my-large.cnf
/usr/share/doc/mysql-server-4.1.12/my-small.cnf
/usr/share/texmf/web2c/mktex.cnf
/usr/share/texmf/web2c/texmf.cnf
/usr/share/texmf/web2c/fmtutil.cnf
/usr/share/texmf/tex/jadetex/jadefmtutil.cnf
/usr/share/ssl/openssl.cnf
找到之後,復制一個出來
cp /usr/share/doc/mysql-server-4.1.12/my-large.cnf /etc/my.cnf
2. 打開my.cnf修改編碼
vi /etc/my.cnf
在[client]下增加default-character-set=utf8
在[mysqld]下增加default-character-set=utf8
同時加上init_connect=‘SET NAMES utf8’ (設定連接mysql數據庫時使用utf8編碼,以讓mysql數據庫為utf8運行)
重新啟動mysql,執行:sudo /etc/init.d/mysql stop 然後sudo /etc/init.d/mysql start
mysql》 show variables like ‘%character%’;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
即使做了以上修改如果直接數據庫再創建表,然後存入中文,取出來的會是問號。解決的辦法是:
創建數據庫的時候指明默認字符集為utf8,如:
create database test default character set utf8;