(六)远程连接管理
- mongo -u admin -p admin 192.168.0.197:27017/pagedb
- mongo --help
- MongoDB shell version: 1.8.3
- usage: mongo [options] [db address] [file names (ending in .js)]
- db address can be:
- foo foo database on local machine
- 192.169.0.5/foo foo database on 192.168.0.5 machine
- 192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
- options:
- --shell run the shell after executing files
- --nodb don't connect to mongod on startup - no 'db address'
- arg expected
- --quiet be less chatty
- --port arg port to connect to
- --host arg server to connect to
- --eval arg evaluate javascript
- -u [ --username ] arg username for authentication
- -p [ --password ] arg password for authentication
- -h [ --help ] show this usage information
- --version show version information
- --verbose increase verbosity
- --ipv6 enable IPv6 support (disabled by default)
2、基于MongoDB支持的javascript实现远程连接
当你已经连接到一个远程的MongoDB数据库服务器(例如,通过mongo连接到192.168.0.184),现在想要在这个会话中连接另一个远程的数据库服务器(192.168.0.197),可以执行如下命令:
- > var x = new Mongo('192.168.0.197:27017')
- > var ydb = x.getDB('pagedb');
- > use ydb
- switched to db ydb
- > db
- ydb
- > ydb.page.findOne()
- {
- "_id" : ObjectId("4eded6a5bf3bfa0014000003"),
- "content" : "Linux教程_Linux公社-Linux系统门户网站",
- "pubdate" : "2012-05-05",
- "title" : "Linux教程_Linux公社-Linux系统门户网站",
- "url" : "http://www.linuxidc.com/Linuxit/index.htm"
- }
如果启用了安全认证模式,可以在获取数据库连接实例时,指定认证账号,例如:
- > var x = new Mongo('192.168.0.197:27017')
- > var ydb = x.getDB('pagedb', 'shirdrn', '(jkfFS$343$_\=\,.F@3');
- > use ydb
- switched to db ydb
