转自:http://blog.csdn.net/wyzxg/article/details/8779682

author:skate
time:2012/05/18

修改mysql的字符集和默认存储引擎

1.修改mysql的字符集

mysql库现有字符集
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     | 
| character_set_connection | latin1                     | 
| character_set_database   | latin1                     | 
| character_set_filesystem | binary                     | 
| character_set_results    | latin1                     | 
| character_set_server     | latin1                     | 
| character_set_system     | utf8                       | 
| character_sets_dir       | /usr/share/mysql/charsets/ | 
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql>

mysql的字符集分为几类
A.客户端字符集:通过系统变量”character_set_client“表示,通知server端,客户端提交的sql语句编码格式
B.连接字符集:通过系统变量”character_set_connectiont“表示,server端翻译sql语句时,使用的编码格式
C.结果集字符集:通过系统变量”character_set_results“表示,server端返回结果集之前把结果集转换成的编码格式
D.存储字符集:通过系统变量”character_set_results“和“character_set_server”表示,是数据在存储引擎里编码格式

为了不出现乱码,我们需要统一修改这些参数,比如我要把数据库的默认字符集改为utf8,如下所示

在[client]下添加如下参数
default-character-set=utf8

在[mysqld]下添加如下参数
default-character-set=utf8

然后重启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.01 sec)

mysql>

mysql> show variables like 'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci | 
| collation_database   | utf8_general_ci | 
| collation_server     | utf8_general_ci | 
+----------------------+-----------------+
3 rows in set (0.00 sec)

mysql>

mysql的字符集分的比较细,可以采用默认值,也可以指定值

A.数据库字符集

使用默认字符集的库
mysql> create database db1;
Query OK, 1 row affected (0.01 sec)

mysql> show create database db1;
+----------+--------------------------------------------------------------+
| Database | Create Database                                              |
+----------+--------------------------------------------------------------+
| db1      | CREATE DATABASE `db1` /*!40100 DEFAULT CHARACTER SET utf8 */ | 
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)

指定字符集的库
mysql> create database db2 default character set latin1;
Query OK, 1 row affected (0.01 sec)

mysql> show create database db2;
+----------+----------------------------------------------------------------+
| Database | Create Database                                                |
+----------+----------------------------------------------------------------+
| db2      | CREATE DATABASE `db2` /*!40100 DEFAULT CHARACTER SET latin1 */ | 
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)

修改库的字符集
mysql> alter database db2 default character set utf8;
Query OK, 1 row affected (0.00 sec)

mysql> show create database db2;
+----------+--------------------------------------------------------------+
| Database | Create Database                                              |
+----------+--------------------------------------------------------------+
| db2      | CREATE DATABASE `db2` /*!40100 DEFAULT CHARACTER SET utf8 */ | 
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)

B.表字符集

mysql> use db2;
Database changed

使用默认库字符集的表
mysql> create table t1(a varchar(10));
Query OK, 0 rows affected (0.01 sec)

mysql> show create table t1;
+-------+-----------------------------------------------------------------------------------------+
| Table | Create Table                                                                            |
+-------+-----------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `a` varchar(10) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 
+-------+-----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

创建指定字符集的表
mysql> create table t2(a varchar(10)) default character set latin1;;
Query OK, 0 rows affected (0.01 sec)

ERROR: 
No query specified

mysql> show create table t2;
+-------+-------------------------------------------------------------------------------------------+
| Table | Create Table                                                                              |
+-------+-------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `a` varchar(10) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 | 
+-------+-------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

更改表的字符集
mysql> alter table t2 default character set utf8;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table t2;
+-------+--------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                 |
+-------+--------------------------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `a` varchar(10) character set latin1 default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 
+-------+--------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

从上面可以看到,修改表的字符集时,不会影响已经存在列的字符集,但新增列将继承表的字符集,如下

mysql> alter table t2 add a2 varchar(10);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table t2;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                  |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `a` varchar(10) character set latin1 default NULL,
  `a2` varchar(10) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

mysql>

如果想更改已经存在表和字符列的字符集,要用如下

mysql> alter table t2 convert to character set latin1;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table t2;
+-------+----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                               |
+-------+----------------------------------------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `a` varchar(10) default NULL,
  `a2` varchar(10) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 | 
