linux安装mongodb


1. 下载安装包

IMG20180817_163457.png

2. 安装

  • 解压缩
root@debian:~/soft# tar zxvf mongodb-linux-x86_64-debian81-4.0.1.tgz
mongodb-linux-x86_64-debian81-4.0.1/README
mongodb-linux-x86_64-debian81-4.0.1/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-debian81-4.0.1/MPL-2
mongodb-linux-x86_64-debian81-4.0.1/GNU-AGPL-3.0
mongodb-linux-x86_64-debian81-4.0.1/LICENSE-Community.txt
mongodb-linux-x86_64-debian81-4.0.1/bin/mongodump
mongodb-linux-x86_64-debian81-4.0.1/bin/mongorestore
mongodb-linux-x86_64-debian81-4.0.1/bin/mongoexport
mongodb-linux-x86_64-debian81-4.0.1/bin/mongoimport
mongodb-linux-x86_64-debian81-4.0.1/bin/mongostat
mongodb-linux-x86_64-debian81-4.0.1/bin/mongotop
mongodb-linux-x86_64-debian81-4.0.1/bin/bsondump
mongodb-linux-x86_64-debian81-4.0.1/bin/mongofiles
mongodb-linux-x86_64-debian81-4.0.1/bin/mongoreplay
mongodb-linux-x86_64-debian81-4.0.1/bin/mongod
mongodb-linux-x86_64-debian81-4.0.1/bin/mongos
mongodb-linux-x86_64-debian81-4.0.1/bin/mongo
mongodb-linux-x86_64-debian81-4.0.1/bin/install_compass
  • 将压缩包移动到要安装的目录
root@debian:~/soft# mv ./mongodb-linux-x86_64-debian81-4.0.1 /usr/local/
root@debian:~/soft# cd /usr/local
root@debian:/usr/local# ls
bin  etc  games  include  lib  man  mongodb-linux-x86_64-debian81-4.0.1  mysql  mysql-8.0.12-linux-glibc2.12-x86_64  sbin  share  src
root@debian:/usr/local# ln -s ./mongodb-linux-x86_64-debian81-4.0.1/ /usr/local/mongodb
root@debian:/usr/local# ls -l
total 40
drwxrwsr-x  2 root  staff 4096 Apr 20 12:35 bin
drwxrwsr-x  2 root  staff 4096 Apr 20 12:35 etc
drwxrwsr-x  2 root  staff 4096 Apr 20 12:35 games
drwxrwsr-x  2 root  staff 4096 Apr 20 12:35 include
drwxrwsr-x  3 root  staff 4096 Apr 20 12:42 lib
lrwxrwxrwx  1 root  staff    9 Apr 20 12:35 man -> share/man
lrwxrwxrwx  1 root  staff   38 Aug 20 14:20 mongodb -> ./mongodb-linux-x86_64-debian81-4.0.1/
drwxr-xr-x  3 root  root  4096 Aug 20 14:17 mongodb-linux-x86_64-debian81-4.0.1
lrwxrwxrwx  1 mysql mysql   36 Aug 17 16:59 mysql -> mysql-8.0.12-linux-glibc2.12-x86_64/
drwxr-xr-x 11 root  mysql 4096 Aug 17 18:13 mysql-8.0.12-linux-glibc2.12-x86_64
drwxrwsr-x  2 root  staff 4096 Apr 20 12:35 sbin
drwxrwsr-x  8 root  staff 4096 Apr 20 12:43 share
drwxrwsr-x  2 root  staff 4096 Apr 20 12:35 src
root@debian:/usr/local#

  • 将mongodb的二进制文件目录添加到PATH环境变量
    打开文件 /etc/profile, 在文件尾加上下面一行
export PATH=$PATH:/usr/local/mongodb/bin

然后运行 source /etc/profile 以使环境变量立即生效

root@debian:~# source /etc/profile
root@debian:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/mongodb/bin:/usr/local/mysql/bin

3. 创建数据库目录

  • 创建数据库数据目录以及日志目录
root@debian:~# cd /data
root@debian:/data# mkdir mongodb
root@debian:/data# cd mongodb/
root@debian:/data/mongodb# mkdir data
root@debian:/data/mongodb# mkdir logs
root@debian:/data/mongodb# ls -la
total 16
drwxr-xr-x 4 root root 4096 Aug 20 14:37 .
drwxr-xr-x 4 root root 4096 Aug 20 14:36 ..
drwxr-xr-x 2 root root 4096 Aug 20 14:37 data
drwxr-xr-x 2 root root 4096 Aug 20 14:37 logs
root@debian:/data/mongodb# cd logs/
root@debian:/data/mongodb/logs# touch logfile
root@debian:/data/mongodb/logs# ls
logfile

4. 启动mongodb服务

root@debian:/usr/local/mongodb/bin# ./mongod --dbpath=/data/mongodb/data  --logpath=/data/mongodb/logs/logfile  -logappend -port=27017 -fork
./mongod: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory
root@debian:/usr/local/mongodb/bin#

提示没有libcurl.so.4, 安装之

apt-get install curl

再次尝试启动

root@debian:/usr/local/mongodb/bin# ./mongod --dbpath=/data/mongodb/data  --logpath=/data/mongodb/logs/logfile  -logappend -port=27017 -fork
2018-08-20T14:48:48.185+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 20614
child process started successfully, parent exiting

或者以指定配置文件的方式启动
我们在/data/mongodb/conf/下创建配置文件mongod.cfg, 内容如下

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /data/mongodb/data
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/logs/logfile

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,192.168.104.17


processManagement:
   fork: true

security:
  authorization: disabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

然后运行以下命令以指定配置文件的方式启动mongod

root@debian:/usr/local/mongodb/bin# mongod -f /data/mongodb/conf/mongod.cfg
2018-08-20T15:19:59.501+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 20838
child process started successfully, parent exiting

5. mongo的shell工具

启动mongo shell

root@debian:/usr/local/mongodb/bin# mongo
MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.1
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
......

>

一些测试

> 2 + 2
4
> 3 + 6
9
> db.runoob.insert({x : 10})
WriteResult({ "nInserted" : 1 })
> db.runoob.find()
{ "_id" : ObjectId("5b7a655a9d9f25e2bbf2713b"), "x" : 10 }
>

6. 用户创建以及权限控制

见我另外一篇文章mongodb用户创建以及权限控制

推荐阅读更多精彩内容