Home > Database > Mysql Tutorial > How to change the character encoding in mysql?

How to change the character encoding in mysql?

藏色散人
Release: 2020-09-15 17:46:02
Original
7190 people have browsed it

How to change the character encoding in mysql?

mysql command line to modify the character encoding

1. Modify the database character encoding

mysql> alter database mydb character set utf8 ;
Copy after login

2. When creating the database, specify Database character encoding

mysql> create database mydb character set utf8 ;
Copy after login

3. Check the character encoding of mysql database

mysql> show variables like 'character%'; //查询当前mysql数据库的所有属性的字符编码
Copy after login
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
Copy after login

4. Modify the character encoding of mysql database

To modify the character encoding, you must modify the mysql configuration file my.cnf, and then restart to take effect

Usually you need to modify the following places of my.cnf:

Under [client], add default-character-set=utf8, or character_set_client= utf8

[mysqld] Below, add character_set_server = utf8;

Because of the above configuration, mysql defaults to latin1. If it is only through the command line client, mysql will not work after restarting .

The following is the client command line modification method. It is not recommended to use

mysql> set character_set_client=utf8 ;
mysql> set character_set_connection=utf8 ;
mysql> set character_set_database=utf8 ;
mysql> set character_set_database=utf8 ;
mysql> set character_set_results=utf8 ;
mysql> set character_set_server=utf8 ;
mysql> set character_set_system=utf8 ;
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
Copy after login

Related learning recommendations: mysql tutorial(Video)

The above is the detailed content of How to change the character encoding in mysql?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template