mongodb安装

  1. 云栖社区>
  2. 博客>
  3. 正文

mongodb安装

科技小先锋 2017-11-22 17:08:00 浏览908
展开阅读全文

## 讲诉mongodb的两种安装方式,其实都蛮简单的

  • 建议使用官方源进行安装

  • 数据库存放目录建议划分到一个单独的分区上面


## monogdb二进制安装包

1
2
3
4
5
6
7
8
9
10
[root@redis tmp]# tar xf mongodb-linux-x86_64-2.6.3.tgz -C /usr/local/
[root@redis tmp]# mv /usr/local/{mongodb-linux-x86_64-2.6.3,mongodb}
[root@redis tmp]# cd /usr/local/mongodb/
[root@redis mongodb]# ls
bin  GNU-AGPL-3.0  README  THIRD-PARTY-NOTICES
[root@redis mongodb]# mkdir -pv /mongo/data/{mongodb_data,mongodb_log}
mkdir: created directory `/mongo'
mkdir: created directory `/mongo/data'
mkdir: created directory `/mongo/data/mongodb_data'
mkdir: created directory `/mongo/data/mongodb_log'


### 启动mongodb

1
2
3
[root@redis mongodb]# /usr/local/mongodb/bin/mongod --port 27017 --fork --dbpath=/mongo/data/mongodb_data/ --logpath=/mongo/data/mongodb_log/mongodb.log --logappend
about to fork child process, waiting until server is ready for connections.
forked process: 2828


### 验证存活情况:

1
2
3
[root@redis mongodb]# ss -tunlp
Netid  Recv-Q Send-Q                                     Local Address:Port                                       Peer Address:Port 
tcp    0      128                                                    *:27017                                                 *:*      users:(("mongod",2828,8))


### 添加配置文件

1
2
3
4
5
6
7
8
9
10
11
[root@redis mongodb]# vim /etc/mongod.conf
[root@redis mongodb]# cat /etc/mongod.conf
port=27017
dbpath=/mongo/data/mongodb_data/
logpath=/mongo/data/mongodb_log/mongodb.log
pidfilepath=/usr/local/mongodb/mongo.pid
fork=true
logappend=true
shardsvr=true
directoryperdb=true
[root@redis mongodb]#


### 关闭和启动mongodb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@redis mongodb]# /usr/local/mongodb/bin/mongo 127.0.0.1:27017/admin --eval "db.shutdownServer()"
MongoDB shell version: 2.6.3
connecting to: 127.0.0.1:27017/admin
2014-07-19T13:04:27.469+0200 DBClientCursor::init call() failed
server should be down...
[root@redis mongodb]# ss -tunlp
Netid  Recv-Q Send-Q                                     Local Address:Port                                       Peer Address:Port 
tcp    0      128                                                   :::44938                                                :::*      users:(("rpc.statd",1078,11))
tcp    0      128                                                    *:44943                                                 *:*      users:(("rpc.statd",1078,9))
tcp    0      128                                                   :::111                                                  :::*      users:(("rpcbind",1060,11))
tcp    0      128                                                    *:111                                                   *:*      users:(("rpcbind",1060,8))
tcp    0      128                                                   :::22                                                   :::*      users:(("sshd",1253,4))
tcp    0      128                                                    *:22                                                    *:*      users:(("sshd",1253,3))
[root@redis mongodb]#


### 重启启动mongodb

1
2
3
4
5
6
7
[root@redis mongodb]# /usr/local/mongodb/bin/mongod --config /etc/mongod.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 2855
child process started successfully, parent exiting
[root@redis mongodb]# ss -tunlp
Netid  Recv-Q Send-Q                                     Local Address:Port                                       Peer Address:Port 
tcp    0      128                                                    *:27017                                                 *:*      users:(("mongod",2855,8))


### 为mongodb添加Synv服务

  • 添加账户

1
2
3
4
5
[root@redis ~]# groupadd -r  mongodb
[root@redis ~]# useradd -r -g mongodb mongodb
[root@redis ~]# id mongodb
uid=498(mongodb) gid=498(mongodb) groups=498(mongodb)
[root@redis ~]#


### 修改mongodb数据库目录的属组属主

1
2
[root@redis ~]# chown -R mongodb.mongodb /mongo/data/
[root@redis ~]#


### 添加init配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
# mongod - Startup script for mongod
# chkconfig: 35 85 15
# description: Mongo is a scalable, document-oriented database.
# processname: mongod
# config: /etc/mongod.conf
# pidfile: /usr/local/mongodb/mongo.pid
. /etc/rc.d/init.d/functions
# things from mongod.conf get there by mongod reading it
# NOTE: if you change any OPTIONS here, you get what you pay for:
# this script assumes all options are in the config file.
CONFIGFILE="/etc/mongod.conf"
OPTIONS=" -f $CONFIGFILE"
#SYSCONFIG="/etc/sysconfig/mongod"
# FIXME: 1.9.x has a --shutdown flag that parses the config file and
# shuts down the correct running pid, but that's unavailable in 1.8
# for now.  This can go away when this script stops supporting 1.8.
DBPATH=`awk -F= '/^dbpath[[:blank:]]*=[[:blank:]]*/{print $2}' "$CONFIGFILE"`
PIDFILE=`awk -F= '/^pidfilepath[[:blank:]]*=[[:blank:]]*/{print $2}' "$CONFIGFILE"`
mongod=${MONGOD-/usr/local/mongodb/bin/mongod}
MONGO_USER=mongodb
MONGO_GROUP=mongodb
if [ -f "$SYSCONFIG" ]; then
    . "$SYSCONFIG"
