分享mysql中文乱码的解决办法
修改mysql配置解决乱码
首先:
用show variables like “%colla%”;show varables like “%char%”;这两条命令查看数据库与服务端的字符集设置
如果查看出来都是gbk2312,或 gbk,那么就只能支持简体中文,繁体和一些特殊符号是不能插入的,我们只有修改字符集为UTF-8,
修改方法如下:
用记事本或UitraEdit打开mysql数据库安装目录下的my.ini文件打开, 然后Ctrl+F搜索default-character-set,将后面的字符集修改为UTF8,注意要修改两个地方,一个事客户端的,一个是服务端的。
然后保存,重启mysql服务、、进去继续用show variables like “%colla%”;show varables like “%char%”;着两条语句查询一下字符集
数据表和连接部分的编码设置
设置数据库和数据表编码
要解决乱码问题,首先必须弄清楚数据库和数据表用什么编码。如果没有指明,将是默认的latin1。
用得最多的应该是这3种字符集 gb2312,gbk,utf8。
如何去指定数据库和数据表的字符集呢?下面也gbk为例:
【在MySQL Command Line Client创建数据库 】
1 2 3 4 5 |
|
其中后面的 TYPE = MYISAM CHARACTER SET gbk COLLATE gbk_chinese_ci;就是指定数据库的字符集,COLLATE (校勘),让mysql同时支持多种编码的数据库。
当然也可以通过如下指令修改数据库数据表的字符集:
1 |
|
前面已经设置了服务器、数据库和数据表的编码,那么数据库中的编码便都是gbk,中文可以存储进去。
但是如果你要通过执行insert或select等操作时,仍然会出现中文乱码问题,这是因为还没设置“连接(connection)”部分的编码,而insert、select等数据库操作都包含与数据库的连接动作。如果不信,你现在可以执行下面的sql文试一试:
1 |
|
设置连接编码
设置了服务器、数据库和数据表部分的编码,必须设置连接编码。连接编码设置如下:
1 2 3 |
|
设置好连接编码,下面便可以成功插入中文了:
1 2 |
|
其实,上面设置连接编码的三条命令可以简化为一条:
1 |
|
设置好了连接编码后,在select查询时,也能正确显示中文:
1 2 3 4 5 6 7 |
|
查询mysql中文记录乱码问题
我们这里以gb2312编码乱码为实例来介绍
如果你想使用gb2312编码,那么建议你使用latin1作为数据表的默认字符集,这样就能直接用中文在命令行工具中插入数据,并且可以直接显示出来.而不要使用gb2312或者gbk等字符集,如果担心查询排序等问题,可以使用binary属性约束,例如:
1 |
|
附:旧数据升级办法
以原来的字符集为latin1为例,升级成为utf8的字符集。
原来的表:old_table (default charset=latin1),新表:new_table(defaultcharset=utf8)。
第一步:导出旧数据
1 |
|
第二步:转换编码
1 |
|
在这里,假定原来的数据默认是gb2312编码。
第三步:导入
修改old.sql,增加一条sql语句: "SET NAMES utf8;",保存。
1 |
|
大功告成!!
mysql导入数据乱码解决
出现乱码语句:mysql -hlocalhost -uroot -proot test
正确的语句:mysql -hlocalhost -uroot -proot test
解决mysql导入导出数据乱码问题
首先要做的是要确定你导出数据的编码格式,使用mysqldump的时候需要加上--default-character-set=utf8,例如下面的代码:
1 |
|
那么导入数据的时候也要使用--default-character-set=utf8:
1 |
|
这样统一编码就解决了mysql数据迁移中的乱码问题了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

Excel spreadsheet is one of the office software that many people are using now. Some users, because their computer is Win11 system, so the English interface is displayed. They want to switch to the Chinese interface, but they don’t know how to operate it. To solve this problem, this issue The editor is here to answer the questions for all users. Let’s take a look at the content shared in today’s software tutorial. Tutorial for switching Excel to Chinese: 1. Enter the software and click the "File" option on the left side of the toolbar at the top of the page. 2. Select "options" from the options given below. 3. After entering the new interface, click the “language” option on the left

How to display Chinese characters correctly in PHPDompdf When using PHPDompdf to generate PDF files, it is a common challenge to encounter the problem of garbled Chinese characters. This is because the font library used by Dompdf by default does not contain Chinese character sets. In order to display Chinese characters correctly, we need to manually set the font of Dompdf and make sure to select a font that supports Chinese characters. Here are some specific steps and code examples to solve this problem: Step 1: Download the Chinese font file First, we need

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

Title: An effective way to repair Chinese garbled characters in PHPDompdf. When using PHPDompdf to generate PDF documents, garbled Chinese characters are a common problem. This problem usually stems from the fact that Dompdf does not support Chinese character sets by default, resulting in Chinese content not being displayed correctly. In order to solve this problem, we need to take some effective ways to fix the Chinese garbled problem of PHPDompdf. 1. Use custom font files. An effective way to solve the problem of Chinese garbled characters in Dompdf is to use

"WWE2K24" is a racing sports game created by Visual Concepts and was officially released on March 9, 2024. This game has been highly praised, and many players are eagerly interested in whether it will have a Chinese version. Unfortunately, so far, "WWE2K24" has not yet launched a Chinese language version. Will wwe2k24 be in Chinese? Answer: Chinese is not currently supported. The standard version of WWE2K24 in the Steam Chinese region is priced at 199 yuan, the deluxe version is 329 yuan, and the commemorative edition is 395 yuan. The game has relatively high configuration requirements, and there are certain standards in terms of processor, graphics card, or running memory. Official recommended configuration and minimum configuration introduction:

Tips for solving Chinese garbled characters written by PHP into txt files. With the rapid development of the Internet, PHP, as a widely used programming language, is used by more and more developers. In PHP development, it is often necessary to read and write text files, including txt files that write Chinese content. However, due to encoding format problems, sometimes the written Chinese will appear garbled. This article will introduce some techniques to solve the problem of Chinese garbled characters written into txt files by PHP, and provide specific code examples. Problem analysis in PHP, text

How to change Chinese to English in Google Chrome? Some friends want to set Google Chrome to English so that they can continuously improve their English during use. So how to set it to English? Google Chrome is Chinese by default. Below, I will show you how to set the language of Google Chrome to English. Let’s take a look. Setting steps: 1. Open [Google Chrome], as shown in the figure below. 2. Click the [three dots] menu in the upper right corner of the Google Chrome interface, as shown in the figure below. 3. After entering the menu page, find [Settings], as shown in the figure below. 4. After entering the settings page, click the [Language] option, as shown in the figure below. 5. Select [Add Language] in the language interface, as shown in the figure below.
