node.js - Spawn a mysql process to import a database using node -


i'm trying write node script automatically import .sql file. think misunderstanding way pass arguments 'spawn'. here's 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 error

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

if don't use -b flag when exporting, , specify database name, changing

'<"' + filename + '"' 

to

databasename + ' <"' + filename + '"' 

i other error:

stdout: error 1102 (42000): incorrect database name ' thedatabase < "/users/user/dumps/sqlfile.sql"' 

i know must doing wrong specifying argument, how fix it? node documentation around spawning child processes confusing me. help!

thanks paul f! solution worked. removed last argument spawn , added code:

mysqlimport.stdin.write( '\\. /users/user/dumps/' + filename ); mysqlimport.stdin.end(); 

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

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -