修改MySQL数据库字符集
root@localhost:mysql3306.sock [zlm]>create table charset(
-> id int,
-> name varchar()
-> ) engine=innodb charset=utf8;
Query OK, rows affected (0.01 sec)
Check the character set.
root@localhost:mysql3306.sock [zlm]>\s
--------------
mysql Ver 14.14 Distrib 5.7., for linux-glibc2. (x86_64) using EditLine wrapper Connection id:
Current database: zlm
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.-log MySQL Community Server (GPL)
Protocol version:
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql3306.sock
Uptime: 2 min sec
Insert a record contains Chinese characters into test table.
root@localhost:mysql3306.sock [zlm]>insert into charset values(,'黎明');
Query OK, row affected (0.00 sec) root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+--------+
| id | name |
+------+--------+
| | 黎明 |
+------+--------+
row in set (0.00 sec)
Change the character from utf8 to to gbk.
root@localhost:mysql3306.sock [zlm]>set @@global.character_set_database=gbk;
Query OK, rows affected, warning (0.00 sec) root@localhost:mysql3306.sock [zlm]>set @@global.character_set_server=gbk;
Query OK, rows affected (0.00 sec) root@localhost:mysql3306.sock [zlm]>show global variables like 'character%';
+--------------------------+----------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | gbk |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.7.-linux-glibc2.-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+
rows in set (0.00 sec) root@localhost:mysql3306.sock [zlm]>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/local/mysql-5.7.-linux-glibc2.-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+
rows in set (0.00 sec)
Start a new mysql client and check the data in test table.
[root@zlm1 :: ~]
#mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. root@localhost:mysql3306.sock [(none)]>show variables like 'character%';
+--------------------------+----------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | gbk |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.7.-linux-glibc2.-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+
rows in set (0.00 sec) root@localhost:mysql3306.sock [(none)]>select * from charset;
ERROR (3D000): No database selected
root@localhost:mysql3306.sock [(none)]>use zlm //After execute "use database",the character set of database will turn into utf8 again.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+--------+
| id | name |
+------+--------+
| | 黎明 |
+------+--------+
row in set (0.00 sec) root@localhost:mysql3306.sock [zlm]>select length('黎明') from dual;
+------------------+
| length('黎明') |
+------------------+
| | //The length of one Chinese character occupys three bytes.It depends on the character set of table.
+------------------+
row in set (0.00 sec) //The data still shows correct result after change the database and server character set to gbk. root@localhost:mysql3306.sock [zlm]>\s
--------------
mysql Ver 14.14 Distrib 5.7., for linux-glibc2. (x86_64) using EditLine wrapper Connection id:
Current database: zlm
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.-log MySQL Community Server (GPL)
Protocol version:
Connection: Localhost via UNIX socket
Server characterset: gbk
Db characterset: utf8 //The character set of database turns back to utf8.Therefore,no messy code appears.
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql3306.sock
Uptime: min sec Threads: Questions: Slow queries: Opens: Flush tables: Open tables: Queries per second avg: 0.052
-------------- root@localhost:mysql3306.sock [zlm]>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 | gbk |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.7.-linux-glibc2.-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+
rows in set (0.01 sec)
Set the character set again in curren session to gbk.
root@localhost:mysql3306.sock [zlm]>set character_set_database=gbk;
Query OK, rows affected, warning (0.00 sec) root@localhost:mysql3306.sock [zlm]>show variables like 'character%';
+--------------------------+----------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | gbk |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.7.-linux-glibc2.-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+
rows in set (0.00 sec) root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+--------+
| id | name |
+------+--------+
| | 黎明 |
+------+--------+
row in set (0.00 sec) //Change the character set of client tool(mine is Xshell) to gbk. root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+--------+
| id | name |
+------+--------+
| | 榛庢槑 | //After changing the character set of client tool,the messy code occurs.
+------+--------+
row in set (0.00 sec)
Change the character set of client tool back to utf8 and insert another record into test table.
root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+--------+
| id | name |
+------+--------+
| | 黎明 |
+------+--------+
row in set (0.00 sec) root@localhost:mysql3306.sock [zlm]>insert into charset values(,'上海');
Query OK, row affected (0.00 sec) root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+--------+
| id | name |
+------+--------+
| | 黎明 |
| | 上海 |
+------+--------+
rows in set (0.00 sec) //The changing of character set from utf8 to gbk does not influence the result of Chinese characters.
Change the character set of database & server to utf8 again.Then,change the character set of client & connection to gbk.
root@localhost:mysql3306.sock [zlm]>set character_set_database=utf8;
Query OK, rows affected, warning (0.01 sec) root@localhost:mysql3306.sock [zlm]>set character_set_server=utf8;
Query OK, rows affected (0.00 sec) root@localhost:mysql3306.sock [zlm]>set names gbk;
Query OK, rows affected (0.00 sec) root@localhost:mysql3306.sock [zlm]>\s
--------------
mysql Ver 14.14 Distrib 5.7., for linux-glibc2. (x86_64) using EditLine wrapper Connection id:
Current database: zlm
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.-log MySQL Community Server (GPL)
Protocol version:
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: gbk
Conn. characterset: gbk
UNIX socket: /tmp/mysql3306.sock
Uptime: hour min sec Threads: Questions: Slow queries: Opens: Flush tables: Open tables: Queries per second avg: 0.038
-------------- root@localhost:mysql3306.sock [zlm]>root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+------+
| id | name |
+------+------+
| | hķ |
| | ʏº£ |
+------+------+
rows in set (0.00 sec) //The messy code occured after I've changed the character of my client tool to utf8.
Insert the third record with Chinese characters.
root@localhost:mysql3306.sock [zlm]>insert into charset values(,'中国');
ERROR (HY000): Incorrect string value: '\xAD\xE5\x9B\xBD' for column 'name' at row //It doesn't permit your insertion operation now 'cause they'll be messy code again.
- Cheracter set in MySQL does not make a large influence even though it has so many variables which may confuse us.
- We can specify character set in a single table or even a column of the table which oracle cannot support.
- In order to avoid messy code,make sure to keep character set of connection is bigger or equal with the one of our client tool.
- It's reccomended to use utf8 even utf8mb4 as the character set of MySQL database because it can support almost all the languages
- Notice that the character set of database may change after you execute "use xxx" to choose a target database.
修改MySQL数据库字符集的更多相关文章
- 使用SQL语句修改Mysql数据库字符集的方法
使用SQL语句修改Mysql数据库字符集的方法 修改库: alter database [$database] character set [$character_set] collate [$c ...
- 修改MYSQL数据库表的字符集
MySQL 乱码的根源是的 MySQL 字符集设置不当的问题,本文汇总了有关查看 MySQL 字符集的命令.包括查看 MySQL 数据库服务器字符集.查看 MySQL 数据库字符集,以及数据表和字段的 ...
- MySQL数据库字符集由utf8修改为utf8mb4一例
对于mysql 5.5 而言,如果不设定字符集,mysql默认的字符集是 latin1 拉丁文字符集: 为了统一管理和应用开发的方便,一般都会统一将操作系统,客户端,数据库各方面的字符集都设置为 ut ...
- 修改mysql默认字符集的方法
+--------------------------+---------------------------------+ | Variable_name | Value | +---------- ...
- 修改mysql默认字符集的方案
mysql默认字符集能否进行修改呢?答案是肯定的,下面就将教您两种修改mysql默认字符集的方法,希望对您学习mysql默认字符集方面能有所启迪. (1) 最简单的修改方法,就是修改mysql的my. ...
- 查看和设置MySQL数据库字符集(转)
查看和设置MySQL数据库字符集作者:scorpio 2008-01-21 10:05:17 标签: 杂谈 Liunx下修改MySQL字符集:1.查找MySQL的cnf文件的位置find / -ina ...
- Linux下修改MySQL数据库字符编码为UTF-8解决中文乱码
由于MySQL编码原因会导致数据库出现乱码. 解决办法: 修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 &g ...
- centos 下修改mysql 默认字符集
解决办法: CentOS 7下修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 mysql -u root - ...
- Mysql数据库字符集问题
修改mysql数据库的默认编码方式 修改my.ini文件 加上 default-character-set=gb2312 设定数据库字符集 alter database da_name default ...
随机推荐
- Leetcode 78. Subsets (backtracking) 90 subset
using prev class Solution { List<List<Integer>> res = new ArrayList<List<Integer&g ...
- Recent plan + Summary (two weeks)
Plan: Homework: B365 (next week) B392, B335 Interview: Friday, do the assignment Thursday Summary: I ...
- java 使用mongodb
1.先连接你的mongodb 看连接是否有问题,代码 public class MongoDB2 { private static MongoDatabase mongoDatabase = null ...
- Maven安装以及使用
maven安装 1.解压maven安装包 2.右键“计算机”,选择“属性”,之后点击“高级系统设置”,点击“环境变量”,来设置环境变量. 3. 新建系统变量 MAVEN_HOME 变量值:C:\apa ...
- linux 内存地址空间管理 mm_struct
http://blog.csdn.net/yusiguyuan/article/details/39520933 Linux对于内存的管理涉及到非常多的方面,这篇文章首先从对进程虚拟地址空间的管理说起 ...
- LeetCode(Add Two Numbers)
一.题目要求 You are given two non-empty linked lists representing two non-negative integers. The digits a ...
- path、classpath理解
path.classpath最常见的场景:环境变量配置 path环境变量:设置path的作用是让操作系统可以找到JDK命令(指定了JDK命令搜索路径):path环境变量原来Windows里面就有,只需 ...
- CXF报错[1 counts of IllegalAnnotationExceptions]and[Two classes have the same XML type name]and[Use @XmlType.name and @XmlType.namespace to assign different names to them]
启动时CXF报错如下: Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalA ...
- 前端小记4——高性能mobile web开发
1.高性能CSS3动画 与PC端场景需要相比,移动web端需要考虑的因素也相对复杂,重点考虑:流量.功耗与流畅度.在pc端上考虑更多的是流畅度,而mobile web中需要考虑网络流量的使用和耗电情况 ...
- Unexpected token o in JSON at position 1
ajax返回的数据已经是object格式,无需再使用“var newjsonObj = JSON.parse(jsonObj)” 进行转换.