Rechercher dans le manuel MySQL
10.5 Configuring Application Character Set and Collation
For applications that store data using the default MySQL character
set and collation (utf8mb4
,
utf8mb4_0900_ai_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:
Specify character settings per database. For example, applications that use one database might use the default of
utf8mb4
, whereas applications that use another database might usesjis
.Specify character settings at server startup. This causes the server to use the given settings for all applications that do not make other arrangements.
Specify character settings at configuration time, if you build MySQL from source. This causes the server to use the given settings as the defaults for all applications, without having to specify them at server startup.
When different applications require different character settings, the per-database technique provides a good deal of flexibility. If most or all applications use the same character set, specifying character settings at server startup or configuration time may be most convenient.
For the per-database or server-startup techniques, the settings control the character set for data storage. Applications must also tell the server which character set to use for client/server communications, as described in the following instructions.
The examples shown here assume use of the
latin1
character set and
latin1_swedish_ci
collation in particular
contexts as an alternative to the defaults of
utf8mb4
and
utf8mb4_0900_ai_ci
.
Specify character settings per database. To create a database such that its tables will use a given default character set and collation for data storage, use a
CREATE DATABASE
statement like this:Tables created in the database will use
latin1
andlatin1_swedish_ci
by default for any character columns.Applications that use the database should also configure their connection to the server each time they connect. This can be done by executing a
SET NAMES 'latin1'
statement after connecting. The statement can be used regardless of connection method (the mysql client, PHP scripts, and so forth).In some cases, it may be possible to configure the connection to use the desired character set some other way. For example, to connect using mysql, you can specify the
--default-character-set=latin1
command-line option to achieve the same effect asSET NAMES 'latin1'
.For more information about configuring client connections, see Section 10.4, “Connection Character Sets and Collations”.
NoteIf you use
ALTER DATABASE
to change the database default character set or collation, existing stored routines in the database that use those defaults must be dropped and recreated so that they use the new defaults. (In a stored routine, variables with character data types use the database defaults if the character set or collation are not specified explicitly. See Section 13.1.17, “CREATE PROCEDURE and CREATE FUNCTION Syntax”.)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=latin1 collation-server=latin1_swedish_ci
These settings apply server-wide and apply as the defaults for databases created by any application, and for tables created in those databases.
It is still necessary for applications to configure their connection using
SET NAMES
or equivalent after they connect, as described previously. You might be tempted to start the server with the--init_connect="SET NAMES 'latin1'"
option to causeSET NAMES
to be executed automatically for each client that connects. However, this may yield inconsistent results because theinit_connect
value is not executed for users who have theCONNECTION_ADMIN
orSUPER
privilege.Specify character settings at MySQL configuration time. To select a character set and collation if you configure and build MySQL from source, use the
DEFAULT_CHARSET
andDEFAULT_COLLATION
CMake options:cmake . -DDEFAULT_CHARSET=latin1 \ -DDEFAULT_COLLATION=latin1_swedish_ci
The resulting server uses
latin1
andlatin1_swedish_ci
as the default for databases and tables and for client connections. It is unnecessary to use--character-set-server
and--collation-server
to specify those defaults at server startup. It is also unnecessary for applications to configure their connection usingSET NAMES
or equivalent after they connect to the server.
Regardless of how you configure the MySQL character set for
application use, you must also consider the environment within
which those applications execute. For example, if you will send
statements using UTF-8 text taken from a file that you create in
an editor, you should edit the file with the locale of your
environment set to UTF-8 so that the file encoding is correct and
so that the operating system handles it correctly. If you use the
mysql client from within a terminal window, the
window must be configured to use UTF-8 or characters may not
display properly. For a script that executes in a Web environment,
the script must handle character encoding properly for its
interaction with the MySQL server, and it must generate pages that
correctly indicate the encoding so that browsers know how to
display the content of the pages. For example, you can include
this <meta>
tag within your
<head>
element:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-charset-applications.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.