上QQ阅读APP看书,第一时间看更新
Options when invoking the shell
Here are some common command line options (https://docs.mongodb.com/manual/reference/program/mongo/#options) when invoking mongo:
- mongo: Opens up the interactive command shell.
- mongo -u username -p password -h host --port nnnn --ssl: The first two options are needed when security has been configured. The next two options are needed when accessing a remote MongoDB instance. The last option secures the command stream using TLS or SSL. Also, refer to the Chapter 7, Securing MongoDB, in this book for more information on setting up TLS/SSL.
- mongo some_JavaScript_file.js: Executes a JavaScript file containing MongoDB commands.
- mongo --eval 'JavaScript': Directly runs the JavaScript commands in quotes.
- mongo --help: Get help on using the mongo command shell.
- exit: Invokes quit(), which closes the shell returning you to the command line.
The mongo command line options shown here can be combined. As an example, to run a script abc.js on a remote MongoDB server remote.mongo.com, as user fred and with a password of flintstone, the command might appear as follows:
mongo abc.js -h remote.mongo.com -u fred -p flintstone
mongo abc.js -h remote.mongo.com -u fred -p flintstone
Before you invoke the shell, it is extremely important to make sure the MongoDB database is up and running! If, after running the mongo command you see a message similar to this, the database instance is probably not running:
$ mongo
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017
2018-07-04T10:41:29.866+0700 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed
$ mongo
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017
2018-07-04T10:41:29.866+0700 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed