MongoDB的源码安装

Mongodb安装使用 

官网下载地址:http://www.mongodb.org/downloads

1)安装mongodb
[root@data-server src]# cd /usr/local/src/
[root@data-server src]# tar -zvxf mongodb-linux-x86_64-rhel62-3.4.4
[root@data-server src]# mv mongodb-linux-x86_64-rhel62-3.4.4 /usr/local/mongodb
[root@data-server src]# cd /usr/local/mongodb           //Mongodb主目录
[root@data-server mongodb]# ll
总用量 120
drwxr-xr-x. 2 root root  4096 6月   3 14:51 bin
-rw-r--r--. 1 root root 34520 4月  21 06:19 GNU-AGPL-3.0
-rw-r--r--. 1 root root 16726 4月  21 06:19 MPL-2
-rw-r--r--. 1 root root  1359 4月  21 06:19 README
-rw-r--r--. 1 root root 55625 4月  21 06:19 THIRD-PARTY-NOTICES
[root@data-server mongodb]# mkdir /usr/local/mongodb/data        //Mongodb数据目录,可以存放在一个独立的大分区上
[root@data-server mongodb]# mkdir /usr/local/mongodb/log         //Mongodb日志目录
  
2)启动Mongodb
使用mongod命令建立一个mongodb数据库链接,端口号设置为10001,数据库的路径为/usr/local/mongodb/data,日志路径为/usr/local/mongodb/log/mogodb.log
mongodb的启动程序放在后台执行,下面命令执行后,按ctrl+c
[root@data-server mongodb]# nohup /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/log/mongo.log &
  
mongodb默认端口是27017
[root@data-server mongodb]# ps -ef|grep mongodb
root     14858 14518  1 15:01 pts/1    00:00:01 /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/log/mongo.log
root     14887 14518  0 15:02 pts/1    00:00:00 grep mongodb
[root@data-server bin]# netstat -tunlp|grep 14858
tcp        0      0 0.0.0.0:27017               0.0.0.0:*                   LISTEN      14858/mongod
[root@data-server mongodb]# lsof -i:27017
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mongod  14858 root    7u  IPv4 145311      0t0  TCP *:27017 (LISTEN)
  
3)设置mongodb的环境变量
[root@data-server ~]# vim /etc/profile
.......
export PATH=$PATH:/usr/local/mongodb/bin/
[root@data-server ~]# source /etc/profile
  
4)为了更方便的启动和关闭MongoDB,我们可以使用Shell写脚本,当然也可以加入到service中
   更好的方式是采用配置文件,把MongoDB需要的参数写入配置文件,然后在脚本中引用;
[root@data-server ~]# cat /usr/local/mongodb/mongodb.conf
#代表端口号,如果不指定则默认为27017
port=27017
#MongoDB数据文件目录
dbpath=/usr/local/mongodb/data
#MongoDB日志文件目录
logpath=/usr/local/mongodb/log/mongo.log
#日志文件自动累加
logappend=true
  
编写启动脚本
[root@data-server ~]# vim /etc/init.d/mongodb
#!/bin/bash
#
# mongod        Start up the MongoDB server daemon
#
   
# source function library
. /etc/rc.d/init.d/functions
#定义命令
CMD=/usr/local/mongodb/bin/mongod
#定义配置文件路径
INITFILE=/usr/local/mongodb/mongodb.conf
start()
{
    #&表示后台启动,也可以使用fork参数
    $CMD -f $INITFILE &
    echo "MongoDB is running background..."
}
   
stop()
{
    pkill mongod
    echo "MongoDB is stopped."
}
   
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    *)
        echo $"Usage: $0 {start|stop}"
esac
[root@data-server ~]# chmod 755 /etc/init.d/mongodb
[root@data-server ~]# /etc/init.d/mongodb status
用法:/etc/init.d/mongodb {start|stop}
[root@data-server ~]# /etc/init.d/mongodb stop
已终止
[root@data-server ~]# lsof -i:27017
[root@data-server ~]# /etc/init.d/mongodb start
MongoDB is running background...
[root@data-server ~]# lsof -i:27017
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mongod  15138 root    7u  IPv4 147713      0t0  TCP *:27017 (LISTEN)
 
通过 shell 连接 MongoDB 服务:(在客户机上连接本机mongodb:mongo 182.48.115.238:27017)
[root@data-server ~]# mongo
MongoDB shell version v3.4.4
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.4
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
  http://docs.mongodb.org/
Questions? Try the support group
  http://groups.google.com/group/mongodb-user
Server has startup warnings:
2017-06-03T15:17:54.695+0800 I STORAGE  [initandlisten]
2017-06-03T15:17:54.695+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-06-03T15:17:54.695+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-06-03T15:17:55.699+0800 I CONTROL  [initandlisten]
2017-06-03T15:17:55.700+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-06-03T15:17:55.700+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-06-03T15:17:55.700+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-06-03T15:17:55.700+0800 I CONTROL  [initandlisten]
2017-06-03T15:17:55.700+0800 I CONTROL  [initandlisten]
2017-06-03T15:17:55.700+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-06-03T15:17:55.700+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-06-03T15:17:55.700+0800 I CONTROL  [initandlisten]
2017-06-03T15:17:55.700+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-06-03T15:17:55.700+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-06-03T15:17:55.700+0800 I CONTROL  [initandlisten]
> 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 [name]              prints out the last segment of log in memory, 'global' is default
  use <db_name>                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

