安装mysql时如果字符编码为默认值latin1,则需要修改为utf8以便支持中文数据。
命令如下:
1、显示数据库字符集
mysql> show create database test;
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| test | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)
2、显示数据表字符集
mysql> show create table t3;
+-------+----------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------+
| t3 | CREATE TABLE `t3` (
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
3、修改数据表字符集编码
修改数据表的编码
mysql> alter table t4 default character set utf8;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> show create table t4;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t4 | CREATE TABLE `t4` (
`name` varchar(10) CHARACTER SET latin1 DEFAULT NULL,
`sex` varchar(5) CHARACTER SET latin1 DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
修改某个字段编码
mysql> alter table t4 change name name varchar(20) character set utf8;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> show create table t4;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------+
| t4 | CREATE TABLE `t4` (
`name` varchar(20) CHARACTER SET utf8 DEFAULT NULL,
`sex` varchar(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
修改整个数据表的字符集编码,包括数据表和字段
mysql> alter table t4 convert to character set utf8 collate utf8_general_ci;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> show create table t4;
+-------+-----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------+
| t4 | CREATE TABLE `t4` (
`name` varchar(10) DEFAULT NULL,
`sex` varchar(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)collate、collation 即比对方法。用于指定数据集如何排序,以及字符串的比对规则。可以由show collation命令查看mysql支持的所有比对规则。例如:utf8_general_ci 中 ci 是 case insensitive 的缩写,代表大小写不敏感;cs 是 case sensitive 的缩写,代表大小写是敏感的。
4、修改数据库默认字符集编码
mysql> alter database test default character set utf8;
Query OK, 1 row affected (0.00 sec)mysql> show create database test;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| test | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
5、修改mysql默认字符集编码
[mysqld]下添加:
character-set-server=utf8
collation-server=utf8_general_ci
然后重启mysql服务,输入命令查询当前字符集编码是否正确
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| 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 | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)