安裝完的MySQL的默認字符集為 latin1 ,為了要將其字符集改為用戶所需要的(比如utf8),就必須改其相關的配置文件;由於linux下MySQL的默認安裝目錄分布在不同的文件下;不像windows一樣放在同一目錄下,只需修改其中的my.ini文件,重起後就生效了;所以先來看看linux下MySQL的數據庫文件、配置文件和命令文件分別在不同的目錄 :
1、數據庫目錄,其所創建的數據庫文件都在該目錄下
/var/lib/mysql/
2、配置文件 (mysql.server命令及配置文件所在地)
/usr/share/mysql
3、相關命令(如mysql mysqladmin等)
/usr/bin
4、啟動腳本(如mysql啟動命令)
/etc/rc.d/init.d/
查看默認字符集
#mysql -u root - p
#(輸入密碼)
mysql> show variables like 'character_set%';
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
修改字符集
修改 /etc/my.cnf文件
#/etc/my.cnf [client] default-character-set=utf8 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 #default-character-set=utf8 character-set-server=utf8 init_connect='SET NAMES utf8' [mysql] no-auto-rehash default-character-set=utf8 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid