码迷,mamicode.com
首页 > 数据库 > 详细

mysql 字符编码设置

时间:2017-04-02 23:23:47      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:warning   charset   安装mysql   margin   innodb   ble   system   like   war   

安装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: 0

mysql> 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: 0

mysql> 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: 0

mysql> 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)

 

mysql 字符编码设置

标签:warning   charset   安装mysql   margin   innodb   ble   system   like   war   

原文地址:http://www.cnblogs.com/tianyuchen/p/6660113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!