04-客户端字符集选项
--default-character-set=charset-name
细心的读者可能会发现,作为服务器的字符集选项,这个选项也可以配置在 my.cnf 的[mysqld]组中。同样,作为客户端字符集选项,也可以配置在my.cnf的[mysql]组中,这样每次用mysql工具连接数据库的时候就会自动使用此客户端字符集。当然,也可以在mysql的命令行中手工指定客户端字符集,如下所示:
shell>mysql –u user --default-character-set=charset
相当于在mysql客户端连接成功后执行:
set names charset;
下例描述了此选项使用前后客户端字符集的变化。
(1)正常连接到MySQL服务后的客户端字符集(粗体显示):
[zzx@localhost~]$ mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.0.41-community-log MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show variables like 'chara%';
+--------------------------+----------------------------+
| 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/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
(2)加上参数重新连接后再次观察客户端字符集(粗体显示):
[zzx@localhost~]$ mysql -uroot --default-character-set=utf8
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.0.41-community-log MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show variables like 'chara%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | gbk |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
果然,系统的客户端字符集按照之前的设置发生了变化。