-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-mongodb.js
More file actions
95 lines (84 loc) · 3.21 KB
/
Copy pathcheck-mongodb.js
File metadata and controls
95 lines (84 loc) · 3.21 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Script to check if MongoDB is running
import { exec } from 'child_process';
import { promisify } from 'util';
import os from 'os';
const execAsync = promisify(exec);
async function checkMongoDBRunning() {
const command = os.platform() === 'win32'
? 'netstat -ano | findstr "LISTENING" | findstr "27017"'
: 'lsof -i:27017';
try {
const { stdout } = await execAsync(command);
return stdout.trim() !== '';
} catch (error) {
return false;
}
}
async function startMongoDB() {
console.log('MongoDB does not appear to be running. Attempting to start...');
if (os.platform() === 'win32') {
// Windows: Try to start MongoDB as a service
try {
console.log('Attempting to start MongoDB Windows service...');
await execAsync('net start MongoDB');
console.log('MongoDB service started successfully');
return true;
} catch (error) {
console.error('Failed to start MongoDB service:', error.message);
console.log('\nPlease make sure MongoDB is installed as a Windows service.');
console.log('You can download MongoDB from: https://www.mongodb.com/try/download/community');
console.log('Instructions to install as a service: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/');
return false;
}
} else if (os.platform() === 'darwin') {
// macOS: Try to start MongoDB using brew
try {
console.log('Attempting to start MongoDB with Homebrew...');
await execAsync('brew services start mongodb-community');
console.log('MongoDB started successfully');
return true;
} catch (error) {
console.error('Failed to start MongoDB:', error.message);
console.log('\nPlease make sure MongoDB is installed.');
console.log('You can install MongoDB using Homebrew: brew install mongodb-community');
return false;
}
} else {
// Linux: Try to start MongoDB with systemctl
try {
console.log('Attempting to start MongoDB with systemctl...');
await execAsync('sudo systemctl start mongod');
console.log('MongoDB started successfully');
return true;
} catch (error) {
console.error('Failed to start MongoDB:', error.message);
console.log('\nPlease make sure MongoDB is installed and you have permissions to start it.');
console.log('You can install MongoDB by following: https://docs.mongodb.com/manual/administration/install-on-linux/');
return false;
}
}
}
async function main() {
console.log('Checking if MongoDB is running...');
const isRunning = await checkMongoDBRunning();
if (isRunning) {
console.log('MongoDB is running. Good!');
process.exit(0);
} else {
const started = await startMongoDB();
if (started) {
console.log('MongoDB is now running.');
process.exit(0);
} else {
console.error('\n⚠️ WARNING: MongoDB could not be started.');
console.log('The application may not work correctly without MongoDB running.');
console.log('Please start MongoDB manually and then restart this application.');
// Exit with error code
process.exit(1);
}
}
}
main().catch(error => {
console.error('Error checking MongoDB status:', error);
process.exit(1);
});