Mysql字符集的修改及查看問題 修改及查看mysql數據庫的字符集 修改my.cnf vi /etc/my.cnf 在[client]下添加 default-character-set=utf8 在[mysqld]下添加 default-character-set=utf8 5.查看字符集設置 mysql> show variables like 'collation_%'; mysql> show variables like 'character_set_%'; 修改數據庫的字符集 mysql>use mydb mysql>alter database mydb character set utf-8; 創建數據庫指定數據庫的字符集 mysql>create database mydb character set utf-8; 通過配置文件修改: 修改/var/lib/mysql/mydb/db.opt default-character-set=latin1 default-collation=latin1_swedish_ci 為 default-character-set=utf8 default-collation=utf8_general_ci 通過MySQL命令行修改: mysql> set character_set_client=utf8; mysql> set character_set_connection=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> set collation_connection=utf8; mysql> set collation_database=utf8; mysql> set collation_server=utf8; 查看: mysql> show variables like 'character_set_%'; mysql> show variables like 'collation_%'; 通常,查看系統的字符集和排序方式的設定可以通過下面的兩條命令: mysql> SHOW VARIABLES LIKE 'character%'; mysql> SHOW VARIABLES LIKE 'collation_%'; 3.修改默認字符集 (1) 最簡單的修改方法,就是修改mysql的my.ini文件中的字符集鍵值, 如 default-character-set = utf8 character_set_server = utf8 修改完後,重啟mysql的服務,service mysql restart 使用 mysql> SHOW VARIABLES LIKE 'character%';查看,發現數據庫編碼均已改成utf8 (2) 還有一種修改字符集的方法,就是使用mysql的命令 mysql> SET character_set_client = utf8 ; 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> 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) MySQL用Load Data local infile 導入部分數據後中文亂碼 今天在兩台MySQL服務器之間導數據,因為另一個MySQL服務器是測試用的,差一個月的數據,從現有MySQL服務器select到一個文件,具體語句是: select * from news where ine_time>='2010-02-01' and ine_time <'2010-03-01' into outfile '/tmp/newsdata.sql';然後scp到另一個MySQL Server上導入到對應的表中,具體語句如下: load data local infile '/home/lsanotes/newsdata.sql' into table news;然後刷新訪問這台數據庫的web頁面,發現剛導進來的這一個月的數據都是亂碼,而以前其它月份的則正常,用show create table news;查看發現兩個服務器中的news表都是utf8,奇怪,把導出的數據轉換成utf8,再導入問題仍舊。 後來在數據庫中查看剛剛導進的這一個月的數據時,沒有執行set names utf8;就可以正常查看中文而不亂碼,而其它月份的必須先執行set names utf8;才能看中文而不亂碼,但是當我執行過set names utf8;後再看剛剛導進的這一個月的數據卻是亂碼,看來導進來的數據並不是utf8格式。最後的解決方法是: load data local infile '/home/lsanotes/newsdata.sql' into table news character set utf8;