手机版
你好,游客 登录 注册
背景:
阅读新闻

MongoDB基本管理命令

[日期:2012-05-05] 来源:Linux社区  作者:shirdrn [字体:大 中 小]

(六)远程连接管理


1、基于mongo实现远程连接
  1. mongo -u admin -p admin 192.168.0.197:27017/pagedb  
通过mongo实现连接,可以非常灵活的选择参数选项,参看命令帮助,如下所示:
  1. mongo --help  
  2. MongoDB shell version: 1.8.3  
  3. usage: mongo [options] [db address] [file names (ending in .js)]  
  4. db address can be:  
  5.   foo                   foo database on local machine  
  6.   192.169.0.5/foo       foo database on 192.168.0.5 machine  
  7.   192.169.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999  
  8. options:  
  9.   --shell               run the shell after executing files  
  10.   --nodb                don't connect to mongod on startup - no 'db address'   
  11.                         arg expected  
  12.   --quiet               be less chatty  
  13.   --port arg            port to connect to  
  14.   --host arg            server to connect to  
  15.   --eval arg            evaluate javascript  
  16.   -u [ --username ] arg username for authentication  
  17.   -p [ --password ] arg password for authentication  
  18.   -h [ --help ]         show this usage information  
  19.   --version             show version information  
  20.   --verbose             increase verbosity  
  21.   --ipv6                enable IPv6 support (disabled by default)  
2、基于MongoDB支持的javascript实现远程连接
当你已经连接到一个远程的MongoDB数据库服务器(例如,通过mongo连接到192.168.0.184),现在想要在这个会话中连接另一个远程的数据库服务器(192.168.0.197),可以执行如下命令:
  1. > var x = new Mongo('192.168.0.197:27017')  
  2. > var ydb = x.getDB('pagedb');  
  3. > use ydb  
  4. switched to db ydb  
  5. > db  
  6. ydb  
  7. > ydb.page.findOne()  
  8. {  
  9.         "_id" : ObjectId("4eded6a5bf3bfa0014000003"),  
  10.         "content" : "Linux教程_Linux公社-Linux系统门户网站",  
  11.         "pubdate" : "2012-05-05",  
  12.         "title" : "Linux教程_Linux公社-Linux系统门户网站",  
  13.         "url" : "http://www.linuxidc.com/Linuxit/index.htm"  
  14. }  
上述通过MongoDB提供的JavaScript脚本,实现对另一个远程数据库服务器进行连接,操作指定数据库pagedb的page集合。
如果启用了安全认证模式,可以在获取数据库连接实例时,指定认证账号,例如:
  1. > var x = new Mongo('192.168.0.197:27017')  
  2. > var ydb = x.getDB('pagedb', 'shirdrn', '(jkfFS$343$_\=\,.F@3');  
  3. > use ydb  
  4. switched to db ydb  
linux
相关资讯       MongoDB 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款