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