/
index.js
60 lines (52 loc) · 1.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
var extend = require('extend');
var defaultConfig = {
port : 3306,
// Binary log settings
server_id : 1,
binlog_format : 'row',
log_bin : '../../binlog/mysql-bin.log',
binlog_checksum : 'CRC32',
expire_logs_days : 10,
max_binlog_size : '100M',
// Settings related to this package's directory structure
// tmpdir set in server/start.sh
basedir : './',
datadir : './data/mysql',
socket : './mysql.sock',
pid_file : './mysql.pid',
// Other settings
innodb_buffer_pool_size : '128M',
expire_logs_days : '10',
sql_mode : 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES',
}
// Start the MySQL server, optionally specifying options for my.cnf
module.exports = function(config) {
var fullConfig = extend(defaultConfig, config || {});
// Generate my.cnf from provided configuration
var myCnf = '[mysqld]\n' +
Object.keys(fullConfig).map(function(key) {
if(fullConfig[key] === null || fullConfig[key] === undefined) {
return ''
} else {
return key + ' = ' + fullConfig[key]
}
}).join('\n');
fs.writeFileSync(path.join(__dirname, 'server/my.cnf'), myCnf);
// Did not work spawning mysqld directly from node, therefore shell script
var child = spawn(path.join(__dirname, 'server/start.sh'));
// Server pid is different than the spawned child pid becuase of shell script
// Provide extra method on child process to stop the server
child.stop = function() {
var pidFile = path.join(__dirname, 'server', fullConfig.pid_file);
var pid = parseInt(fs.readFileSync(pidFile), 10);
process.kill(pid);
};
return child
}
// Provide package directory for external use
module.exports.pkgdir = function() {
return __dirname
}