2

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);
});

And I'm getting the error

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

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

'<"' + fileName + '"'

to

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

I get this other error:

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!

1
  • Check this out - stackoverflow.com/questions/22658957/… - I think you need to use mysqlimport.stdin.write( 'contents of filename' ) and mysqlimport.stdin.end() Commented Jan 7, 2016 at 17:10

1 Answer 1

3

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)
        });
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.