my.cnf
[mysql]
default-character-set = gbk
[mysqld]
skip-character-set-client-handshake=1 跳过mysql程序起动时的字符参数设置 ,使用服务器端字符集设置
修改不过来
[root@localhost ~]# mysql -uroot -p --default-character-set=latin1
Enter password:
mysql> show variables like "%char%";
+--------------------------+------------------------------------+
| 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/local/mysql56/share/charsets/ |
+--------------------------+------------------------------------+
8 rows in set (0.01 sec)
mysql> set names latin1 ; //进入环境中才可以设置
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like "%char%";
+--------------------------+------------------------------------+
| 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/local/mysql56/share/charsets/ |
+--------------------------+------------------------------------+
8 rows in set (0.01 sec)
[my.cnf]
[mysql]
default-character-set = gbk
[mysqld]
character-set-client-handshake = 0
[root@localhost ~]# mysql -uroot -p --default-character-set=latin1
Enter password:
mysql> show variables like "%char%";
+--------------------------+------------------------------------+
| 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/local/mysql56/share/charsets/ |
+--------------------------+------------------------------------+
8 rows in set (0.01 sec)
mysql> set names gbk;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like "%char%";
+--------------------------+------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql56/share/charsets/ |
+--------------------------+------------------------------------+
8 rows in set (0.18 sec)
[mysqld]
character-set-server = gbk
skip-character-set-client-handshake=1 //使用服务端字符集
[root@localhost ~]# mysql -uroot -p --default-character-set=latin1
Enter password:
mysql> show variables like "%char%";
+--------------------------+------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | gbk |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql56/share/charsets/ |
+--------------------------+------------------------------------+
8 rows in set (0.02 sec)
[mysqld]
character-set-server = gbk
character-set-client-handshake=0 //使用服务端字符集
[root@localhost ~]# mysql -uroot -p --default-character-set=latin1
Enter password:
mysql> show variables like "%char%";
+--------------------------+------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | gbk |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql56/share/charsets/ |
+--------------------------+------------------------------------+
8 rows in set (0.02 sec)
[mysqld]
character-set-server = gbk
character-set-client-handshake = 1
[root@localhost ~]# mysql -uroot -p --default-character-set=latin1
Enter password:
mysql> show variables like "%char%";
+--------------------------+------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | gbk |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql56/share/charsets/ |
+--------------------------+------------------------------------+
8 rows in set (0.02 sec)
mysql 字符集非交互设置:当mysql起动连到mysqld时,非手动命令
mysql字符集交互设置:通过命令行手动设置
MySQL 5.5版本解决中文乱码问题时my.ini内
[mysqld]项中不能再写default-character-set=utf8
来看看如何解决乱码问题:
在mysql中默认字符集是latin1,
想要设置字符集为uft-8,可以在 my.cnf 文件中添加以下设置:
·[client]
1 |
default-character- set =utf8
|
·[mysqld]
1 |
default-character- set =utf8
|
2 |
skip-character- set -client-handshake #此处是忽略客户端的字符集,使用服务器的设置
|
(skip语句和client中的default选一个)
当然你可以在建立数据库是规定字符集:
1 |
create database db_name default character set utf8 callate utf8-general-ci;
|
也可以在链接数据库前设置:
重点在红字上,加了skip-character-set-client-handshake,client里面就不用加default了
01 |
show variables like 'characte%' ;
|
02 |
+ --------------------------+----------------------------+
|
03 |
| Variable_name | Value | |
04 |
+ --------------------------+----------------------------+
|
05 |
| character_set_client | utf8 | |
06 |
| character_set_connection | utf8 | |
07 |
| character_set_database | utf8 | |
08 |
| character_set_filesystem | binary |
|
09 |
| character_set_results | utf8 | |
10 |
| character_set_server | utf8 | |
11 |
| character_set_system | utf8 | |
12 |
| character_sets_dir | /usr/share/mysql/charsets/ | |
13 |
+ --------------------------+----------------------------+
|
但是根据以上的配置过了后,mysql就无法启动了,这是为什么呢?往下看:
在5.1版本时,为了解决中文乱码问题设置默认字符集为utf8时,在my.ini内的 [mysql] 和 [mysqld] 项中都是写:
1 |
default-character- set =utf8
|
到了5.5版本, [mysql] 项内可以这么写, [mysqld] 项内这么写在启动MySQL服务时会有1067错误,经查询发现这里必须要这样写:
1 |
character- set -server=utf8
|
目前已知5.1和5.5有这么一个不同之处,且是从5.5的安装版本自动生成的my.ini文件中看出的。配置免安装的5.5版本还需要进一步的实验。