วิธีหยุด MongoDB ด้วยคำสั่งเดียว
หากต้องการหยุด MongoDB ด้วยคำสั่งเดียว ให้ใช้รูปแบบคำสั่งดังนี้:
mongo --eval "db.getSiblingDB('admin').shutdownServer()"
ตัวอย่างการใช้งาน
เปิด Command Prompt แล้วรันคำสั่งดังนี้:
C:\Program Files\MongoDB\Server\4.0\bin>mongo --eval "db.getSiblingDB('admin').shutdownServer()"
ผลลัพธ์ที่คาดหวัง
MongoDB shell version v4.0.5 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c0337c02-7ee2-45d9-9349-b22d6b1ffe85") }
MongoDB server version: 4.0.5
server should be down...
2019-03-14T21:56:10.327+0530 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2019-03-14T21:56:11.331+0530 I NETWORK [js] reconnect 127.0.0.1:27017 failed
failed
2019-03-14T21:56:11.333+0530 I QUERY [js] Failed to end session { id: UUID("c0337c02-7ee2-45d9-9349-b22d6b1ffe85") } due to SocketException: socket exception [CONNECT_ERROR] server [couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be made because the target machine actively refused it.]
วิธีเริ่มต้น MongoDB อีกครั้ง
ใช้คำสั่ง mongod เพื่อเริ่มบริการ MongoDB ใหม่:
C:\Program Files\MongoDB\Server\4.0\bin>mongod
การตรวจสอบการเชื่อมต่อ
หลังจากเริ่ม MongoDB แล้ว สามารถทดสอบการเชื่อมต่อด้วยคำสั่ง mongo:
C:\Program Files\MongoDB\Server\4.0\bin>mongo