使用 MySQL 字符 集 对于非英文网站,当他们使用非英语语言从数据库中写入或读取数据时,常常必须解决字符集的问题。字符集指导数据库哪种字符编码方案用于数据的写入读取,这样 可以简单地理解 为 字符集 的一个子集 整理 ,它告诉数据库如何存储数据。 今天

使用 MySQL字符集

对于非英文网站,当他们使用非英语语言从数据库中写入或读取数据时,常常必须解决字符集的问题。字符集指导数据库哪种字符编码方案用于数据的写入读取,这样可以简单地理解为字符集的一个子集整理,它告诉数据库如何存储数据。

今天我们谈论的是使用MySQL的字符集。在MySQL环境中,我们想存储中文、日文等除了英文外其它的语言,这个时候我们就要将字符集应用到数据库、表和列中。当我们连接MySQL数据库时同样也需要字符集,应该为连接设置字符集。现在,我总结了一些命令用于查看我们使用的数据的字符集以及根据需要如何改变字符集。在命令提示符窗口,首先我们需要使用

“mysql -u [name] -p” 登录mysql客户端。

接下来,我们想检查数据端和服务的一些有关于字符集的变量,例如:连接字符集。我们输入如下命令:

show variables like 'char%';

show variables like 'collation%';

执行命令后会出现如下信息提示:

+--------------------------+---------------------------------------------------------+

| 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 | C:\Program Files\MySQL\MySQL Server 5.1\share\charsets\ |

+--------------------------+---------------------------------------------------------+

对于我们的数据库引擎所使用的字符集便一目了然。我们可以令改变这些变量使用如下命令:

SET variable_name=value /* SET character_set_connection=utf8; */

进入到我们设置的字符集环境,运行:

SHOW CREATE DATABASE database_name

在输出中我们可以找到如上注释处默认的字符集。如果想改变数据库的字符集,我们执行:

ALTER DATABASE database_name CHARACTER SET charset_name COLLATE collation_name

当我们创建新的数据库时也可以设置字符集,命令:

CREATE DATABASE database_name CHARACTER SET charset_name COLLATE collation_name

对于数据库的表, 命令相似的, 执行:

SHOW CREATE TABLE table_name

在输出的最后面,可以找到“DEFAULT CHARSET or COLLATE”,如果我们想改变这些,执行:

ALTER TABLE table_name CONVERT TO CHARACTER SET charset_name COLLATE collation_name

当我们创建新的表时也可以设置字符集,命令:

CREATE TABLE table_name (column_list) CHARACTER SET charset_name COLLATE collation_name

针对列, 需要执行:

SHOW FULL COLUMNS IN table_name

第三列是 collation. 需要如下方法改变:

ALTER TABLE table_name MODIFY col_name data_type CHARACTER SET charset_name COLLATE collation_name

通过学习以上命令, 你能够掌握MySQL字符集和collation. 如果你使用编程语言连接MySQL用于存入和读取数据,你也需要关联语言中设置字符集编码方案如PHP。

小贴士:如果你在MySQL中存储中文或是其它非英文数据,有时候你会在命令控制台中发现如上陈列的问题。你可以尝试导出外部sql文件并用文本编辑软件打开,你会惊奇发现你的中文数据再现。 这意味着你的数据存储正确,但是命令控制台中却无法正确显示。

译者注:我也遇到过“小贴士”中最后一点提到的情况。我的MySQL是5.1版,起先我在Console中使用的是UTF8字符集,表中显示的字符时中文乱码(我的表级约束是UTF8字符集),我使用 charset gbk; 命令后任然是乱码。再次使用 charset gbk; 命令,发现能正确显示中文。但是在MySQL5.0版中却无法用上述方法实现中文正确显示。

--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --

Work with MySQL character set and collation

原文内容:

Work with MySQL character set and collation

Source : Peter Date : 2012-06-17 07:07:28

For non-English websites, they often have to deal with character set and collation if they want to store data to and read data from databases with other languages. Character set tells the database which kind of character encoding scheme to use to store or

read data, collation can be simply understood as a subset of character set, it tells the database how to sort data.

We talk about working with character set and collation of MySQL today. In MySQL, if we want to store Chinese, Japanese or other languages other than English, we may need to set the relative character set for the database, tables and columns. Also, when we

connect to MySQL. we may need to set the character set for the connection. Now I summarize some commands used to see what are the character set and collation of our database and how to change them as needed. On command prompt window, we need to log in to

the mysql client with the mysql -u [username] -p command first.

Now we may want to check some variables about character set and collation for our database client and server, for example, connection character set. We can type following commands:

SHOW VARIABLES LIKE 'char%';

SHOW VARIABLES LIKE 'collation%';

The command will give us some information like

+--------------------------+---------------------------------------------------------+

| 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 | C:\Program Files\MySQL\MySQL Server 5.1\share\charsets\ |

+--------------------------+---------------------------------------------------------+

We can easily understand that the character set we are using for the database engine. also we can change these variables by using

SET variable_name=value /* SET character_set_connection=utf8; */

Next come to the database character set and collation, we run

SHOW CREATE DATABASE database_name

We can find our default character set in the comment of the output. If we want to change the character set and collation of the database, we run

ALTER DATABASE database_name CHARACTER SET charset_name COLLATE collation_name

We can also set the character set and collation when we create the new database

CREATE DATABASE database_name CHARACTER SET charset_name COLLATE collation_name

For database tables, the commands are similar, we run

SHOW CREATE TABLE table_name

At the end of the output, we may find the DEFAULT CHARSET or COLLATE, if we want to change them, we run

ALTER TABLE table_name CONVERT TO CHARACTER SET charset_name COLLATE collation_name

we can also set the character set and collation when we create a table, we run

CREATE TABLE table_name (column_list) CHARACTER SET charset_name COLLATE collation_name

For columns, we need to run

SHOW FULL COLUMNS IN table_name

the third column is the collation. We can change them with

ALTER TABLE table_name MODIFY col_name data_type CHARACTER SET charset_name COLLATE collation_name

By knowing all the commands above, you may be able to handle MySQL character set and collation. If you use programming languages to connect to MySQL to store and read data, you may also need to set the character encoding scheme in relative languages such as

PHP.

Finally one tip for you: If you store Chinese or other non-English data in MySQL database, sometimes you may find they are displayed as question marks in the command console. You can have a try to export the data to an external sql file and open the sql file

with a text editor, you may be surprised that you can see your Chinese again. This means your data are stored properly but somehow the command console cannot display them correctly.

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

