- 工信部备案号 滇ICP备05000110号-1
- 滇公安备案 滇53010302000111
- 增值电信业务经营许可证 B1.B2-20181647、滇B1.B2-20190004
- 云南互联网协会理事单位
- 安全联盟认证网站身份V标记
- 域名注册服务机构许可:滇D3-20230001
- 代理域名注册服务机构:新网数码
show variables like 'character%';
# 或者
show variables like '%char%';
MySQL5.7中执行如下:
MySQL 5.7 默认的客户端和服务器都用了 latin1 ,不支持中文,保存中文会报错。示例如下:
ysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
mysql> create database dbdemo1;
Query OK, 1 row affected (0.00 sec)
mysql> use dbdemo1;
Database changed
mysql> create table t_emp(id int,name varchar(15));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t_emp(id,name) values(1001,'ZhangSan');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t_emp(id,name) values(1002,'李四');
ERROR 1366 (HY000): Incorrect string value: '\\\\\\\\\\\\\\\\xE6\\\\\\\\\\\\\\\\x9D\\\\\\\\\\\\\\\\x8E\\\\\\\\\\\\\\\\xE5\\\\\\\\\\\\\\\\x9B\\\\\\\\\\\\\\\\x9B' for column 'name' at row 1
因为默认情况下,创建表使用的是 latin1
mysql> show create table t_emp;
+-------+----------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------+
| t_emp | CREATE TABLE `t_emp` (
`id` int(11) DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
1、编辑配置文件,在mysqld
模块下添加如下内容
# vim /etc/my.cnf
[mysqld]
...
character_set_server=utf8
collation-server=utf8_general_ci
2、重启数据库
# systemctl restart mysqld
注意:已存在的原库、原表的设定不会发生变化,参数修改只是对新建的数据库生效。
3、修改已有库&表字符集的变更
-- 修改已创建数据库的字符集
alter database dbtest1 character set 'utf8';
-- 修改已创建数据表的字符集
alter table t_emp convert to character set 'utf8';
范例:
# 修改已经创建数据库的字符集
-- 修改之前查看字符集
mysql> show create database dbdemo1;
+----------+--------------------------------------------------------------------+
| Database | Create Database |
+----------+--------------------------------------------------------------------+
| dbdemo1 | CREATE DATABASE `dbdemo1` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+--------------------------------------------------------------------+
1 row in set (0.01 sec)
-- 进行修改
mysql> alter database dbdemo1 character set 'utf8';
Query OK, 1 row affected (0.00 sec)
-- 修改之后查看字符集
mysql> show create database dbdemo1;
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| dbdemo1 | CREATE DATABASE `dbdemo1` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.00 sec)
# 修改已经创建数据表的字符集
-- 修改之前查看字符集
mysql> show create table t_emp;
+-------+------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------+
| t_emp | CREATE TABLE `t_emp` (
`id` int(11) DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
-- 进行修改
mysql> alter table t_emp convert to character set 'utf8';
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
-- 修改之后查看字符集
mysql> show create table t_emp;
+-------+----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------+
| t_emp | CREATE TABLE `t_emp` (
`id` int(11) DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
注意:原有的数据如果是用非'utf8'编码的话,数据本身编码不会发生改变。已有数据需要导出或删除,然后重新插入。
蓝队云官网上拥有完善的技术支持库可供参考,大家可自行查阅,更多技术问题,可以直接咨询。同时,蓝队云整理了运维必备的工具包免费分享给大家使用,需要的朋友可以直接咨询。
更多技术知识,蓝队云期待与你一起探索。
售前咨询
售后咨询
备案咨询
二维码
TOP