First of all, there are two main concepts about MySQL character set, one is character sets, the other is collations, the former is character content
and encoding, and the latter is some rules for comparing the former. These two parameter sets can be specified at four levels: database instance, single database, table and column.
For users, utf8 encoding is generally recommended to store data. To solve the problem of garbled code, it is not only the storage of MySQL data, but also related to the coding mode of user program files and the connection mode between user program and MySQL database.
first of all, MySQL has a default character set, which is determined during installation. When compiling mysql, you can use default_ Charset =
utf8 and default_ COLLATION=utf8_ general_ CI (MySQL version 5.5, version 5.1 uses — with charset =
utf8 — with collation = utf8)_ general_ The default character set is utf8, which is also the most once and for all method. After this is specified,
the coding mode of client connecting to database is utf8 by default, and the application does not need any processing.
unfortunately, many people don’t specify these two parameters when compiling and installing mysql, and most people install it through binary programs.
at this time, the default character set of MySQL is Latin1. At this time, we can still specify the default character set of MySQL by my.cnf Add two parameters to the file:
default character set = utf8 under [mysqld]
default character set = utf8 under [Client]
2 In this way, we don’t need to specify utf8 character set when we build database and table. This writing method in the configuration file solves the problem of data storage and comparison
, but it has no effect on the connection of the client. At this time, the client usually needs to specify utf8 connection to avoid garbled code. This is the general set
Names command. In fact, the set names utf8 command corresponds to the following server-side commands:
set character_ set_ client = utf8;
SET character_ set_ results = utf8;
SET character_ set_ Connection = xutf8;
but these three parameters cannot be written in the configuration file my.cnf It’s in the library. It can only be modified dynamically by the set command. What we need is to write a way to
Yongyi in the configuration file. At this time, is there a way to solve the problem on the server?The feasible idea is in init_ Set in connect. This command will be triggered every time an ordinary user connects. You can add the following line in the [mysqld] section to set the connection character set:
Add under [mysqld]:
init_ Connect =’set names utf8 ‘
1. It is preferred to specify two parameters to use utf8 encoding when compiling and installing mysql.
2. Select in the configuration file my.cnf or my.ini Set two parameters and init at the same time_ Connect parameter.
3. The third is in the configuration file my.cnf or my.ini Set two parameters, and specify the set names command for the client connection.
4. In the configuration file my.cnf The default character set parameter is added to the client and server in to facilitate management.
- Spoop export hive to MySQL
- Solution of MySQL data garbled problem
- Error 2005 (HY000) unkonw MySQL server host…
- node.js Server MySQL database connection timeout (error: connect etimeout)
- The meaning of escape character is’ ^] ‘prompted by telnet under linux / Unix
- Solution to MySQL Error 1130 problem
- ERROR 1148 (42000): The used command is not allowed with this MySQL version
- Can’t connect to MySQL server on ‘192.168.64.132‘
- After installing MySQL again under Linux, the solution of ‘MySQL module failed to start’ appears
- 2059 error in connecting to database by Navicat premium
- Windows FTP Error 425: Unable to build data connection
- Perfectly solve the 1366 error of saving Chinese in MySQL
- The problem of [connection lost contact] after C # code connecting Oracle database for a period of time
- mysql configuration supports SSL connection
- Putty remote login VMware virtual machine Linux
- Analysis of JDBC connection to MySQL
- [error handling] when logging into MySQL with CentOS command, an error 1045 (28000) is reported
- Error 1364 (HY000): field ‘SSL_ cipher’ doesn’t have a default value
- Connection between PHP 7.4 and MySQL (MariaDB) under Ubuntu (kali Linux)
- Call to undefined function mysql_ Connect() solution summary