Configuring the Character Set and Collation for Applications
778
The MySQL client programs
mysql
,
mysqladmin
,
mysqlcheck
,
mysqlimport
, and
mysqlshow
determine the default character set to use as follows:
• In the absence of other information, the programs use the compiled-in default character set, usually
latin1
.
• The programs support a
--default-character-set
[266]
option, which enables users to specify
the character set explicitly to override whatever default the client otherwise determines.
When a client connects to the server, it sends the name of the character set that it wants to use. The
server uses the name to set the
character_set_client
[441]
,
character_set_results
[443]
,
and
character_set_connection
[442]
system variables. In effect, the server performs a
SET
NAMES
operation using the character set name.
With the
mysql
client, to use a character set different from the default, you could explicitly execute
SET NAMES
every time you start up. To accomplish the same result more easily, add the
--default-
character-set
[266]
option setting to your
mysql
command line or in your option file. For example,
the following option file setting changes the three connection-related character set variables set to
koi8r
each time you invoke
mysql
:
[mysql]
default-character-set=koi8r
If you are using the
mysql
client with auto-reconnect enabled (which is not recommended), it is
preferable to use the
charset
command rather than
SET NAMES
. For example:
mysql>
charset utf8
Charset changed
The
charset
command issues a
SET NAMES
statement, and also changes the default character set
that
mysql
uses when it reconnects after the connection has dropped.
Example: Suppose that
column1
is defined as
CHAR(5) CHARACTER SET latin2
. If you do not say
SET NAMES
or
SET CHARACTER SET
, then for
SELECT column1 FROM t
, the server sends back
all the values for
column1
using the character set that the client specified when it connected. On the
other hand, if you say
SET NAMES 'latin1'
or
SET CHARACTER SET latin1
before issuing the
SELECT
statement, the server converts the
latin2
values to
latin1
just before sending results back.
Conversion may be lossy if there are characters that are not in both character sets.
If you want the server to perform no conversion of result sets or error messages, set
character_set_results
[443]
to
NULL
or
binary
:
SET character_set_results = NULL;
To see the values of the character set and collation system variables that apply to your connection, use
these statements:
SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';
You must also consider the environment within which your MySQL applications execute. See
Section 10.1.5, “Configuring the Character Set and Collation for Applications”
.
For more information about character sets and error messages, see
Section 10.1.6, “Character Set for
Error Messages”
.
10.1.5. Configuring the Character Set and Collation for Applications
For applications that store data using the default MySQL character set and collation (
latin1
,
latin1_swedish_ci
), no special configuration should be needed. If applications require data storage
using a different character set or collation, you can configure character set information several ways:
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...