Home Database Mysql Tutorial 总结下MYSQL编码转换的问题latin1转utf8_MySQL

总结下MYSQL编码转换的问题latin1转utf8_MySQL

Jun 01, 2016 pm 01:47 PM
php website yes

bitsCN.com

黑面

昨天碰到个PHP网站,数据库是MYSQL5.1的,进了网站的phpmyadmin管理后发现表内中文全部显示乱码,导出后本地导入一样是乱码,无法查看所需要的信息,乱码一般情况不用说都知道多数是编码的问题,查看了下目标库的编码为latin1,估计网站程序是GBK的。。。直接备份下载,下载到本地需要转换下编码,可惜以前没搞过,网上搜索了一堆资料测试。。。具体有这几种我给需要的朋友总结下,就不用到处搜索了

一:

1、备份数据库

mysqldump --default-character-set=latin1 --create-options=false --set-charset=false -u root -p 数据库名称 > E:ack.sql

2、创建新数据库

CREATE DATABASE 数据库名称 CHARACTER SET utf8 COLLATE utf8_general_ci;

3、导入数据

mysql -u root -p --default-character-set=gbk 数据库名称 ack.sql

 

二:

步骤一 命令行执行:mysqldump --opt -hlocalhost -uroot -p*** --default-character-set=lantin1 dbname > /usr/local/dbname.sql

步骤二 将 dbname.sql文件中的create table语句的CHARSET=latin1改为CHARSET=utf8

步骤三 在dbname.sql文件中的insert语句之前加一条set names utf8;

步骤四 将dbname.sql转码为utf-8格式,建议使用UltraEditor,可以直接使用该编辑器的转换->ASCII到UTF-8(Unicode编辑),或者将文件另存为UTF-8(无BOM)格式

步骤五 命令行执行:mysql -hlocalhost -uroot -p*** --default-character-set=utf8 new_dbname

总结:这种方法有个致命之处就是当数据中有大量中文字符和其他特殊符号字符时,很有可能导致在[步骤五]时报错导致无法正常导入数据,如果数据库比较大可以分别对每张表执行上述步骤

 

三:

为了解决第一种方法中总结时说到的问题,在网上苦苦查找了一天资料才东拼西凑的搞出一个比较稳妥的解决方法

步骤一 将待导出的数据表的表结构导出(可以用Phpmyadmin、mysqldump等,很简单就不说了),然后将导出的create table语句的CHARSET=latin1改为CHARSET=utf8,在目标库newdbname中执行该create table语句把表结构建好,接下来开始导出-导入数据

步骤二 命令行:进入mysql命令行下,mysql -hlocalhost -uroot -p*** dbname

步骤三 执行SQL select * from tbname into outfile /usr/local/tbname.sql;

步骤四 将tbname.sql转码为utf-8格式,建议使用UltraEditor,可以直接使用该编辑器的转换->ASCII到UTF-8(Unicode编辑),或者将文件另存为UTF-8(无BOM)格式

步骤五 在mysql命令行下执行语句 set character_set_database=utf8;  注:设置mysql的环境变量,这样mysql在下一步读取sql文件时将以utf8的形式去解释该文件内容

步骤六 在mysql命令行下执行语句 load data infile tbname.sql into table newdbname.tbname;

注意:千万不要忘了第四步

采用第二种方法,所有数据均正常导入,且格式转换成功没有乱码

 

四:

mysql数据转换编码
[root@piglet home]# mysqldump -u root -p --default-character-set=latin1 --set-charset=utf8 --skip-opt aiyomama_cn > /home/sites/savemoney.sql
Enter password:

 

五:

下回来的是latin1乱码的文件,因此,本机也架起Mysql5.0的环境
2. 然后新建一个空数据库,编码为UTF8,用下回来的sql文件导入
3. 再用phpmyadmin导出来,这样编码就成了UTF8
4. 把导出的sql文件中所有的DEFAULT CHARSET=latin1替换成DEFAULT CHARSET=utf8
5. 删除原来的latin1编码的数据库,导入这个UTF8编码的sql文件
完成:)

补充一下重点:
在执行完第2步的时候,用PHPMYADMIN打开数据库看看
里面所有的表和字段都还是LATIN1,没关系,继续执行第3步
执行完第3步,可以用文本编辑器打开SQL文件看看,换UTF8格式查看
这时,表和字段的内容就是UTF8了!

