MySQL設置字符集CHARACTER SET


本文地址: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

參考資料:

  14.1.4 Character Set

  5.1.4 Server System Variables

  10.5, “Character Set Configuration”

  

  10.1 Character Set Support

  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=system_character_set—that is, setting the client character set to match the system character set—should fix the problem.

 

 

查看設置結果

更改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/ | +--------------------------+---------------------------------------------------------------+
復制代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM