MySQL character set encoding related

Windows 10 Home Chinese Edition, MySQL 5.7.20, 2018-05-07

 

Part.1 Find the character set encoding of the database

View MySQL character set encoding: status command

Use the command line to log in to the MySQL server, and then enter the status command to view the character set encoding and other information of the server.

As you can see from the figure below, the character set encoding of my server (Server characterset) and database (Db characterset) is latin1.

 

The above figure finds four character set codes, what do they mean?

-Server characterset

The default character set encoding of the database server. When creating a database later, if no character set encoding is specified, the new database will use this character set encoding by default.

-Db characterset

When no database is currently selected (use database_name), the default character set encoding of the newly created database is displayed, which is the same as Server character;

database, the actual character set encoding of the database is displayed. The following figure shows the status information of a database whose character set encoding is utf8.

-Client characterset、Conn. characterset

The character set encoding of the client itself and the character set encoding used by the client and the server to establish a connection at this time.

 

You can also use the show variables command to view more character set encoding information: show variables like 'char%'

 

show variables can also view more database information, you can use the help show variables command to view its specific usage

 

Note, the show variables command can display all system variables of the database - all or search as needed, some of the variables in it can be placed in the startup of the database

Configuration file my.ini, thereby modifying the configuration information of the database. For more information and authoritative information, please refer to the official website link Server System Variables .

 

Part.2 Modify the database default character set encoding

Two questions:

Why modify the default character set encoding?

The default is latin1, which does not support Chinese, and the data I want to store includes Chinese, so I need to change it.

Of course, it's okay to not change it. In this case, you need to specify the character set encoding every time you create a new database, as follows:

create database if not exists test2 default charset utf8 collate utf8_general_ci;

 

What to modify the default character set encoding?

utf8 ( it's utf8, not utf-8, no dash )!

 

Two ways:

1. Modify in the command line client

set character_set_server=utf8;

Warning, this method will not work after a restart of the command line client.

Test: Create a database after modification, check the character set encoding of the newly created database, the expectation is utf8.

 

2. Modify in the database server configuration file my.ini

Add any of the following two lines to the my.ini file to set the default character set encoding to utf8 (existing under [mysqld]):

character_set_server = utf8
character-set-server = utf8

Is there any difference between the two lines? The first line is connected with an underscore, and the second line is connected with a dash, however, both are valid.

Again, it's utf8, not utf-8 (dash).

This method does not have the first problem, but, after configuration, the database server needs to be restarted to take effect .

 

Part.3 Appendix

1. There are too many contents displayed in show variables and show status (displaying the database server status) - one screen cannot be displayed, what should I do?

Output to file!

Methods as below:

mysql -uroot -p -e "show variables" > d:\mysql.txt

Press Enter, enter the root account password, and press Enter again to save the result to the mysql.txt file on the D drive (maybe it is better to save it to a csv file?).

Note that the show variables command is enclosed in double quotes , not single quotes .

2. You can use show character set; to view the character set supported by the MySQL database server

3. Character set encoding relationship of server, database and table

If it is not specified when creating the database, its character set encoding is the same as that of the server;

If it is not specified when creating a table in the database, its character set encoding is the same as the database where it is located.

However, the encoding of the server, database, and table can be modified through the command line (the character set encoding of each field can also be specified separately).

4. View the encoding of the table Table

Use the show create table... command as shown in the image below.

5. Legacy issues

What do [mysqld], [client], and [mysql] in the my.ini configuration file mean?

 

Reference link:

https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html

https://dev.mysql.com/doc/refman/8.0/en/charset-configuration.html

https://blog.csdn.net/hwhua1986/article/details/53431852

https://blog.csdn.net/enlyhua/article/details/79638776

https://www.cnblogs.com/jiangxiaobo/p/6110647.html

https://zhidao.baidu.com/question/539586616.html

https://www.2cto.com/database/201112/114418.html

https://www.cnblogs.com/candle806/archive/2013/01/14/2859721.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325629732&siteId=291194637