Modify the default MySQL encoding settings

Source: Internet
Author: User

Recently, I used the django framework for web development in MacOS, So I installed MySQL5 with MacPorts. However, during the test, the django test framework reports an error because UTF8 data cannot be inserted.

We know that the default encoding for MySQL after installation is latin1 is not suitable for Chinese applications. Therefore, we usually use the following SQL statement to create a database:

Create database mydb default character set utf8 COLLATE utf8_general_ci; the django test framework does not specify the encoding when creating a test DATABASE, so the DATABASE created is encoded as latin1, as a result, errors occur when UTF8 data is loaded.

It seems that django does not provide the encoding option when creating a database. Therefore, you can only modify the default encoding settings of the MySQL server. The following uses mysql5 under MacPorts as an example to describe the setting method.

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

$ Cd/opt/local/etc/mysql5/
$ Sudo cp/opt/local/share/mysql5/mysql/my-small.cnf my. cnf
Edit my. cnf and add the following two lines in [mysqld:

$ Sudo vi my. cnf
[Mysqld]
...
Collation_server = utf8_general_ci
Character_set_server = utf8 save and exit and restart mysqld:

$ Sudo/opt/local/bin/mysqladmin5-u root shutdown
$ Sudo/opt/local/bin/mysqld_safe5 & in this way. Confirm:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.