官术网_书友最值得收藏!

Setting UTF-8 as the default encoding for MySQL configuration

MySQL is the most popular open source database. In this recipe, I will tell you how to set UTF-8 as the default encoding for it. Note that if you don't set this encoding in the database configuration, you might get into a situation where LATIN1 is used by default with your UTF-8 encoded data. This will lead to database errors whenever symbols such as € are used. Also, this recipe will save you from the difficulties of converting the database data from LATIN1 to UTF-8, especially when you have some tables encoded in LATIN1 and others in UTF-8.

Getting ready

Make sure that the MySQL database management system and the MySQLdb Python module are installed and you are using the MySQL engine in your project's settings.

How to do it…

Open the /etc/mysql/my.cnf MySQL configuration file in your favorite editor and ensure that the following settings are set in the sections: [client], [mysql], and [mysqld], as follows:

# /etc/mysql/my.cnf
[client]
default-character-set = utf8

[mysql]
default-character-set = utf8

[mysqld]
collation-server = utf8_unicode_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8

If any of the sections don't exist, create them in the file. Then, restart MySQL in your command-line tool, as follows:

$ /etc/init.d/mysql restart

How it works…

Now, whenever you create a new MySQL database, the databases and all their tables will be set in UTF-8 encoding by default.

Don't forget to set this in all computers where your project is developed or published.

主站蜘蛛池模板: 大姚县| 兴宁市| 威远县| 钟祥市| 巴青县| 乾安县| 禄丰县| 龙井市| 呼玛县| 高淳县| 桂东县| 昭平县| 黄梅县| 历史| 德江县| 罗源县| 凤城市| 宝丰县| 海伦市| 固始县| 海丰县| 美姑县| 永胜县| 阜阳市| 镇安县| 宿州市| 兴山县| 宁强县| 洞头县| 汝南县| 曲阜市| 吉木乃县| 马鞍山市| 电白县| 衡南县| 伊川县| 诸暨市| 瓦房店市| 牡丹江市| 五原县| 鹿泉市|