mongodb入门安装与配置

http://docs.mongodb.org/manual/installation/
http://wordpressapi.com/2010/09/09/how-to-install-mongodb-on-linux/

依赖安装:
 sudo apt-get install git tcsh scons gcc-c++ glibc-devel boost-devel pcre-devel js-devel readline-devel boost-devel-static readline-static ncurses-static

下载文件:
 对于32位的linux
  curl http://downloads.mongodb.org/linux/mongodb-linux-i686-2.2.2.tgz > mongodb.tgz
  tar -zxvf mongodb.tgz

 对于64位的linux
  curl http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.2.2.tgz > mongodb.tgz
  tar -zxvf mongodb.tgz
  重命名mv mongodb-linux-x86_64-2.2.2 /opt/mongodb

创建数据存储目录:
 默认情况下,MongoDB会在/data/db/这个文件夹存放数据
 cd /opt/mongodb
 sudo mkdir -p /opt/mongodb/data/db/
 sudo mkdir /opt/mongodb/log
 touch /opt/mongodb/log/mongodb.log
 adduser mongodb
 passwd mongodb
 sudo chown `id -u` /opt/mongodb/data/db
 chown -R mongodb:mongodb /opt/mongodb/data
 #可以通过--dbpath命令指定MongoDB数据存储目录

启动数据库:
 cd /opt/mongodb/bin
 启动服务:
  ./mongod --dbpath=../data/db --logpath=../log/mongodb.log
 连接数据库:
  ./mongo 127.0.0.1:27017
 or
 nohup /opt/mongodb/bin/mongod & /opt/mongodb/bin/mongo
 参数说明
 --dbpath            #指定db文件存放的目录
 --port              #指定mongod服务使用的端口
 --fork              #设置mongo服务为后台运行
 --logpath           #指定log文件的目录和文件名
 --logappend         #设置每次log添加在文件最后
 --rest              #关闭rest api功能
 --nohttpinterface   #关闭web管理功能
 --auth              #指定mongo使用身份验证机制
 --bindip            #用逗号分隔ip地址,用来指定
 --f                 #将所有前面介绍的参数都可以存放到一个配置文件中,然后用这个参数调用配置文件来启动mongod服务

nohup /opt/mongodb/bin/mongod --dbpath=/opt/mongodb/data/db --logpath=/opt/mongodb/log/mongodb.log & /opt/mongodb/bin/mongo

 --------------------关闭MongoDB--------------------
 1.db.shutdownServer()  #推荐优先使用
 2.ctrl + c             #在不使用 --fork参数的时候可以使用,可能会造成数据文件损坏
 3.kill / kill -2       #在无法使用 a和b的情况下使用,可能会造成数据文件损坏
 4.kill -9              #不在万不得已的情况下,不要使用这个方法
 --------------------查看MongoDB状态--------------------
 db.runCommand({"serverStatus":1})
 MONGO_HOME/bin/mongostat
 --------------------添加用户,切换用户--------------------
 db.addUser("root","123")
 db.addUser("read_only","123",true);  #第3个参数表示设置readonly的状态
 db.auth("read_only","123")
 --------------------数据库备份--------------------
 1.关闭mongod服务后,复制--dbpath参数指定的数据文件。优点速度快,缺点需要停止mongo服务。
 2.使用mongodump 导出数据,并用mongorestore 导入数据。优点不需要停止mongo服务,缺点在mongodump操作时用户插入的数据可能无法备份出来。
 3.fsync and lock锁定数据库的让用户只能使用read功能,再使用方法b导出并导入数据。优点不需要停止mongo服务,缺点在数据库lock期间用户无法执行insert操作。
 4.使用slaveDB并且 使用方法c锁定slaveDB,再使用方法b导出并导入数据。优点不需要停止mongo服务,不会影响用户insert操作(推荐使用此方法)。
 --------------------修复数据库--------------------
 1.MONGO_HOME/bin/mongod --repair
 2.use test
   db.repairDatabase()
 3.db.runCommand({"repairDatabase":1});

注册服务:
 #-----------------------------------
 vim /opt/mongodb/bin/mongodb-stop
 #!/bin/bash
 pid=`ps -o pid,command ax | grep mongod | awk '!/awk/ && !/grep/ {print $1}'`;
 if [ "${pid}" != "" ]; then
 kill -2 ${pid};
 fi
 chmod +x /opt/mongodb/bin/mongodb-stop
 #-----------------------------------
 mkdir /opt/mongodb/config/
 vim /opt/mongodb/bin/mongodb-start
 #!/bin/sh
 /opt/mongodb/bin/mongod --config /opt/mongodb/config/mongodb \
 ## --upgrade \ ##runs a database upgrade option if needed \
 chmod +x /opt/mongodb/bin/mongodb-start
 #-----------------------------------
 vim /opt/mongodb/config/mongodb
 # Configuration Options for MongoDB
 #
 # For More Information, Consider:
 # - Configuration Parameters: http://www.mongodb.org/display/DOCS/Command+Line+Parameters
 # - File Based Configuration: http://www.mongodb.org/display/DOCS/File+Based+Configuration
 dbpath = /opt/mongodb/data/db
 logpath = /opt/mongodb/log/mongodb.log
 logappend = true
 bind_ip = 127.0.0.1
 port = 27017
 fork = true
 auth = true
 # noauth = true
 #---------------自启动---------------
 wget http://library.linode.com/databases/mongodb/reference/init-rpm.sh
 mv init-rpm.sh /etc/rc.d/init.d/mongodb
 chmod +x /etc/rc.d/init.d/mongodb /etc/init.d/mongodb
 chkconfig –add mongodb
 chkconfig –level 35 mongodb on
-----------------------------------------------------------------
http://sameveryday.blog.163.com/blog/static/178072337201102041455353/
http://blog.csdn.net/shirdrn/article/details/7105539
http://my.oschina.net/xqinghu/blog/32381
以下为客户端常用命令
show dbs #查看所有数据库
使用test数据库use test
db.getName() #查看当前操作的数据库
显示test所有表show collections
查询服务器状态db.serverStatus()

User:
db.addUser(username, password) #添加用户
db.removeUser(username) #删除用户

Database:
db.dropDatabase() #修复数据库
db.repairDatabase() #删除当前操作数据库

Collection:
db.createCollection(name, { size : …, capped : …, max : … }) #创建数据集合(表)
db.printCollectionStats() #查看数据库状态信息

Profile:
db.getProfilingLevel()
db.setProfilingLevel(level) #0=off 1=slow 2=all

Server:
db.version() #当前数据库版本
db.shutdownServer() #关闭服务端

原文链接: https://www.cnblogs.com/wen12128/archive/2012/12/25/2832587.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    mongodb入门安装与配置

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/73559

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午3:59
下一篇 2023年2月9日 下午4:00

相关推荐