win10 系统解决mysql中文乱码问题

问题:

向mysql 数据库插入数据是,出现中文乱码(中文均显示为‘??’)

原因:

mysql 默认的字符集是latin1,所以我么需要改为ut8编码才可以

解决:

1、以管理员权限运行cmd窗口

2、查看当前字符集

>net start mysql

启动mysql服务

>mysql

进入mysql运行环境

>show variables like 'character%';

查看当前字符集编码情况

 

其中,character_set_client为客户端编码方式;

character_set_connection为建立连接使用的编码;

character_set_database数据库的编码;

character_set_results结果集的编码;

character_set_server数据库服务器的编码;

只要保证以上四个采用的编码方式一样,就不会出现乱码问题。

3、修改数据库编码格式

>select @@basedir;

在mysql环境中查看mysql的安装位置

 

找到这个安装位置,下图为安装位置下的文件

 

本机的mysql是通过解压安装的,开始时只有my-default.ini文件,无my.ini文件

这时复制一份my-default.init并重命名为my.ini

打开my.ini文件

在[mysqld] 下添加character-set-server=utf8

在[client] 下添加:default-character-set=utf8

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the

# *** default location during install, and will be replaced if you

# *** upgrade to a newer version of MySQL.

 

[mysqld]

character-set-server=utf8

# Remove leading # and set to the amount of RAM for the most important data

# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.

# innodb_buffer_pool_size = 128M

 

# Remove leading # to turn on a very important data integrity option: logging

# changes to the binary log between backups.

# log_bin

 

# These are commonly set, remove the # and set as required.

# basedir = .....

# datadir = .....

# port = .....

# server_id = .....

 

 

# Remove leading # to set options mainly useful for reporting servers.

# The server defaults are faster for transactions and fast SELECTs.

# Adjust sizes as needed, experiment to find the optimal values.

# join_buffer_size = 128M

# sort_buffer_size = 2M

# read_rnd_buffer_size = 2M

 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

 

[client]

port=3306

default-character-set=utf8
my.ini

保存文件

4、重新在mysql环境中查看字符编码

 

发现这时的字符编码已经改为utf8

5、如果更改字符编码之前已经有创建的数据表,可以通过如下语句,更改已经创建数据表的字符格式

mysql> alter table testapp_article convert to character set utf8;

也可以在创建数据表的时候指定使用utf8编码,方法是在创建语句后加上character set = utf8

如:

create table test_table (
id int auto_increment,
title text,
content text,
posted_on datetime,
primary key (id)
) character set = utf8;

参考:

https://www.cnblogs.com/amy420/p/7218891.html

https://www.cnblogs.com/lijavasy/p/10234563.html

原文地址:https://www.cnblogs.com/eye-like/p/11718756.html