zxcvb12 发表于 2018-10-26 10:28:35

mongodb的安装部署

  1.下载软件:
  wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.0.3.tgz
  2.解压安装:
  tar -zxvf mongodb-linux-x86_64-rhel62-3.0.3.tgz
  mv mongodb-linux-x86_64-rhel62-3.0.3 /usr/local/
  cd /usr/local/
  ln -s mongodb-linux-x86_64-rhel62-3.0.3 mongodb
  mkdir -p data/db mkdir data/log
  3.启动:
  echo 'export PATH=/usr/local/mongodb/bin:$PATH' >/etc/profile.d/mongodb.sh
  source /etc/profile.d/mongodb.sh
  命令行启动:
  ./mongod --dbpath=../data/db --logpath=../data/log/mongod.log--fork
  mongod为服务端程序--dbpath指定数据的存储目录--logpath指定日志的存储目录--fork指定以守护进程的方式启动(注意,以守护进程方式启动的话必须指定日志存储路径)
  配置文件启动:
  # cat /etc/mongod.conf
  logpath=/usr/local/mongodb/data/log/mongod.log
  logappend=true
  fork=true
  dbpath=/usr/local/mongodb/data/db
  port=27017
  rest=true
  #默认的mongodb会监控27017端口不甚安全,可以用--port参数进行设定其监控的端口。mongodb默认的会在服务的端口号加上1000的端口上启动一个web服务器,要使用web服务器的有关内容,需要启用--rest参数;
  #http://192.168.16.131:28017
  #./mongod -f /etc/mongod.conf
  4.mongodb的架构介绍:
  4.1数据逻辑结构
  MongoDB的数据逻辑结构由:数据库(database)、集合(collections)、文档(document)三部分组成。
  一个MongoDB支持多个数据库,每个数据库中包含多个集合(相当于关系数据库中的表),每个集合中包含多个文档(相当于关系型数据库中表的一行)。
  4.2数据存储结构
  MongoDB中,每个数据库包含一个.ns和一个或多个数据文件,其中,数据文件会随着数据量的增多而变多,例如Test数据库的数据文件就由Test.0、Test.1、Test.2等等组成。MongoDB采用预分配空间机制,每个预分配空间的文件都采用0进行填充,由于集合中的数据增加,数据文件每新分配一次,它的大小会是上一个文件大小的2倍,数据库里每个集合和索引都对应一个命名空间,这些命名空间的元数据都存储在.ns文件里。
  4.3BSON
  BSON是一种类似于json的二进制的存储格式,Binary JSON,支持内建的文档对象和数组对象,并且包含JSON所没有的一些数据类型。MongoDB采用BSON这种结构来存储数据和进行网络数据交换,把这个格式转化成Document的概念,由于BSON是模式自由的,所以document也是模式自由的。
  5.mongodb的简单操作:
  # mongo
  > help
  db.help()                  help on db methods
  db.mycoll.help()             help on collection methods
  sh.help()                  sharding helpers
  rs.help()                  replica set helpers
  help admin                   administrative help
  help connect               connecting to a db help
  help keys                  key shortcuts
  help misc                  misc things to know
  help mr                      mapreduce
  show dbs                     show database names
  show collections             show collections in current database
  show users                   show users in current database
  show profile               show most recent system.profile entries with time >= 1ms
  show logs                  show the accessible logger names
  show log             prints out the last segment of log in memory, 'global' is default
  use               set current database
  db.foo.find()                list objects in collection foo
  db.foo.find( { a : 1 } )   list objects in foo where a == 1
  it                           result of the last line evaluated; use to further iterate
  DBQuery.shellBatchSize = x   set default number of items to display on shell
  exit                         quit the mongo shell
  > show users
  > show profile
  db.system.profile is empty
  Use db.setProfilingLevel(2) will enable profiling
  Use db.system.profile.find() to show raw profile entries
  #列出当前有哪些数据库
  > show dbs;
  local0.078GB
  > db.test_1.save({1:"AAA"});
  WriteResult({ "nInserted" : 1 })
  > db.test_1.save({2:"BBB"});
  WriteResult({ "nInserted" : 1 })
  > db.test_1.find();
  { "_id" : ObjectId("556d171a75f85e97eeec2f5b"), "1" : "AAA" }
  { "_id" : ObjectId("556d172375f85e97eeec2f5c"), "2" : "BBB" }
  > show dbs;
  local0.078GB
  test   0.078GB
  #查看当前的数据库
  > db
  test
  #列出当前数据库中有哪些集合
  > show collections
  system.indexes
  test_1
  6.关闭mongodb:
  # mongo
  > use admin
  switched to db admin
  > db.shutdownServer();
  2015-06-01T19:35:45.155-0700 I NETWORKDBClientCursor::init call() failed
  server should be down...
  2015-06-01T19:35:45.159-0700 I NETWORKtrying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
  2015-06-01T19:35:45.159-0700 W NETWORKFailed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
  2015-06-01T19:35:45.160-0700 I NETWORKreconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed

页: [1]
查看完整版本: mongodb的安装部署