Database Reference
In-Depth Information
mysql>
SET NAMES 'utf8' COLLATE 'utf8_general_ci';
• If your client program supports the
--default-character-set
option, you can use
it to specify the character set at program invocation time.
mysql
is one such program.
Put the option in an option file so that it takes effect each time you connect to the
server:
[mysql]
default-character-set=utf8
• If you set the environment for your working environment using the
LANG
or
LC_ALL
environment variable on Unix, or the code page setting on Windows, MySQL client
programs automatically detect which character set to use. For example, setting
LC_ALL
to
en_US.UTF-8
causes programs such as
mysql
to use
utf8
.
• Some programming interfaces provide their own method of setting the character
set. For example, MySQL Connector/J for Java clients detects the character set used
on the server side automatically when you connect, but you can specify a different
set explicitly using the
characterEncoding
property in the connection URL. The
property value should be the Java-style character-set name. To select
utf8
, you
might use a connection URL like this:
jdbc:mysql://localhost/cookbook?characterEncoding=UTF-8
This is preferable to
SET
NAMES
because Connector/J performs character-set con‐
version on behalf of the application, but is unaware of which character set applies
if you use
SET
NAMES
. Similar principles apply to programs written for other APIs.
For PDO, use a
charset
option in your data source name (DSN) string (this works
in PHP 5.3.6 or later):
$dsn
=
"mysql:host=localhost;dbname=cookbook;charset=utf8"
;
For Connector/Python, specify a
charset
connection parameter:
conn_params
=
{
"database"
:
"cookbook"
,
"host"
:
"localhost"
,
"user"
:
"cbuser"
,
"password"
:
"cbpass"
,
"charset"
:
"utf8"
,
}
Some APIs may also provide a parameter to specify the collation.
Some character sets cannot be used as the connection character set:
ucs2
,
utf16
,
utf16le
,
utf32
.