以上五种方法我都试验了一遍,可能是数据库实在太大,有的方法完全不行,有的可以但只能转换到一半也出错停止,无法转换完全部,最后在不懈努力之下找到个编码转换的PHP小脚本文件,使用后全部库转换正常,看来还是工具来的方便啊,手工忙了半天都没解决。。。不过还有点就是不管遇到什么问题,搜索引擎一般情况下都能帮助你解决问题,只要你愿意去花时间自己解决问题。

 

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Optimizing website SEO: practice of pseudo-static hiding php suffix Optimizing website SEO: practice of pseudo-static hiding php suffix Mar 07, 2024 pm 12:27 PM

As we all know, optimizing the SEO of a website is a very important part of website operation. The default URLs of dynamic web systems (such as PHP) used by many websites have extensions (.php, .html, etc.), which will affect the SEO effect of the website. In order to improve the optimization effect of the website, a common practice is to change the dynamic URL to a pseudo-static URL to hide the extension name and improve the user experience and search engine ranking of the website. This article will take "pseudo-static hidden php suffix" as the theme, introduce how to achieve this optimization in PHP websites, and

What is the development method of Empire CMS template? What is the development method of Empire CMS template? Apr 17, 2024 am 12:09 AM

Empire cms template development methods include: 1. Understand the template structure; 2. Modify the template code; 3. Use tags and variables; 4. Create custom functions; 5. Use CSS and JS; 6. Use template modules; 7. Debugging and test.

Which websites are made with php Which websites are made with php Aug 11, 2023 am 10:55 AM

Websites made with PHP include: 1. Facebook, one of the world's largest social media platforms; 2. Wikipedia, an online encyclopedia edited by users around the world; 3. WordPress, a popular open source blog and content management system; 4. Slack, a widely used team collaboration tool; 5. Magento, a powerful open source e-commerce platform; 6. Etsy, an online marketplace for handicrafts and independent designers.

Which websites are developed using php Which websites are developed using php Aug 11, 2023 am 11:03 AM

Websites developed with PHP include: 1. Facebook, one of the world's largest social media platforms; 2. Wikipedia, the world's leading online encyclopedia; 3. WordPress, a widely used open source blog and content management system; 4. Twitter, a popular social media platform; 5. Yahoo, a world-renowned portal; 6. Flickr, an online image hosting and sharing platform; 7. LinkedIn, a professional social media platform.

Build a simple website in php Build a simple website in php Mar 22, 2024 pm 03:04 PM

Building a PHP website involves the following steps: 1. Prepare the environment, install the PHP development environment and create the website root directory. 2. Create the basic file structure, including the homepage index.php and other required files. 3. Write home page content, use HTML and PHP to build page structure and dynamically generate content. 4. Add other pages and create other PHP files to process page content. 5. Connect to the database (optional), use PHP to connect to the database and operate the data. 6. Design styles and interactive effects, and use CSS and JavaScript to enhance the beauty and interactivity of the website. 7. Deploy the website and upload the website files to the server or

How to protect your PHP website using secure coding practices? How to protect your PHP website using secure coding practices? Aug 20, 2023 pm 06:05 PM

How to protect your PHP website using secure coding practices? With the popularity and development of the Internet, more and more websites use PHP as the development language. However, PHP's flexibility and ease of use also make it vulnerable to various security threats. Therefore, when developing a PHP website, it is essential to apply secure coding practices to protect the website from possible attacks. This article will introduce some ways to protect your PHP website using secure coding practices and provide corresponding code examples. Input Validation and Filtering Input Validation and Filtering is the key to protecting PHP websites

How to use encryption algorithm to protect user data of PHP website? How to use encryption algorithm to protect user data of PHP website? Aug 19, 2023 pm 04:00 PM

How to use encryption algorithm to protect user data of PHP website? With the rapid development of the Internet, user data protection of websites has become more and more important. In PHP development, we can use encryption algorithms to protect the security of user data. This article will introduce some commonly used encryption algorithms and how to use them in PHP websites to encrypt user data. 1. Selection of encryption algorithms For PHP websites, we can choose the following commonly used encryption algorithms to protect the security of user data: 1. Symmetric encryption algorithm: This algorithm uses the same encryption algorithm.

PHP website security and protection measures PHP website security and protection measures May 04, 2024 pm 10:48 PM

PHP website security measures include: Preventing SQL injection: using prepared statements or escaping user input. Prevent XSS: Escape user input. Prevent CSRF: Use CSRF tokens. Prevent buffer overflow: Set the maximum input length. Stay updated, use security frameworks, enable firewalls, monitor websites, conduct security audits.

See all articles