fi
# Handle NUMA access to CPUs (SERVER-3574)
# This verifies the existence of numactl as well as testing that the command works
NUMACTL_ARGS="--interleave=all"
if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
then
    NUMACTL="numactl $NUMACTL_ARGS"
else
    NUMACTL=""
fi
start()
{
  # Recommended ulimit values for mongod or mongos
  # See http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
  #
  ulimit -f unlimited
  ulimit -t unlimited
  ulimit -v unlimited
  ulimit -n 64000
  ulimit -m unlimited
  ulimit -u 32000
  echo -n $"Starting mongod: "
  daemon --user "$MONGO_USER" "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod
}
stop()
{
  echo -n $"Stopping mongod: "
  killproc -p "$PIDFILE" -d 300 /usr/bin/mongod
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod
}
restart () {
stop
start
}
RETVAL=0
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart|reload|force-reload)
    restart
    ;;
  condrestart)
    [ -f /var/lock/subsys/mongod ] && restart || :
    ;;
  status)
    status $mongod
    RETVAL=$?
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
    RETVAL=1
esac
exit $RETVAL


### 重新启动mongodb




## mongodbrpm包安装


### 添加官方的repo

1
2
3
4
5
6
vim /etc/yum.repos.d/monogdb.repo
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

    spacer.gif

### 安装mongodb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@mongo1 ~]# yum list mongo*
Loaded plugins: fastestmirror, presto
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * extras: mirrors.btte.net
 * updates: mirrors.btte.net
Available Packages
mongo-10gen.x86_64                                                      2.4.10-mongodb_1                                       mongodb
mongo-10gen-server.x86_64                                               2.4.10-mongodb_1                                       mongodb
mongo-10gen-unstable.x86_64                                             2.5.2-mongodb_1                                        mongodb
mongo-10gen-unstable-mongos.x86_64                                      2.5.2-mongodb_1                                        mongodb
mongo-10gen-unstable-server.x86_64                                      2.5.2-mongodb_1                                        mongodb
mongo-10gen-unstable-shell.x86_64                                       2.5.2-mongodb_1                                        mongodb
mongo-10gen-unstable-tools.x86_64                                       2.5.2-mongodb_1                                        mongodb
mongo18-10gen.x86_64                                                    1.8.5-mongodb_1                                        mongodb
mongo18-10gen-server.x86_64                                             1.8.5-mongodb_1                                        mongodb
mongo20-10gen.x86_64                                                    2.0.8-mongodb_1                                        mongodb
mongo20-10gen-server.x86_64                                             2.0.8-mongodb_1                                        mongodb
mongodb-mms-backup-agent.x86_64                                         2.0.0.97-1                                             mongodb
mongodb-org.x86_64                                                      2.6.3-1                                                mongodb
mongodb-org-mongos.x86_64                                               2.6.3-1                                                mongodb
mongodb-org-server.x86_64                                               2.6.3-1                                                mongodb
mongodb-org-shell.x86_64                                                2.6.3-1                                                mongodb
mongodb-org-tools.x86_64                                                2.6.3-1                                                mongodb
mongodb-org-unstable.x86_64                                             2.7.3-1                                                mongodb
mongodb-org-unstable-mongos.x86_64                                      2.7.3-1                                                mongodb
mongodb-org-unstable-server.x86_64                                      2.7.3-1                                                mongodb
mongodb-org-unstable-shell.x86_64                                       2.7.3-1                                                mongodb
mongodb-org-unstable-tools.x86_64                                       2.7.3-1                                                mongodb
[root@mongo1 ~]# 
[root@mongo1 ~]# yum install mongo-10gen.x86_64 mongo-10gen-server.x86_64 -y


### 创建数据库目录

  • 数据库存放目录的属主属组要和安装mongodb生成的账户一致

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@mongo1 ~]# 
[root@mongo1 ~]# mkdir /mongo/data
mkdir: cannot create directory `/mongo/data': No such file or directory
[root@mongo1 ~]# mkdir /mongo/data -pv
mkdir: created directory `/mongo'
mkdir: created directory `/mongo/data'
[root@mongo1 ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
vagrant:x:500:500::/home/vagrant:/bin/bash
puppet:x:52:52:Puppet:/var/lib/puppet:/sbin/nologin
vboxadd:x:499:1::/var/run/vboxadd:/bin/false
mongod:x:498:498:mongod:/var/lib/mongo:/bin/false
[root@mongo1 ~]# chown -R mongod.mongod /mongo/data


### 修改配置文件

1
2
3
4
5
6
7
8
[root@mongo1 ~]# sed -e '/^#/d;/^$/d' /etc/mongod.conf
logpath=/var/log/mongodb/mongod.log
logappend=true
fork=true
dbpath=/mongo/data
pidfilepath=/var/run/mongodb/mongod.pid
bind_ip=127.0.0.1
[root@mongo1 ~]#


### 将mongodb服务器添加到服务项并启动服务

1
2
3
4
5
[root@mongo1 ~]# chkconfig --add mongod
[root@mongo1 ~]# chkconfig mongod on
[root@mongo1 ~]# service mongod start
Starting mongod:                                           [  OK  ]
[root@mongo1 ~]#

### 验证存活情况

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@mongo1 ~]# mongo
MongoDB shell version: 2.6.3
connecting to: test
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
> show dbs;
admin  (empty)
local  0.078GB
>


接下来是mongodb的CRUD的讲诉~



本文转自lovelace521 51CTO博客,原文链接:http://blog.51cto.com/lovelace/1440988,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论