How to execute mongo commands from bash?

后端 未结 3 979
旧时难觅i
旧时难觅i 2021-02-19 18:00

I am trying to run this command from bash script:

 mongo 192.168.10.20:27000 --eval \"use admin && db.shutdownServer() && quit()\"
3条回答
  •  野性不改
    2021-02-19 18:43

    You can use heredoc syntax.

    #! /bin/sh
    mongo <

    Turns out heredoc syntax throws a warning when EOF is missing at the end for bash script. This is the bash script version.

    #! /bin/bash
    mongo <

    Here is the output, I guess this is what you expected.

    MongoDB shell version: 2.4.14
    connecting to: test
    switched to db admin
    Wed Jun 24 17:07:23.808 DBClientCursor::init call() failed
    server should be down...
    Wed Jun 24 17:07:23.810 trying reconnect to 127.0.0.1:27017
    Wed Jun 24 17:07:23.810 reconnect 127.0.0.1:27017 ok
    Wed Jun 24 17:07:23.812 Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
    Wed Jun 24 17:07:23.812 SocketException: remote: 127.0.0.1:27017 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:27017] 
    Wed Jun 24 17:07:23.812 DBClientCursor::init call() failed
    

提交回复
热议问题