Linux下修改MySQL編碼的方法影響項目亂碼問題 默認登錄mysql之後可以通過SHOW VARIABLES語句查看系統變量及其值。 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/ | -------------------------- ---------------------------- 說明:以下是在CentOS-6.2下的設置 (不同的版本可能有些差異,比如文件的位置。但設置的內容應該是一樣的) 1. 找到mysql的配置文件,拷貝到etc目錄下,第一步很重要 把/usr/share/doc/mysql-server-5.1.52/my-large.cnf 復制到 /etc/my.cnf 即用命令:cp /usr/share/doc/mysql-server-5.1.52/my-large.cnf /etc/my.cnf 2. 打開my.cnf修改編碼 在[client]下增加default-character-set=utf8 在[mysqld]下增加default-character-set=utf8 同時加上init_connect='SET NAMES utf8' (設定連接mysql數據庫時使用utf8編碼,以讓mysql數據庫為utf8運行) 3.重新啟動mysql service mysqld restart 再次輸入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;