mysql modify encoding

The default encoding of MySQL is Latin1 and does not support Chinese. To support noon, you need to change the default encoding of the database to gbk or utf8.

1. You need to log in as the root user to view the database encoding (the command to log in as the root user is: >mysql -u root -p, then enter the root user's password twice), and the command to view the database encoding is:

1

2

3

4

5

6

7

8

9

10

11

12

13

>show variables like 'character%';

+--------------------------+----------------------------+

| Variable_name | Value |

+--------------------------+----------------------------+

| character_set_client | latin1 |

| character_set_connection | latin1 |

| character_set_database | latin1 |

| character_set_filesystem | binary |

| character_set_results | latin1 |

| character_set_server | latin1 |

| character_set_system | utf8 |

| character_sets_dir | /usr/share/mysql/charsets/ |

+--------------------------+----------------------------+

From the above information, we can see that the code of the database is latin1, which needs to be modified to gbk or utf8;

Among them, character_set_client is the client encoding method;

character_set_connection is the encoding used to establish a connection;

Character_set_database database encoding;

Character_set_results result set encoding;

character_set_server database server encoding;

As long as the encoding methods used in the above four are the same, there will be no garbled problems.

Another command to view the database encoding:

>show variables like'collation%';
 
2. Under linux system, the steps to modify the default encoding of MySQL database are:

ü Stop MySQL operation

/etc/init.d/mysql start (stop) is to start and stop the server

ü The main MySQL configuration file is my.cnf, and the general directory is /etc/mysql

var/lib/mysql/ places the database table folder, where mysql is equivalent to the date folder of mysql under windows

ü When we need to modify the default encoding of the MySQL database, we need to edit the my.cnf file to modify the encoding, modify the mysql configuration file my.cnf under linux, the file location defaults to the /etc/my.cnf file

Find the client configuration [client] and add it below

default-character-set=utf8 The default character set is utf8

Find [mysqld] add

default-character-set=utf8 The default character set is utf8

init_connect='SET NAMES utf8' (set to use utf8 encoding when connecting to the mysql database, so that the mysql database runs as utf8)

After the modification is complete, restart mysql, and re-query the database encoding to find the change in encoding:

1

2

3

4

5

6

7

8

9

10

11

12

13

>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/ |

+--------------------------+----------------------------+

This method is also valid for the standard mysql version. For the /etc/my.cnf file, you need to copy a copy from the mysql/support-files folder cp my-large.cnf to /etc/my.cnf.
 
3. It can be used under the windows system. Delete the MySQL database and reinstall it. You can directly use the Mysql Server Instance Config Wizard to set up during the installation process.
 
4. When the MySQL database server has data that is not suitable for deletion and reinstallation, you can individually specify the encoding of the database. The way MySQL specifies encoding is very flexible and diversified. You can specify table-level encoding, row-level encoding, and even field-level encoding.

The following examples show two ways to specify encoding when creating a database:

1)CREATE  DATABASE  ms_db  CHARACTER SET  utf8  COLLATE utf8_general_ci;

2)create  database  if  not  exists  netctoss  default character set utf8;

5. If you use external access, you can determine the encoding format of the request in the connection, such as: jdbc:mysql://localhost:3306 /mysql?useUnicode=true&characterEncoding=utf-8 (Note: Do not show any spaces , Otherwise an error)

6. Execute the script: specify the encoding format set names gbk (note, not UTF-8) can be modified

Before execution:

After execution:

从执行命令前后可知,set names gbk只可以修改character_set_client、character_set_connection、 character_set_results的编码方式,并且这种修改是窗口级别的,只针对本窗口有效,打开另外一个窗口修改无效。也可发现数据库底层的编码方式没有改变,插入数据后还是以utf8编码方式保持。

以上就是小编为大家带来的MySQL查看和修改字符编码的实现方法全部内容了,希望大家多多支持脚本之家~

Guess you like

Origin blog.csdn.net/yucaifu1989/article/details/110859570