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

How to set the default encoding in MySQL

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

MySQL how to set the default code, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Recently, I used django framework to do web development under MacOS, so I installed MySQL5 with MacPorts. However, when testing, django's test framework will report an error because the UTF8 data cannot be inserted.

We know that the default code after MySQL installation is latin1, which is not suitable for Chinese applications. So we usually create it with the following SQL statement:

CREATE DATABASE mydb DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; while the django test framework does not specify a code like this when creating a test database, so the database code created is latin1, resulting in errors when loading UTF8 data.

It seems that django does not provide the option to specify the encoding when creating the database, so you can only modify the default encoding settings of the MySQL server. Let's take 5 under MacPorts as an example to describe the setting method.

First make a copy of my.cnf to / opt/local/etc/mysql5:

$cd / opt/local/etc/mysql5/

$sudo cp / opt/local/share/mysql5/mysql/my-small.cnf my.cnf

Then edit my.cnf and add the following two lines in the [mysqld] section:

$sudo vi my.cnf

[mysqld]

...

Collation_server = utf8_general_ci

Character_set_server = restart mysqld after utf8 save and exit:

$sudo / opt/local/bin/mysqladmin5-u root shutdown

$sudo / opt/local/bin/mysqld_safe5 & that's fine. Make sure:

Mysql > show variables like collation_server

+-+ +

| | Variable_name | Value |

+-+ +

| | collation_server | utf8_general_ci |

+-+ +

1 row in set (0.00 sec)

Mysql > show variables like character_set_server

+-+ +

| | Variable_name | Value |

+-+ +

| | character_set_server | utf8 |

+-+ +

1 row in set (0.00 sec)

Charlee

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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