MySQL(导入sql文件)

传文件省略...(从windows传到linux)

改编码格式

为什么不在windows里面修改呢?因为windows打开发现根本打不开直接就卡住了数据过多了(4百万数据(不信可以自己试一下))

bash 复制代码
[root@CentOS8 ~]# file order_info.sql
order_info.sql: UTF-8 Unicode text

iconv -f utf8 -t gb2312 order_info.sql -o order_info1.sql
-f 是文件原编码 
-t是文件要转换编码
-o是输出
[root@CentOS8 ~]# file order_info1.sql
order_info1.sql: ISO-8859 text
#这里根据自己需求修改。

#查看当前操作系统编码和语言
[root@CentOS8 ~]# echo $LANG
en_US.UTF-8
#如果想修改
LANG=zh_CN.UTF-8

#为了避免出错都改成utf8就行 utf8mb4是正宗的utf8字符集,使用1~4个字节表示字符。
mysql> show variables like 'character_set_client';
+----------------------+---------+
| Variable_name        | Value   |
+----------------------+---------+
| character_set_client | utf8mb4 |
+----------------------+---------+
1 row in set (0.00 sec)
 
mysql> SHOW VARIABLES LIKE 'character_set_connection';
+--------------------------+---------+
| Variable_name            | Value   |
+--------------------------+---------+
| character_set_connection | utf8mb4 |
+--------------------------+---------+
1 row in set (0.01 sec)
 
mysql> SHOW VARIABLES LIKE 'character_set_results';
+-----------------------+---------+
| Variable_name         | Value   |
+-----------------------+---------+
| character_set_results | utf8mb4 |
+-----------------------+---------+
1 row in set (0.00 sec)

#可以把三个参数(character_set_client、character_set_connection、character_set_results)设置成一样。
SET NAMES utf8mb4;

改文件格式

bash 复制代码
yum -y install dos2unix   #dos(windows) -> unix(linux)

[root@CentOS8 ~]# file order_info.sql
order_info.sql: UTF-8 Unicode text, with CRLF line terminators
You have new mail in /var/spool/mail/root

[root@CentOS8 ~]# dos2unix order_info.sql
dos2unix: converting file order_info.sql to Unix format...
[root@CentOS8 ~]# file order_info.sql
order_info.sql: UTF-8 Unicode text

导入

sql 复制代码
mysql> source /root/order_info.sql

检查

sql 复制代码
mysql> select count(*) from order_info;
+----------+
| count(*) |
+----------+
|  1036109 |
+----------+
1 row in set (0.28 sec)

假如表的编码格式是lat1(意外情况)

sql 复制代码
mysql> show create table DICT_REGION_CITY;
+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table            | Create Table                                                                                                                                                                                                                  |
+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| DICT_REGION_CITY | CREATE TABLE `DICT_REGION_CITY` (
  `code` varchar(36) NOT NULL,
  `name` varchar(100) DEFAULT NULL,
  `parent_code` varchar(36) DEFAULT NULL,
  `LEVEL_CODE` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> alter table DICT_REGION_CITY default character set utf8;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table DICT_REGION_CITY change code code varchar(36) character set utf8;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table DICT_REGION_CITY change name name varchar(36) character set utf8;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table DICT_REGION_CITY change parent_code parent_code varchar(36) character set utf8;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table DICT_REGION_CITY change name name varchar(100) character set utf8;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table DICT_REGION_CITY change LEVEL_CODE LEVEL_CODE varchar(100) character set utf8;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table DICT_REGION_CITY;
+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table            | Create Table                                                                                                                                                                                                                     |
+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| DICT_REGION_CITY | CREATE TABLE `DICT_REGION_CITY` (
  `code` varchar(36) DEFAULT NULL,
  `name` varchar(100) DEFAULT NULL,
  `parent_code` varchar(36) DEFAULT NULL,
  `LEVEL_CODE` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

再接着导入就可以啦!

相关推荐
未济1 天前
linux 配置环境变量
linux
傲世仙尊1 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
虎头金猫1 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
小白男神1 天前
MySQL进阶学习四(存储过程、游标、触发器)
mysql
这个DBA有点耶1 天前
MVCC深入:Read View、版本链与快照读——InnoDB并发控制的内核
数据库·mysql·架构
DBA_G1 天前
从地面到云霄:GBase数据库在民航三大场景的落地实践
数据库
自由能燃气设备1 天前
商用全预混低氮冷凝锅炉免费方案vs付费方案对比+选型避坑指南
大数据·数据库·人工智能
科创致远1 天前
科创致远 ESOP 系统核心效能与实战价值展示
大数据·数据库·人工智能·精益工程
_艾伦 耶格尔.1 天前
进程间通信
linux
AI职业加油站1 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展