windows访问远程linux下mysql乱码问题
和windwos中的Mysql一樣,默認的設置都是latin1.而並非我們需要的gbk,或者big5.所以還是需要人爲的設置:
無論是採用telnet還是直接到Linux中進入mysql中:找到/etc/目錄下的my.cnf文件:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
[mysql.server]
user=mysql
basedir=/var/lib
[mysqld_safe]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
以上默認的文件内容,現在把它修改為如下:
[mysqld]
default-character-set=utf8
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
[client]
default-character-set=gbk
[mysql.server]
user=mysql
basedir=/var/lib
default-character-set=utf8
[mysqld_safe]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
紅色部分為增加的部分,其餘保持不變。
然後重新啓動Mysql服務器
執行命令:
set names 'gbk';
show variables like "character_set_%";
show variables like "collation_%";
看到的結果應該是:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8 |
| character_set_results | gbk |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
7 rows in set (0.00 sec)
mysql> show variables like "collation_%";
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | gbk_chinese_ci |
| collation_database | utf8_general_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
這樣儅windows程序訪問數據庫的時候就應該是會顯示正確了。如果還不行的話,可能看看在建立數據庫的時候有沒有制定了編碼:
比如:
CREATE DATABASE sample
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci ;