Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Set the default character set for mysql

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Mysql > show global variables like'% charac%';-- View the character set currently used in mysql

Mysql > show global variables like'% server%';-- View the current server global character set

+-+ +

| | Variable_name | Value |

+-+ +

| | character_set_server | latin1 |

| | collation_server | latin1_swedish_ci |

+-+ +

2 rows in set (0.00 sec)

How to change it to the familiar UTF8 character set? Two categories are listed below:

To modify the my.cnf file, you need to restart the service, which is valid for a long time.

Add two lines under server:

Character_set_server=utf8

Collation_server=utf8_general_ci

two。 Modify the mysql variable without restarting. It takes effect when a new session is established, but expires after restart.

Mysql > set global character_set_server=utf8;-changes the current character set to utf8, which is valid only for the current session without global.

3. Specify the character set to be used on the client

Mysql > set character-set_results=gbk;-- set on the client

4. Set the character set used by the current session:

Mysql > set names gbk;-sets the current session to use the gbk character set.

5.mysql > set character set gbk;-- sets the variable of the current system to GBK

Mysql > set charset gbk;-- same as the previous sentence

-

Mysql > show create table table name;-- View the character set used when creating the table

Mysql > alter database database name character set utf8;-- change the default character set utf8 of the database. After updating, the new table will adopt the new character set.

Mysql > alter table table name character set utf8;-- change the default character set of the table to utf8, and the columns added after the update will use the new character set

Mysql > show full fields from table name;-- View the column information of the data table, including the character set and operable permissions of the column, etc.

Mysql > alter table table name modify field name field type CHARACTER SET utf8, modify field name field type CHARACTER SET utf8;-the updated field character set attribute is utf8, my original is latin1, and cannot be inserted into Chinese

- -practice your hands--

Mysql > create database test2 charset gbk;-specify the character set as GBK when establishing the test2 database, and then create a new table under this database. The GKB character set is used by default.

Mysql > alter database test2 charset utf8;-- change the default character set to utf8; and then create a new table under this library using the utf8 character set.

Mysql > show create database tests;-- query the information when creating new data

Mysql > create table ab (id char (10)) charset gbk;-- create a new table ab with the gbk character set

Mysql > alter table ab charset utf8;-- change the default character set of ab to utf8

Mysql > create table bd (id char (10) charset gbk);-- specify the gbk character set for the id field when creating the table

Mysql > show create table ab;-- query the information used to build the ab table.

Be careful when changing the character set!

When modifying the character set of tables and columns, consider the compatibility of the character set, otherwise the data will be lost during the mysql conversion process and cannot be restored.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report