關於mysql中文字元編碼設定的解決辦法
1、修改資料庫字元編碼
mysql> alter database mydb character set utf8 ;
2、建立資料庫時,指定資料庫的字元編碼
mysql> create database mydb character set utf8 ;
3、檢視mysql資料庫的字元編碼
mysql> show variables like 'character%'; //查詢當前mysql資料庫的所有屬性的字元編碼
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
4、修改mysql資料庫的字元編碼
修改字元編碼必須要修改mysql的配置檔案my.cnf,然後重啟才能生效
通常需要修改my.cnf的如下幾個地方:
【client】下面,加上default-character-set=utf8,或者character_set_client=utf8
【mysqld】下面,加上character_set_server = utf8 ;
因為以上配置,mysql預設是latin1,如果僅僅是通過命令列客戶端,mysql重啟之後就不起作用了。
如下是客戶端命令列修改方式,不推薦使用
mysql> set character_set_client=utf8 ;
mysql> set character_set_connection=utf8 ;
mysql> set character_set_database=utf8 ;
mysql> set character_set_database=utf8 ;
mysql> set character_set_results=utf8 ;
mysql> set character_set_server=utf8 ;
mysql> set character_set_system=utf8 ;
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/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)