Home >Database >Mysql Tutorial >mysql utf8 settings
When using the MySQL database, in many cases we need to set the character encoding to UTF8 to support more language characters. UTF8 is an encoding method of Unicode that can represent most known characters. In MySQL, UTF8 requires several steps to set up, which will be introduced one by one below.
Before using MySQL, we need to first understand the current MySQL character set. You can view it through the following command:
SHOW VARIABLES LIKE 'character_set%';
This command will list the current character set settings of MySQL. The more important ones are the settings of the three parameters character_set_client, character_set_connection and character_set_database. If these three parameters are all utf8, it means that MySQL is currently encoded in UTF8.
If the current MySQL character set is not UTF8, we need to manually modify the MySQL configuration file to enable UTF8 encoding. The modification steps are as follows:
sudo vi /etc/my.cnf
[mysqld] collation-server = utf8_unicode_ci init-connect='SET NAMES utf8' character-set-server = utf8
Among them, collation-server and character-set-server They are used to set the collation rules and character set of MySQL respectively. init-connect is used to execute the SET NAMES utf8 statement every time a new connection is made to MySQL to ensure that the character set is correct.
After modifying the MySQL configuration file, we need to restart the MySQL service to take effect. You can use the following command to restart MySQL:
sudo service mysql restart
After the character set of MySQL is set to UTF8, we also need to modify the database separately. Only the character set of the table can truly support UTF8 encoding.
You can use the following command to modify the character set of the database:
ALTER DATABASE database_name CHARACTER SET utf8 COLLATE utf8_general_ci;
Among them, database_name is the name of the database you need to modify. , utf8_general_ci is the only collation that supports UTF8.
You can use the following command to modify the character set of the table:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
Among them, table_name is the name of the table you need to modify , utf8_general_ci is the only collation that supports UTF8.
Summary
Through the above steps, we can set the character set of MySQL to UTF8 to support more language characters. It should be noted that during the development process, we also need to pay attention to whether the correct character set is used for data input and output to ensure the correctness of the data.
The above is the detailed content of mysql utf8 settings. For more information, please follow other related articles on the PHP Chinese website!