生成一个mysql进程以使用节点导入数据库 [英] Spawn a mysql process to import a database using node

查看:59
本文介绍了生成一个mysql进程以使用节点导入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个节点脚本来自动导入.sql文件.我认为我误解了将参数传递给生成"的方式.这是我所拥有的:

I'm trying to write a node script to automatically import a .sql file. I think I am misunderstanding the way to pass arguments to 'spawn'. Here's what I have:

var spawn    = require('child_process').spawn;
var mysqlimport = spawn('/usr/local/bin/mysql', [
    '-u' + database.user,
    '-p' + database.password,
    '-h' + database.address,
    '--default-character-set=utf8',
    '--comments',
    '<"' + fileName + '"'
]);
mysqlimport
        .stdout
        .pipe(logFile)
        .on('data', function(data) {
           console.log(data); 
        })
        .on('finish', function() {
            console.log('finished')
        })
        .on('error', function(err) {
            console.log(err)
        });
mysqlimport.stderr.on('data', function(data) {
   console.log('stdout: ' + data);
});
mysqlimport.on('close', function(code) {
   console.log('closing code: ' + code);
});

我得到了错误

stdout: ERROR 1049 (42000): Unknown database '<"/users/user/dumps/sqlfile.sql"

如果我在导出时不使用-B标志,并指定数据库名称,则进行更改

if I don't use the -B flag when exporting, and specify the database name, changing

'<"' + fileName + '"'

databaseName + ' <"' + fileName + '"'

我收到另一个错误:

stdout: ERROR 1102 (42000): Incorrect database name ' theDatabase < "/users/user/dumps/sqlfile.sql"'

我知道我在指定参数时一定做错了什么,但是如何解决呢?关于产生子进程的节点文档令我感到困惑.感谢您的帮助!

I know I must be doing something wrong with specifying the argument, but how to fix it? The node documentation around spawning child processes is confusing to me. Thanks for your help!

推荐答案

感谢Paul F!您的解决方案有效.我从生成中删除了最后一个参数,并将其添加到我的代码中:

Thanks Paul F! your solution worked. I removed the last argument from the spawn and added to my code:

mysqlimport.stdin.write( '\\. /Users/user/dumps/' + fileName );
mysqlimport.stdin.end();

所以总的来说,像这样:

So In all it looks like:

var mysqlimport = spawn('/usr/local/bin/mysql', [
    '-u' + database.user,
    '-p' + database.password,
    '-h' + database.address,
    '--default-character-set=utf8',
    '--comments'
]);
mysqlimport.stdin.write( '\\. /Users/user/dumps/' + fileName );
mysqlimport.stdin.end();
mysqlimport
        .stdout
        .pipe(logFile)
        .on('data', function(data) {
           console.log(data); 
        })
        .on('finish', function() {
            console.log('finished')
        })
        .on('error', function(err) {
            console.log(err)
        });

这篇关于生成一个mysql进程以使用节点导入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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