Mongodb从0到1系列一:下载、安装、启动、停止

本系列文章主要针对mongodb初学者


1. 下载与安装


1.1 下载

下载地址,根据操作系统选择对应文件:
https://www.mongodb.com/download-center#enterprise

我的环境是SUSE 11.4,下载的文件名为:
db2a:~ # ls mon*
mongodb-linux-x86_64-enterprise-suse11-3.4.6.tgz

1.2 安装

下载的文件是一个压缩包,解压即可:
db2a:~ #   tar -zxvf mongodb-linux-x86_64-enterprise-suse11-3.4.6.tgz
mongodb-linux-x86_64-enterprise-suse11-3.4.6/LICENSE.txt
mongodb-linux-x86_64-enterprise-suse11-3.4.6/README
mongodb-linux-x86_64-enterprise-suse11-3.4.6/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-enterprise-suse11-3.4.6/MPL-2
mongodb-linux-x86_64-enterprise-suse11-3.4.6/snmp/MONGOD-MIB.txt
mongodb-linux-x86_64-enterprise-suse11-3.4.6/snmp/MONGODBINC-MIB.txt
mongodb-linux-x86_64-enterprise-suse11-3.4.6/snmp/mongod.conf.master
mongodb-linux-x86_64-enterprise-suse11-3.4.6/snmp/mongod.conf.subagent
mongodb-linux-x86_64-enterprise-suse11-3.4.6/snmp/README-snmp.txt
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongodump
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongorestore
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongoexport
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongoimport
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongostat
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongotop
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/bsondump
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongofiles
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongooplog
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongoperf
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongod
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongos
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongo
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongodecrypt
mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/mongoldap

所有的命令都在mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/,所以要在PATH环境变量里添加上这个路径:

临时添加:
db2a:~ # export PATH=/root/mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/:$PATH

永久添加:
db2a:~ # echo "export PATH=/root/mongodb-linux-x86_64-enterprise-suse11-3.4.6/bin/:$PATH" >> /etc/profile
db2a:~ # source /etc/profile

2. 启动与关闭mongodb服务


2.1 启动mongodb服务

mongodb默认的数据库目录为/data/db,需要先创建好,否则启动会报错
db2a:~ # mkdir -p /data/db

启动mongodb服务的命令是mongod,这里没有加任何参数,表示使用默认的数据库目录/data/db和默认的端口27017
db2a:~ # mongod
2017-07-19T10:49:31.796+0800 I CONTROL  [initandlisten] MongoDB starting : pid=9278 port=27017 dbpath=/data/db 64-bit host=db2a
2017-07-19T10:49:31.797+0800 I CONTROL  [initandlisten] db version v3.4.6
2017-07-19T10:49:31.797+0800 I CONTROL  [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2017-07-19T10:49:31.797+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 0.9.8j-fips 07 Jan 2009
2017-07-19T10:49:31.797+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2017-07-19T10:49:31.797+0800 I CONTROL  [initandlisten] modules: enterprise 
2017-07-19T10:49:31.797+0800 I CONTROL  [initandlisten] build environment:
2017-07-19T10:49:31.797+0800 I CONTROL  [initandlisten]     distmod: suse11
2017-07-19T10:49:31.797+0800 I CONTROL  [initandlisten]     distarch: x86_64
2017-07-19T10:49:31.797+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2017-07-19T10:49:31.797+0800 I CONTROL  [initandlisten] options: {}
2017-07-19T10:49:31.846+0800 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2017-07-19T10:49:31.846+0800 I STORAGE  [initandlisten] 
2017-07-19T10:49:31.846+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-07-19T10:49:31.846+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-07-19T10:49:31.846+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=684M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-07-19T10:49:35.330+0800 I CONTROL  [initandlisten] 
2017-07-19T10:49:35.330+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-07-19T10:49:35.330+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-07-19T10:49:35.330+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-07-19T10:49:35.331+0800 I CONTROL  [initandlisten] 
2017-07-19T10:49:35.331+0800 I CONTROL  [initandlisten] 
2017-07-19T10:49:35.331+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-07-19T10:49:35.331+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-07-19T10:49:35.331+0800 I CONTROL  [initandlisten] 
2017-07-19T10:49:35.331+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-07-19T10:49:35.331+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-07-19T10:49:35.331+0800 I CONTROL  [initandlisten] 
2017-07-19T10:49:35.808+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2017-07-19T10:49:35.929+0800 I NETWORK  [thread1] waiting for connections on port 27017

在另一个session看到27017处于监听状态:
db2a:~ # netstat -an | grep -i 27017
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      
unix  2      [ ACC ]     STREAM     LISTENING     153655 /tmp/mongodb-27017.sock

2.2 关闭mongodb服务

有两种办法

第一种是在开启mongodb的session里按CTRL+C直接结束:


^C2017-07-19T11:40:27.474+0800 I CONTROL  [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends
2017-07-19T11:40:27.474+0800 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2017-07-19T11:40:27.474+0800 I NETWORK  [signalProcessingThread] closing listening socket: 7
2017-07-19T11:40:27.474+0800 I NETWORK  [signalProcessingThread] closing listening socket: 8
2017-07-19T11:40:27.474+0800 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2017-07-19T11:40:27.474+0800 I NETWORK  [signalProcessingThread] shutdown: going to flush diaglog...
2017-07-19T11:40:27.474+0800 I FTDC     [signalProcessingThread] Shutting down full-time diagnostic data capture
2017-07-19T11:40:27.478+0800 I STORAGE  [signalProcessingThread] WiredTigerKVEngine shutting down
2017-07-19T11:40:27.548+0800 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2017-07-19T11:40:27.548+0800 I CONTROL  [signalProcessingThread] now exiting
2017-07-19T11:40:27.548+0800 I CONTROL  [signalProcessingThread] shutting down with code:0
2017-07-19T11:40:27.548+0800 I CONTROL  [initandlisten] shutting down with code:0
db2a:~ # 

第二种是使用shutdownServer(),在另一个session里输入mongo来打开一个MongoDB Shell,可以在这个Shell里输入交互命令来管理mongodb:

db2a:/etc/init.d # mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.6
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-07-19T11:43:09.314+0800 I STORAGE  [initandlisten] 
2017-07-19T11:43:09.314+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-07-19T11:43:09.314+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-07-19T11:43:11.038+0800 I CONTROL  [initandlisten] 
2017-07-19T11:43:11.038+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-07-19T11:43:11.038+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-07-19T11:43:11.038+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-07-19T11:43:11.038+0800 I CONTROL  [initandlisten] 
2017-07-19T11:43:11.038+0800 I CONTROL  [initandlisten] 
2017-07-19T11:43:11.038+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-07-19T11:43:11.038+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-07-19T11:43:11.038+0800 I CONTROL  [initandlisten] 
2017-07-19T11:43:11.038+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-07-19T11:43:11.038+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-07-19T11:43:11.038+0800 I CONTROL  [initandlisten] 
MongoDB Enterprise > use admin
switched to db admin
MongoDB Enterprise > db.shutdownServer()
server should be down...
2017-07-19T11:44:32.233+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2017-07-19T11:44:32.233+0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2017-07-19T11:44:32.233+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed 

你可能感兴趣的:(mongodb)