search
HomeDatabaseMysql TutorialMysql中文乱码以及导出为sql语句和Excel问题解决方法[图文]_MySQL

bitsCN.com

一、导出数据。

先说明一下自己的环境:Mac OS X 10.8.3, MySQL Community Server 5.6.10, MySQL Workbench 5.2.47。

我想把本机数据库内的数据迁移到另一台机器上,于是使用Workbench中自带的import/export功能,其实就是调用mysqldump。不幸的是,出现了版本不一致的错误。

错误没治了,最终找到解决方案,可以指定mysql的mysqldump,路径为:/usr/local/mysql/bin/mysqldump,这样是把数据导出为sql语句的insert语句。

由于需要是把数据导出为excel,所以通过mysql控制台使用select语句把数据导出到excel文件中。

下面先介绍怎么导出为excel文件,然后介绍怎么导出为insert语句。

1、通过终端操作。

1 cd /usr/local/mysql/bin/<p></p> <p>2、到达bin目录后,可以ls -l命令看看当前目录有哪些程序可以用,这里先用mysql,命令格式为:</p> <p>mysql -h主机IP -u用户名 -p密码</p> <p>如:</p> <p></p><pre class="brush:php;toolbar:false">1 ./mysql -hlocalhost -uroot -p123456<p></p> <p>注意前面加的"./"。</p> <p>这时就进入mysql命令控制台,终端上显示为:</p> <p><img src="/static/imghwm/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c140719/1405L112406250-52419.jpg" class="lazy" alt=""></p> <p>3、然后通过show databases命令查看当前的所有数据库,使用use命令选择进入某个数据库,<strong>注意每个命令都要以英文分号“;”结束。</strong></p> <p><strong><img src="/static/imghwm/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c140719/1405L11244DP-B4Z.jpg" class="lazy" alt=""></strong></p> <p>4、使用sql语句导出需要的数据,sql语句不限于单个表的查询。由于我的数据库编码是utf8格式,而office默认的编码则是gb2312,所以当某个字段中包含中文时,导出到excel后,中文内容是会乱码的,此时需要convert转换编码,具体使用方式:</p> <p><img src="/static/imghwm/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c140719/1405L112493K0-L115.jpg" class="lazy" alt=""></p> <p>我试着把文件保存到桌面,但始终提示没有权限,应该是和用户有关吧,无视了。当使用“./”这个路径保存时,实际是保存到了/usr/local/mysql/data下面。打开看看,哟西,不乱码了。</p> <p>5、下面是把数据导出为sql的insert语句。</p> <p>使用mysqldump命令,可以指定是单个表还是整个数据库导出。</p> <p>打开终端,定位到/usr/local/mysql/bin,使用这个目录下的mysqldump。</p> <p><strong>导出单个表:</strong></p> <p>命令格式为:</p> <p class="p1">mysqldump -u用户名 -p密码 -h主机地址 数据库名 表名 > 导出文件存储路径</p> <p class="p1">例如:</p> <p></p><pre class="brush:php;toolbar:false">/usr/local/mysql/bin/mysqldump -uroot -p123456 -hlocalhost -t --extended-insert=false --default-character-set=utf8 SpiderBBSDB Catalog > /Users/ethan/Desktop/Catalog.sql<p></p> <p>其中用到了几个参数,简单说明一下:</p> <p><strong>-t:</strong>等同于--no-create-info,只导出数据,而不添加CREATE TABLE 语句。默认导出的文件中也有create table语句。</p> <p><strong>--extended-insert:</strong>使用具有多个VALUES列的INSERT语法,也就是传说中一次插入多条数据的INSERT句式。这样使导出文件更小,并加速导入时的速度,但是有可能sql语句会有长度限制,所以我并不推荐此种方式,比如我某个表中有500W条数据,难保能用一条insert语句可以执行完毕。此选项默认为打开状态,把他置为false,就是一条数据一个insert语句了。</p> <p><strong>--default-character-set:</strong>设置默认字符集,由于我的数据库和表均是设定为utf8编码格式,当不设置此选项时,导出的中文是乱码,奇怪的是官方说明中,说这个选项的默认值是utf8,表示不解。</p> <p><strong>导出整个数据库:</strong></p> <p></p><pre class="brush:php;toolbar:false">/usr/local/mysql/bin/mysqldump -uroot -p123456 -hlocalhost -t --extended-insert=false --default-character-set=utf8 SpiderBBSDB > /Users/ethan/Desktop/SpiderBBSDB.sql<p></p> <p>二、导入数据。 </p> <p>有导出就有导入。上面第5步导出的sql文件,可以直接在mysql workbench中执行,也可以使用mysqldump导入,这里说明一下如何使用mysqldump导入:</p> /usr/local/mysql/bin/mysqldump -uroot -p123456 -hlocalhost --default-character-set=utf8 SpiderBBSDB 三、关于java连接mysql写入中文乱码。  <p>关于这个中文乱码问题,着实折腾了我好久好久。一开始就百度谷歌bing,网上大多复制粘贴的答案,在这里记录一下自己的情况,希望同路人不再走弯路。</p> <p>其实我的修改很简单,把数据库的编码改为utf-8,在新建表时,把表的默认编码也改为utf-8,就可以了。就这么个小小的改动,让我足足折腾了一个通宵,表示有解决问题强迫症,问题不解决真的睡不着,唉~~~</p> <p><img src="/static/imghwm/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c140719/1405L112535940-WL9.jpg" class="lazy" alt=""></p> <p><img src="/static/imghwm/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c140719/1405L1125F310-92S6.jpg" class="lazy" alt=""></p> <p><img src="/static/imghwm/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c140719/1405L11261250-105O0.jpg" class="lazy" alt=""></p> <p><strong>四、总结。 </strong></p> <p>似乎很多领导做报告都喜欢加个总结,说上一堆废话,虽然回回都听不懂,但感觉很厉害的样子。于是我也加一个总结:中文乱码真特么折腾人,这些年跟你斗争了好多回了,好了,总结完毕。</p>
bitsCN.com
Statement
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
MySQL: An Introduction to the World's Most Popular DatabaseMySQL: An Introduction to the World's Most Popular DatabaseApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

The Importance of MySQL: Data Storage and ManagementThe Importance of MySQL: Data Storage and ManagementApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

Why Use MySQL? Benefits and AdvantagesWhy Use MySQL? Benefits and AdvantagesApr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Apr 12, 2025 am 12:16 AM

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

What are common causes of poor MySQL query performance?What are common causes of poor MySQL query performance?Apr 12, 2025 am 12:11 AM

The main reasons for poor MySQL query performance include not using indexes, wrong execution plan selection by the query optimizer, unreasonable table design, excessive data volume and lock competition. 1. No index causes slow querying, and adding indexes can significantly improve performance. 2. Use the EXPLAIN command to analyze the query plan and find out the optimizer error. 3. Reconstructing the table structure and optimizing JOIN conditions can improve table design problems. 4. When the data volume is large, partitioning and table division strategies are adopted. 5. In a high concurrency environment, optimizing transactions and locking strategies can reduce lock competition.

When should you use a composite index versus multiple single-column indexes?When should you use a composite index versus multiple single-column indexes?Apr 11, 2025 am 12:06 AM

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)Apr 10, 2025 am 09:36 AM

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL: Essential Skills for DevelopersMySQL and SQL: Essential Skills for DevelopersApr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use