mysql convert(`id` using utf8)_如何设置mysql数据库为utf-8编码

本文介绍如何创建一个UTF-8编码的MySQL数据库,并提供将现有数据库转换为UTF-8编码的方法,包括设置字符集、校对规则及转换过程中的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建一个UTF-8 编码的数据库

MySQL

Create a UTF-8 database with binary UTF-8 collation.

Binary UTF-8 provides case-sensitive collation.

CREATE DATABASE confluence CHARACTER SET utf8 COLLATE utf8_bin;

You will also need to set the Server Characterset to utf8. This can be done by adding the following in my.ini for Windows or my.cnf for other OS. It has to be declared in the Server section, which is the section after [mysqld]:

[mysqld]

default-character-set=utf8

Use the status command to verify database character encoding information.

    Screenshot: Using the Status Command to Verify Database Character Encodingmysql-database-creation-and-status-check-via-console.png?version=1&modificationDate=1247635701761

In some cases, the individual tables collation and character encoding may differ from the one that the database as a whole has been configured to use. Please use the command below to ensure all tables within your Confluence database are correctly configured to use UTF-8 character encoding and binary UTF-8 collation:

use confluence;

show table status;

Check for the value listed under the Collation column, to ensure it has been set to utf8_bin (that is, case-sensitive) collation for all tables.

If not, then this can be changed by the following command, executed for each table in the Confluence database:

ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;

Please substitute the above, with each table within the confluence database.

现有mysql数据库改成 UTF-8编码

MySQL database with existing data

For an existing database

If you're using a existing database, confirm the Character Encoding by executing the query:

SHOW VARIABLES LIKE 'character%'; and SHOW VARIABLES LIKE 'collation%';.

The results should be UTF-8.

Before proceeding with the following changes, please backup your database.

This example shows how to change your database from latin1 to utf8.

Dump the database to a text file using mysqldump tool from the command-line :

mysqldump -p --default_character-set=latin1 -u --skip-set-charset confluence > confluence_database.sql

copy confluence_database.sql to confluence_utf8.sql

Open confluence_utf8.sql in a text editor and change all character sets from 'latin1' to 'utf8'

replace method :

(1)in vi or vim ::%s/CHARSET=latin1/CHARSET=utf8/gc    (2) sed -i 's/CHARSET=latin1/CHARSET=utf8/g' /path/to/confluence_utf8.sql

recode latin1..utf8 confluence_utf8.sql (the recode utility is described at http://directory.fsf.org/recode.html; it can actually be downloaded from http://recode.progiciels-bpi.ca/, and is available for Ubuntu via apt-get)

In MySQL:

DROP DATABASE confluence;

CREATE DATABASE confluence CHARACTER SET utf8 COLLATE utf8_bin;

Finally, reimport the UTF-8 text file:

mysql -u -p --default-character-set=utf8 --max_allowed_packet=64M confluence < /home/confluence/confluence_utf8.sql

To support large imports, the parameter '--max_allowed_packet=64M' used above sets the maximum size of an SQL statement to be very large. In some circumstances, you may need to increase it further, especially if p_w_uploads are stored in the database.

***************************************************************

Warning: Always make backups.

Convert existing MySQL database from one charset encoding to anotherALTER DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Convert existing table from one charset encoding to another

ALTER TABLE db_table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

The following commands seem useful (see Related )

Export your latin1 encoded database

mysqldump --user=username --password=password --default-character-set=latin1 --compatible=mysql40 dbname > dump.sqlImport database as utf8

mysql --user=username --password=password --default-character-set=utf8 dbname < dump.sql

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值