+-------+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

C.字段字符集

使用默认和指定字符集创建的列
mysql> create table t3 (a1 varchar(10),a2 varchar(10) character set latin1);
Query OK, 0 rows affected (0.04 sec)

mysql> show create table t3;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                   |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------+
| t3    | CREATE TABLE `t3` (
  `a1` varchar(10) default NULL,
  `a2` varchar(10) character set latin1 default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 
+-------+------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

查看列的字符集
mysql> show full columns from t3;
+-------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type        | Collation         | Null | Key | Default | Extra | Privileges                      | Comment |
+-------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
| a1    | varchar(10) | utf8_general_ci   | YES  |     | NULL    |       | select,insert,update,references |         | 
| a2    | varchar(10) | latin1_swedish_ci | YES  |     | NULL    |       | select,insert,update,references |         | 
+-------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
2 rows in set (0.01 sec)

修改列的字符集
mysql> alter table t3 change a2 a2 varchar(10) character set utf8;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show full columns from t3;
+-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type        | Collation       | Null | Key | Default | Extra | Privileges                      | Comment |
+-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| a1    | varchar(10) | utf8_general_ci | YES  |     | NULL    |       | select,insert,update,references |         | 
| a2    | varchar(10) | utf8_general_ci | YES  |     | NULL    |       | select,insert,update,references |         | 
+-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
2 rows in set (0.00 sec)

mysql>

结论:字符集从数据库、表、列的字符集是逐级继承的关系

查看字符集命令
mysql> show create table t4; //显示表字符集
mysql> show create database db2; //显示库字符集
mysql> show full columns from t4; //显示列字符集

alter database db2 default character set utf8; //更改库的字符集
alter table t2 convert to character set latin1;//更改表的现存列字符集
alter table t2 default character set utf8;  //更能改表的默认字符集
alter table t3 change a2 a2 varchar(10) character set utf8; //更能改字段的字符集

2.修改mysql的默认存储引擎

mysql> show variables like 'storage%';
+----------------+--------+
| Variable_name  | Value  |
+----------------+--------+
| storage_engine | MyISAM | 
+----------------+--------+
1 row in set (0.01 sec)

mysql>

在[mysqld]下添加如下参数
default-storage-engine=innodb

重启mysql服务,查看

mysql> show variables like 'storage%';
+----------------+--------+
| Variable_name  | Value  |
+----------------+--------+
| storage_engine | InnoDB | 
+----------------+--------+
1 row in set (0.01 sec)

mysql>

创建默认字符集的表
mysql> create table t4 (a1 int);
Query OK, 0 rows affected (0.01 sec)

mysql> show create table t4;
+-------+--------------------------------------------------------------------------------------+
| Table | Create Table                                                                         |
+-------+--------------------------------------------------------------------------------------+
| t4    | CREATE TABLE `t4` (
  `a1` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 
+-------+--------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

更改表的默认字符集
mysql> alter table t4 engine myisam;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table t4;
+-------+--------------------------------------------------------------------------------------+
| Table | Create Table                                                                         |
+-------+--------------------------------------------------------------------------------------+
| t4    | CREATE TABLE `t4` (
  `a1` int(11) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 | 
+-------+--------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

------end------

修改mysql的字符集和默认存储引擎的更多相关文章

  1. 【Mysql】修改mysql的字符集和默认存储引擎,解决数据入库乱码问题

    背景 在使用Python + Testlink做自动化的过程中,遇到了数据入库出现乱码的情况,后来通过修改字符集的方式解决了这个问题.下面的内容主要来自于一篇相关博文,博主做了细微的调整, 原文链接: ...

  2. 字符编码,存储引擎,MySQL字段类型,MySQL字段约束条件

    字符编码 查看MySQL默认编码命令:\s """ 如果是5.X系列 显示的编码有多种 latin1 gbk 如果是8.X系列 显示的统一是utf8mb4 utf8mb4 ...

  3. MySQL教程详解之存储引擎介绍及默认引擎设置

    什么是存储引擎? 与其他数据库例如Oracle 和SQL Server等数据库中只有一种存储引擎不同的是,MySQL有一个被称为“Pluggable Storage Engine Architectu ...

  4. [MySQL Reference Manual]15. 其他存储引擎

    15. 其他存储引擎 15. 其他存储引擎 15.1 设置存储引擎 15.2 MyISAM存储引擎 15.2.1 MyISAM启动选项 15.2.2 Key的空间要求 15.2.3 MyISAM表存储 ...

  5. [MySQL Reference Manual]14 InnoDB存储引擎

    14 InnoDB存储引擎 14 InnoDB存储引擎 14.1 InnoDB说明 14.1.1 InnoDB作为默认存储引擎 14.1.1.1 存储引擎的趋势 14.1.1.2 InnoDB变成默认 ...

  6. MySQL存储引擎的实际应用以及对MySQL数据库中各主要存储引擎的独特特点的描述

    MySQL存储引擎的实际应用以及对MySQL数据库中各主要存储引擎的独特特点的描述: 1.MySQL有多种存储引擎: MyISAM.InnoDB.MERGE.MEMORY(HEAP).BDB(Berk ...

  7. 《MySQL技术内幕:InnoDB存储引擎(第2版)》书摘

    MySQL技术内幕:InnoDB存储引擎(第2版) 姜承尧 第1章 MySQL体系结构和存储引擎 >> 在上述例子中使用了mysqld_safe命令来启动数据库,当然启动MySQL实例的方 ...

  8. mysql-介绍、MySQL部署、数据类型、存储引擎

    数据库介绍  什么是数据? 数据(data)是事实或观察的结果,是对客观事物的逻辑归纳,是用于表示客观事物的未经加工的原始素材.数据是信息的表现形式和载体,可以是符号.文字.数字.语音.图像.视频等. ...

  9. MySQL - 常用三种数据库存储引擎

    数据库存储引擎:是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建.查询.更新和删除数据.不同的存储引擎提供不同的存储机制.索引技巧.锁定水平等功能,使用不同的存储引擎,还可以获得特 ...

随机推荐

  1. e669. 绘制缓冲图像

    To draw on a buffered image, create a graphics context on the buffered image. // Create a graphics c ...

  2. e657. 用直线和曲线绘制图形

    GeneralPath shape = new GeneralPath(); shape.moveTo(x, y); shape.lineTo(x, y); shape.quadTo(controlP ...

  3. linux -- gedit快捷键

    Shortcuts for tabs:Ctrl + Alt + PageDown     Switches to the next tab to the left.Ctrl + Alt + PageD ...

  4. 转载:QTableView中嵌入可视化组件

    出处:http://qimo601.iteye.com/blog/1538364 QTableView中嵌入可视化组件方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简 ...

  5. org.xml.sax.SAXParseException: prolog 中不允许有内容

    org.xml.sax.SAXParseException: prolog 中不允许有内容 digester.fatalError 不下心踢了电源导致的错误应该是解析xml出问题,找了半天不知道哪个x ...

  6. jQuery实现鼠标选中文字后弹出提示窗口效果

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  7. _T("D:\\122.txt")【字符集问题】或【类型转换问题】

    项目->属性->常规->字符集->使用多字节字符集!时用_T("Filename"), 貌似不是字符集的问题!  1.使用替换,,,后, _T(" ...

  8. 【观点】“马云:金融是要为外行人服务",这个观点其实并不新鲜

    不久前,马云在外滩国际金融峰会演讲,称金融行业需要“搅局者”.他说:“今天的金融,确实做得不错,没有今天这样的金融机构,中国的经济30年来不可能发展到今天,但是靠今天银行的机制,我不相信能支撑30年以 ...

  9. 使用tensorflow深度学习识别验证码

    除了传统的PIL包处理图片,然后用pytessert+OCR识别意外,还可以使用tessorflow训练来识别验证码. 此篇代码大部分是转载的,只改了很少地方. 代码是运行在linux环境,tesso ...

  10. BigDecimal类(精度计算类)的加减乘除

    BigDecimal类 对于不需要任何准确计算精度的数字可以直接使用float或double,但是如果需要精确计算的结果,则必须使用BigDecimal类,而且使用BigDecimal类也可以进行大数 ...