-------------------------------------------------mongodb非正常关闭导致启动失败问题-------------------------------------------------

之前强制关闭了mongodb,后续再次启动的时候,发现报错:
[root@i-omxpbsuo ~]# /usr/local/mongodb/bin/mongod --logpath /usr/local/mongodb/log/system.log --logappend --dbpath /data/mongodb --directoryperdb --auth --journal --profile=1 --slowms=5 --fork 
forked process: 4853
all output going to: /usr/local/mongodb/log/system.log

查看日志

 这是由于上次mongodb非正常关闭导致的。
解决办法:
1)删除数据目录/data/mongodb 下的 mongod.lock 
[root@i-omxpbsuo ~]# rm -rf /data/mongodb/mongod.lock

2)修复mongodb
[root@i-omxpbsuo ~]# /usr/local/mongodb/bin/mongod --logpath --dbpath /data/mongodb --repair 

3)删除/data/mongodb/journal 下的 j._4 文件(或者将journal下的文件清空)
[root@i-omxpbsuo ~]# rm -rf /data/mongodb/journal/*j._4

4)然后再次启动mongodb就ok了
[root@i-omxpbsuo ~]# /usr/local/mongodb/bin/mongod --logpath --dbpath /data/mongodb --directoryperdb --auth --journal --profile=1 --slowms=5 --fork &

---------------------------------------------------------
正确关闭mongod 的方法:进入mongo shell
> use admin
> db.shutdownServer()
 
也可以按照文档粗暴的杀掉它,它内部应该有KILL信号处理程序。
# killall mongod
  
请不要 kill -9 ,会造成文件数据混乱丢失repair也无力回天。
---------------------------------------------------------

配置文件解释

[root@data-server ~]# cat /etc/mongod.conf
# mongo.conf    
     
#where to log    
logpath=/var/log/mongo/mongod.log    
     
logappend=true #以追加方式写入日志    
     
# fork and run in background    
fork = true   
     
#port = 27017 #端口    
     
dbpath=/var/lib/mongo #数据库文件保存位置    
directoryperdb=true 
# Enables periodic logging of CPU utilization and I/O wait    
#启用定期记录CPU利用率和 I/O 等待    
#cpu = true   
     
# Turn on/off security.  Off is currently the default   
# 是否以安全认证方式运行,默认是不认证的非安全方式    
#noauth = true   
#auth = true   
     
# Verbose logging output.    
# 详细记录输出    
#verbose = true   
     
# Inspect all client data for validity on receipt (useful for   
# developing drivers)用于开发驱动程序时的检查客户端接收数据的有效性    
#objcheck = true   
     
# Enable db quota management 启用数据库配额管理,默认每个db可以有8个文件,可以用quotaFiles参数设置    
#quota = true   
# 设置oplog记录等级    
# Set oplogging level where n is    
#   0=off (default)    
#   1=W    
#   2=R    
#   3=both    
#   7=W+some reads    
#oplog = 0   
     
# Diagnostic/debugging option 动态调试项    
#nocursors = true   
     
# Ignore query hints 忽略查询提示    
#nohints = true   
# 禁用http界面,默认为localhost:28017   
# Disable the HTTP interface (Defaults to localhost:27018).这个端口号写的是错的    
#nohttpinterface = true   
     
# 关闭服务器端脚本,这将极大的限制功能    
# Turns off server-side scripting.  This will result in greatly limited    
# functionality    
#noscripting = true   
# 关闭扫描表,任何查询将会是扫描失败    
# Turns off table scans.  Any query that would do a table scan fails.    
#notablescan = true   
# 关闭数据文件预分配    
# Disable data file preallocation.    
#noprealloc = true   
# 为新数据库指定.ns文件的大小,单位:MB    
# Specify .ns file size for new databases.    
# nssize = <size>    
     
# Accout token for Mongo monitoring server.    
#mms-token = <token>    
# mongo监控服务器的名称    
# Server name for Mongo monitoring server.    
#mms-name = <server-name>    
# mongo监控服务器的ping 间隔    
# Ping interval for Mongo monitoring server.    
#mms-interval = <seconds>    
     
# Replication Options 复制选项    
     
# in replicated mongo databases, specify here whether this is a slave or master 在复制中,指定当前是从属关系    
#slave = true   
#source = master.example.com    
# Slave only: specify a single database to replicate    
#only = master.example.com    
# or    
#master = true   
#source = slave.example.com 
posted @ 2020-07-31 15:46  fat_girl_spring  阅读(796)  评论(0编辑  收藏  举报