Laravel-播种大型SQL文件 [英] Laravel - seeding large SQL file

查看:78
本文介绍了Laravel-播种大型SQL文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在生产环境中运行数据库种子脚本时发生内存耗尽.

A memory exhaustion happens when I run my DB seed script in production.

下面是我的种子脚本.

class MembershipTableSeeder extends Seeder 
{
    public function run()
    {
        DB::table('members')->delete();

        foreach (range(1, 99) as $days){
            Members::create(array('membership_code' => 'test'.$days));
        }

        DB::unprepared(file_get_contents(app_path()."/database/seeds/members.sql"));
    }
}

所以我要做的是在种子脚本上添加无限制.

So what I did was add a no-limit on my seed script.

ini_set('memory_limit', '-1');

现在的问题是,当我运行脚本时,它将SQL脚本的内容(非常非常大)记录到终端中.

The problem now is that when I run the script it logs the output into the terminal the content of the SQL script (which is very, very big).

是否有一种很好的方法可以在不消耗大量内存的数据库种子中运行SQL转储?我现在所做的是手动运行它:

Is there a good way of running a SQL dump inside my DB seeds that doesn't consume much memory? What I did now was run it manually:

mysql -uuser -p db < script.sql

推荐答案

之所以会出现此问题,是因为使用Db :: unprepared时,它还会将查询记录到laravel.log文件中,从而在后台进行了比您认为更多的操作,这边你有记忆力.如果您没有运行安全模式,我将坚持执行以下控制台命令:

The problem happens because when using Db::unprepared it also logs the query to the laravel.log file, making in background much more actions then you think, from this side you have memory exhaust. If you are not running the safe mode I would stick to executing the console command like this:

exec("mysql -u ".\Config::get('database.mysql.user')." -p".\Config::get('database.mysql.password')." ".\Config::get('database.mysql.database')." < script.sql")

这篇关于Laravel-播种大型SQL文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