mysql 字符编码
一、Windows
1、中止MySQL服务
2、在MySQL的安装目录下找到my.ini,如果没有就把my-medium.ini复制为一个my.ini即可
3、打开my.ini以后,在[client]和[mysqld]下面均加上default-character-set=utf8,保存并关闭
4、启动MySQL服务
二、Linux
1、中止MySQL服务(bin/mysqladmin -u root shutdown)
2、在/etc/下找到my.cnf,如果没有就把MySQL的安装目录下的support-files目录下的my-medium.cnf复制到/etc/下并改名为my.cnf即可
3、打开my.cnf以后,在[client]和[mysqld]下面均加上default-character-set=utf8,保存并关闭
4、启动MySQL服务(bin/mysqld_safe &)
如果不幸你导出的是整个有很多不同编码的数据表的数据库,ok。请使用替换工具把原来有的各种编码都替换成你要的目的编码。我这里原来有utf8和latin1,所以只需要把Latin1全部替换成utf8即可。
之后新建一个要导入数据的数据库:
view plaincopy to clipboardprint?
mysql> create database `TARGET_DB` default character set utf8 collate utf8_general_ci
mysql> create database `TARGET_DB` default character set utf8 collate utf8_general_ci
OK,现在可以导入数据表了:
view plaincopy to clipboardprint?
mysql -uroot -p TARGET_DB --default-character-set=utf8 < /home/test/sends.sql
mysql -uroot -p TARGET_DB --default-character-set=utf8 < /home/test/sends.sql
最后附带一些mysql操作字符集的时候会用到的命令,都是从网上收集的:
view plaincopy to clipboardprint?
1. 修改 MySQL 数据库默认字符集(mysql database default character set)
alter database testdb default character set = gb2312;
2. 修改 MySQL 数据表默认字符集(mysql table default character set)
alter table ip_to_country default character set = gb2312;
注意:修改 MySQL 的默认字符集,不管是在数据库级别,还是数据表级别, 对已经存储的字符数据无任何改变。只是新增的表或列,开始使用新的 字符集。
3. 转换 MySQL 数据表字符集(mysql table character set)
MySQL 中有一个数据表 ip_to_country,以前该表字符列数据都是以 utf8 编码格式存放的, 现在想转换为 gb2312 编码格式存放。需要使用 MySQL 转换字符集命令:convert to character set
alter table ip_to_country convert to character set gb2312;
提示:convert to 也会把数据表 ip_to_country 的默认字符集从 utf8 更改为 gb2312。
4. 转换 MySQL 数据表中某个列的字符集(mysql column character set)
alter table ip_to_country modify country_name varchar(50) character set gb2312;
这个语句把单独一个列 country_name 的字符集转换为 gb2312。这在用法在实际中比较少见, 因为大多数情况下,都是改变整个表的字符集。
1. 修改 MySQL 数据库默认字符集(mysql database default character set)
alter database testdb default character set = gb2312;
2. 修改 MySQL 数据表默认字符集(mysql table default character set)
alter table ip_to_country default character set = gb2312;
注意:修改 MySQL 的默认字符集,不管是在数据库级别,还是数据表级别, 对已经存储的字符数据无任何改变。只是新增的表或列,开始使用新的 字符集。
3. 转换 MySQL 数据表字符集(mysql table character set)
MySQL 中有一个数据表 ip_to_country,以前该表字符列数据都是以 utf8 编码格式存放的, 现在想转换为 gb2312 编码格式存放。需要使用 MySQL 转换字符集命令:convert to character set
alter table ip_to_country convert to character set gb2312;
提示:convert to 也会把数据表 ip_to_country 的默认字符集从 utf8 更改为 gb2312。
4. 转换 MySQL 数据表中某个列的字符集(mysql column character set)
alter table ip_to_country modify country_name varchar(50) character set gb2312;
这个语句把单独一个列 country_name 的字符集转换为 gb2312。这在用法在实际中比较少见, 因为大多数情况下,都是改变整个表的字符集。
view plaincopy to clipboardprint?
创建一个名为test的数据库,并且设置字符集都是utf8:
create database `test` default character set utf8 collate utf8_general_ci
查看ta数据库中类似test的数据表的信息:
show table status from ta like '%test%';
查看 MySQL 数据库服务器和数据库字符集。
show variables like '%char%';
+--------------------------+-------------------------------------+------
| Variable_name | Value |......
+--------------------------+-------------------------------------+------
| character_set_client | utf8 |...... -- 客户端字符集
| character_set_connection | utf8 |......
| character_set_database | utf8 |...... -- 数据库字符集
| character_set_filesystem | binary |......
| character_set_results | utf8 |......
| character_set_server | utf8 |...... -- 服务器字符集
| character_set_system | utf8 |......
| character_sets_dir | D:/MySQL Server 5.0/share/charsets/ |......
查看 MySQL 数据表(table) 的字符集。
show table status from sqlstudy_db like '%countries%';
+-----------+--------+---------+------------+------+-----------------+------
| Name | Engine | Version | Row_format | Rows | Collation |......
+-----------+--------+---------+------------+------+-----------------+------
| countries | InnoDB | 10 | Compact | 11 | utf8_general_ci |......
查看 MySQL 数据列(column)的字符集。
show full columns from countries;
+----------------------+-------------+-----------------+--------
| Field | Type | Collation | .......
+----------------------+-------------+-----------------+--------
| countries_id | int(11) | NULL | .......
| countries_name | varchar(64) | utf8_general_ci | .......
| countries_iso_code_2 | char(2) | utf8_general_ci | .......
| countries_iso_code_3 | char(3) | utf8_general_ci | .......
| address_format_id | int(11) | NULL | .......
查看当前安装的 MySQL 所支持的字符集。
show charset; 或
show char set;
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
修改表和字段的字符集
//修改表
alter table 表名 convert to character set gbk;
//修改字段
alter table 表名 modify column '字段名' varchar(30) character set gbk not null;