mysql修改字符集

mysql> show variables like 'character%';
+--------------------------+-------------------------------------------------+
| Variable_name            | Value                                           |
+--------------------------+-------------------------------------------------+
| character_set_client     | gbk                                             |
| character_set_connection | gbk                                             |
| character_set_database   | latin1                                          |
| character_set_filesystem | binary                                          |
| character_set_results    | gbk                                             |
| character_set_server     | latin1                                          |
| character_set_system     | utf8                                            |
| character_sets_dir       | E:\0Develop\mysql-5.7.15-winx64\share\charsets\ |
+--------------------------+-------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)

mysql>

可以看出database和server都采用了latin1编码,不支持中文,我们需要把它修改为utf-8。

除了这两个,其它的如下

| 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       | E:\0Develop\mysql-5.7.15-winx64\share\charsets\ 

设置mysql数据库编码

在mysql安装目录下,将 my-default.ini 复制一分保存为 my.ini

做如下修改

主要看红色框里的内容

[mysqld]
character-set-server=utf8 
collation-server=utf8_general_ci 

[mysql]
default-character-set = utf8

[mysql.server]
default-character-set = utf8


[mysqld_safe]
default-character-set = utf8


[client]
default-character-set = utf8

然后删除你当前的mysql服务,再重新注册mysql服务

注:一定要以管理员的权限运行dos命令,且cd到mysql安装目录下的bin目录,再注册mysql服务

C:\Windows\system32>sc delete mysql
[SC] DeleteService 成功

C:\Windows\system32>e: cd
E:\>cd E:\0Develop\mysql-5.7.15-winx64\bin

E:\0Develop\mysql-5.7.15-winx64\bin>mysqld --install mysql --defaults-file="E:\0Develop\mysql-5.7.15-winx64\my.ini"
The service already exists!
The current server installed: E:\0Develop\mysql-5.7.15-winx64\bin\mysqld MySQL

E:\0Develop\mysql-5.7.15-winx64\bin>net stop mysql
MySQL 服务正在停止..服务名无效。
请键入 NET HELPMSG 2185 以获得更多的帮助。

E:\0Develop\mysql-5.7.15-winx64\bin>mysqld --install mysql --defaults-file="E:\0Develop\mysql-5.7.15-winx64\my.ini"
Service successfully installed.
E:\0Develop\mysql-5.7.15-winx64\bin>

结果

启动服务,查看编码

C:\Windows\System32>net start mysql
mysql 服务正在启动 .
mysql 服务已经启动成功。


C:\Windows\System32>mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.15 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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.

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       | E:\0Develop\mysql-5.7.15-winx64\share\charsets\ |
+--------------------------+-------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)

mysql>

数据库实例编码设置

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

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)

mysql>

//查看MySQL数据库服务器和数据库字符集 show variables like '%char%';

//查看表的字符集 show table status from mysql_db like '%tb_name%'

//查看表中所有列的字符集 show full columns from tb_name;

//查看mysql所支持的字符集 show charset;

==修改字符集==

//修改全局字符集

set character_set_connection=gb2312;

set character_set_database=gb2312;

set character_set_results=gb2312;

set character_set_server=gb2312;

set character_set_system=gb2312;

set collation_connection=gb2312;

set collation_database=gb2312;

set collation_server=gb2312;

//修改表的字符集 ALTER TABLE tb_name CONVERT TO CHARACTER SET gb2312;

//修改字段字符集 alter table tb_name modify column tb_column varchar(30) character set gb2312 not null;

相关推荐
Ai 编码助手21 分钟前
MySQL中distinct与group by之间的性能进行比较
数据库·mysql
陈燚_重生之又为程序员36 分钟前
基于梧桐数据库的实时数据分析解决方案
数据库·数据挖掘·数据分析
caridle38 分钟前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
白云如幻39 分钟前
MySQL排序查询
数据库·mysql
萧鼎41 分钟前
Python并发编程库:Asyncio的异步编程实战
开发语言·数据库·python·异步
^velpro^43 分钟前
数据库连接池的创建
java·开发语言·数据库
苹果醋31 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
荒川之神1 小时前
ORACLE _11G_R2_ASM 常用命令
数据库·oracle
IT培训中心-竺老师1 小时前
Oracle 23AI创建示例库
数据库·oracle
小白学大数据1 小时前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