mysql字符集
1. 配置mysql默认字符集
2. 查看mysql当前使用字符集
使用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 ;
3. C api
[client]
default-character-set=utf8 # 客户端连接默认字符集
[mysqld]
default-character-set=utf8 # mysqld默认字符集
default-character-set=utf8 # 客户端连接默认字符集
[mysqld]
default-character-set=utf8 # mysqld默认字符集
2. 查看mysql当前使用字符集
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/local/mysql/share/mysql/charsets/ |
+--------------------------+----------------------------------------+
+--------------------------+----------------------------------------+
| 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/mysql/share/mysql/charsets/ |
+--------------------------+----------------------------------------+
使用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 ;
3. C api
mysql_options(mysql, MYSQL_SET_CHARSET_NAME, 'utf8');
mysql_real_connect(mysql, ...);
mysql_real_connect(mysql, ...);
-------------------------------------------
大部分情况下,使用mysql默认编码latin1可以解决mysql乱码问题。同时在插入数据和读取数据时,不需要通过mysql_options来指定编码类型。配置文件my.cnf也不需要指定编码类型。