菜鸟要飞

Mysql导入sql文件过大问题解决

2017-08-12  本文已影响22人  万总有点菜

方案一:修改Mysql配置

默认情况下,MySQL的最大允许包(max_allowed_packet)是1048576B(1MB)当向mysql导入的sql脚本文件超过1MB时,此时导入就会出现问题。
通过修改MySQL的my.ini配置文件,在my.ini文件中任意位置添加:
max_allowed_packet = 1024M 注:改数值根据实际情况调整
添加完后,重启Mysql服务。
可以通过命令行查询修改结果
show VARIABLES like '%max_allowed_packet%';


这个值为1073741824/1024/1024=1024M

附上mysql重启说明

启动方式
1、使用 service 启动:service mysqld start
2、使用 mysqld 脚本启动:/etc/inint.d/mysqld start
3、使用 safe_mysqld 启动:safe_mysqld&
停止
1、使用 service 启动:service mysqld stop
2、使用 mysqld 脚本启动:/etc/inint.d/mysqld stop
3、 mysqladmin shutdown
重启
1、使用 service 启动:service mysqld restart
2、使用 mysqld 脚本启动:/etc/inint.d/mysqld restart

方案二:通过代码读取sql文件

这种方法,要求sql中的每一行都是操作命令,如下


特别注意:
不能有其他的内容,比如说建表语句和注释(如果有可以在代码中跳过也行)


所以对应的被操作的表,需要存在数据库中,如果没有需要手动新建下表。

工程代码

首先创建一个Maven工程,pom.xml加入

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>

代码如下:

package com.lczyfz.data;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Data Clean
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.err.println("begin");
        long start = System.currentTimeMillis();
        String path = "E:\\matchedbidcontent_2016.sql";
//        String path = "E:\\test.txt";
        writeDataToMysql(path);
        System.err.print((System.currentTimeMillis() - start) / 1000);
    }

    private static void writeDataToMysql(String path) {
        //读取文件
        BufferedReader reader;
        Connection conn = null;
        Statement pst = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "123456");
            pst = conn.createStatement();
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "utf-8"));
            String line;
            int i = 0;
            while ((line = reader.readLine()) != null) {
//                System.out.println(line);
//                line = new String(line.getBytes("ISO-8859-1"),"UTF-8");
                pst.addBatch(line);
                 System.out.println("-----------------------");
                    System.out.println(line);
                    System.out.println("-----------------------");
                if (i % 100 == 0) {
                    System.out.println("执行了:" + i);
                    pst.executeBatch();
                }
                i += 1;
            }
            reader.close();
            // 执行批量更新
            pst.executeBatch();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pst != null) {
                    pst.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读