修改MySQL字符编码
Posted BaldWinf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了修改MySQL字符编码相关的知识,希望对你有一定的参考价值。
windows环境
查看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>
设置mysql数据库编码
在mysql安装目录下,将 my-default.ini 复制一分保存为 my.ini
做如下修改
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
[client]
default-character-set = utf8
然后删除你当前的mysql服务,再重新注册mysql服务。
注:一定要以管理员的权限运行dos命令,且cd到mysql安装目录下的bin目录,再注册mysql服务
我将mysql的路径配置到了path环境变量中,如果不是,需要到程序目录下执行命令
C:\\WINDOWS\\system32>net stop mysql
mysql 服务正在停止.
mysql 服务已成功停止。
C:\\Windows\\system32>sc delete mysql
[SC] DeleteService 成功
C:\\WINDOWS\\system32>mysqld -install mysql --defaults-file="D:\\develops\\mysql-5.7.17-winx64\\my.ini"
Service successfully installed.
C:\\WINDOWS\\system32>net start mysql
mysql 服务正在启动 .
mysql 服务已经启动成功。
C:\\WINDOWS\\system32>
再次查看
C:\\WINDOWS\\system32>mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \\g.
Your MySQL connection id is 3
Server version: 5.7.17 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 | D:\\develops\\mysql-5.7.17-winx64\\share\\charsets\\ |
+--------------------------+-------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)
mysql>
修改数据库实例编码
修改是永久的,重启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>
查看字符集sql语句
查看mysql数据库默认编码
show variables like '%char%';
查看数据库实例编码
show create database mysql_db;
修改全局字符集sql语句
注:下面方式修改字符集 是临时的,mysql重启后无效
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;
Linux环境
查看默认编码
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
设置默认编码
-
备份配置文件
sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf_2 sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
-
修改内容
[mysqld] character-set-server=utf8 collation-server=utf8_general_ci [client] default-character-set = utf8
-
重启mysql
service mysql restart
-
查看字符集
mysql> show variables like "%char%"; +--------------------------+----------------------------+ | 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/ | +--------------------------+----------------------------+
作者:书霖
wx:yy82801
以上是关于修改MySQL字符编码的主要内容,如果未能解决你的问题,请参考以下文章