mysql 非英文_非英文网站如何使用MySQL的字符集相关推荐

  1. jsp mysql失物招领_失物招领网站系统的设计与实现(JSP,MySQL)(含录像)

    失物招领网站系统的设计与实现(,MySQL)(含录像)(任务书,毕业论文10000字,程序代码,MySQL数据库,答辩PPT) 本系统的设计主要分六个大模块,每个大模块包含有各自的小模块对功能进行细分 ...

  2. 负载均衡mysql的使用_使用负载均衡集群集化 MySQL - Azure Virtual Machines | Microsoft Docs...

    使用负载均衡的集来群集化 Linux 上的 MySQL 04/14/2015 本文内容 重要 经典 VM 将于 2023 年 3 月 1 日停用. 如果从 ASM 使用 IaaS 资源,请在 2023 ...

  3. mysql 前台启动_从Windows命令行启动MySQL

    可以从命令行手动启动MySQL服务器.可以在任何版本的Windows中实现. 要想从命令行启动mysqld服务器,你应当启动控制台窗口(或"DOS window")并输入命令: C ...

  4. mysql死锁语句_记一次神奇的Mysql死锁排查

    背景 说起Mysql死锁,之前写过一次有关Mysql加锁的基本介绍,对于一些基本的Mysql锁或者死锁都有一个简单的认识,可以看下这篇文章为什么开发人员需要了解数据库锁.有了上面的经验之后,本以为对于 ...

  5. mysql 撤销删除_线上磁盘告警,mysql无法释放空间,踩了个大坑,大家记得别踩坑...

    活动买的阿里云服务器磁盘告警了,活动买的磁盘只有40g,里面装的宝塔环境,除了网站程序还有mysql数据库.数据库整个大概20多G. 有几个表数据达到千万级别了,在不扩容的情况下,老王选择了删除部分不 ...

  6. 韩国mysql化妆品_jsp70279化妆品护肤品购物网站 双数据库 mysql版

    jsp70279化妆品护肤品购物网站 双数据库 mysql版 该设计有演示视频 100%能运行 买重包换 保密发送 一校一份 编号: jsp70279 语言+数据库: jsp+sql2008+mysq ...

  7. mysql邮箱配置文件_[flask实践] 解决qq邮箱/mysql的相关配置问题

    笔者经过flask web(Miguel著,封面是一条狗)一书的学习,打算实现一个旅游类网站,在此过程中发现,相对于书中的flasky博客程序,需要作出一些改变: 1. 注册邮箱:国内要使用126,q ...

  8. c 获取mysql列数据_转 用C API 操作MySQL数据库

    用C API 操作MySQL数据库 参考MYSQL的帮助文档整理 这里归纳了C API可使用的函数,并在下一节详细介绍了它们.请参见25.2.3节,"C API函数描述". 函数 ...

  9. mysql编译参数查看_查看 apache,nginx,mysql 安装时的编译参数

    快速查看服务器软件的编译参数:1.nginx编译参数: nginx -V 2.apache编译参数: cat apache的安装目录/build/config.nice 3.php编译参数: php ...

最新文章

  1. 整理一点关于Lucene的学习资料, 方便自己与别人查看
  2. 7.Nginx_Keepalived高可用配置
  3. 【Netty】NIO 缓冲区 ( Buffer ) 分散 Scattering 与 聚合 Gathering 操作
  4. HTML的五种经典布局方式(二)
  5. IBASE and ES change pointer
  6. 35.使用拦截器实现权限验证
  7. 江西住建云实名认证怎么弄_王者荣耀无限时间怎么弄 2020年无限时间账号
  8. html如何太假icon图标,CSS3 icon font完全指南(CSS3 font 会取代icon图标)
  9. 一文了解十大 Java 开发者必备测试框架!
  10. 3.3 rsync同步之ssh隧道方式
  11. 乐至天气预报软件测试,乐至天气预报15天
  12. 系统编程IO操作 之 电子词典
  13. JS替换、删除指定字符
  14. SFP光纤收发器搭配光模块的交换机连接方案解决方案
  15. 如何保证代码的健壮性和可读性
  16. 牛客网C语言编程初学者入门训练135题
  17. Python编程:实现凯撒密码加密解密
  18. Spring Boot保姆级入门,还不会过来胖我
  19. shardingjdbc多数据源配置
  20. JavaSE项目 | 纯Java实现贪吃蛇小游戏

热门文章

  1. 深度学习网络模型实战
  2. 大数据分析入门小技巧
  3. 数据分析数据可视化(一)
  4. android迷宫源代码,迷宫 c++源代码(Maze c++ source code).doc
  5. php表单验证代码实例,PHP表单验证实例代码-三体教程在线编辑器
  6. AcWing 802. 区间和
  7. oracle 转成sql server,怎样把Oracle查询转换为SQL Server
  8. 光耦驱动单向可控硅_单向可控硅最筒单电路图大全
  9. Android - 开发者应该深入学习的10个开源应用项目
  10. 011-你觉得自动化测试有什么意义,都需要做些什么