本文地址:http://www.cnblogs.com/yhLinux/p/4036506.html
在 my.cnf 配置文件中设置相关选项,改变为相应的character set。
设置数据库编码(sudo vi /etc/mysql/my.cnf):
[client] default-character-set = utf8 [mysqld] character-set-server = utf8 collation-server = utf8_general_ci
参考资料:
10.5, “Character Set Configuration”
You can specify character sets at the server, database, table, and column level.
10.1.3 Specifying Character Sets and Collations
10.1.3.1 Server Character Set and Collation
MySQL Server has a server character set and a server collation. These can be set at server startup on the command line or in an option file and changed at runtime.
10.1.3.2 Database Character Set and Collation
Every database has a database character set and a database collation.
The character set and collation for the default database can be determined from the values of the character_set_database
and collation_database
system variables. The server sets these variables whenever the default database changes. If there is no default database, the variables have the same value as the corresponding server-level system variables, character_set_server
and collation_server
.
Specify character settings at server startup. To select a character set and collation at server startup, use the --character-set-server
and --collation-server
options. For example, to specify the options in an option file, include these lines:
[mysqld] character-set-server=utf8 collation-server=utf8_general_ci
These settings apply server-wide and apply as the defaults for databases created by any application, and for tables created in those databases.
You can force client programs to use specific character set as follows:
[client]
default-character-set=charset_name
This is normally unnecessary. However, when character_set_system
differs from character_set_server
or character_set_client
, and you input characters manually (as database object identifiers, column values, or both), these may be displayed incorrectly in output from the client or the output itself may be formatted incorrectly. In such cases, starting the mysql client with --default-character-set=
—that is, setting the client character set to match the system character set—should fix the problem.system_character_set
查看设置结果
更改my.cnf之前:
mysql> SHOW VARIABLES LIKE ‘%char%‘; +--------------------------+---------------------------------------------------------------+ | Variable_name | Value | +--------------------------+---------------------------------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql-5.6.21-linux-glibc2.5-x86_64/share/charsets/ | +--------------------------+---------------------------------------------------------------+
设置之后,重启mysql服务($ sudo service mysql restart):
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/mysql-5.6.21-linux-glibc2.5-x86_64/share/charsets/ | +--------------------------+---------------------------------------------------------------+