mongodb3.2二进制单机版安装
1.环境介绍2.配置环境变量,修改与之相关的系统参数3.创建mongodb运行用户和目录等4.上传安装包,解压安装5.创建配置文件6.启动并进行简单测试7.总结1.环境介绍:OS:Centos6.5_64MongoDB版本:mongodb-linux-x86_64-rhel62-v3.2-latest.tgzMemory:2G
2.配置环境变量,修改与之相关的系统参数在该文件中末尾添加这些参数,否则后面会有相应地告警信息# vim /etc/security/limits.conf*-fsize unlimited # (file size)*-cpu unlimited # (cpu time)*-as unlimited # (virtual memory size)*-nofile 64000 # (open files)*-nproc 64000 # (processes/threads)
mongodb soft nofile 64000mongodb hard nofile 64000mongodb soft nproc 32000mongodb hard nproc 32000
在proc中关闭NUMA# echo 0 > /proc/sys/vm/zone_reclaim_mode # sysctl -w vm.zone_reclaim_mode=0vm.zone_reclaim_mode = 0# echo never > /sys/kernel/mm/transparent_hugepage/enabled# echo never > /sys/kernel/mm/transparent_hugepage/defrag
# vim /etc/rc.local if test -f /sys/kernel/mm/transparent_hugepage/enabled; then echo never > /sys/kernel/mm/transparent_hugepage/enabledfiif test -f /sys/kernel/mm/transparent_hugepage/defrag; then echo never > /sys/kernel/mm/transparent_hugepage/defragfi
上述操作的目的是解决类似如下的告警信息:2015-03-19T00:43:27.760+0800 I CONTROL ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.2015-03-19T00:43:27.760+0800 I CONTROL ** We suggest setting it to 'never'
3.创建mongodb运行用户和目录等因为高版本的mongodb需要独立的用户来运行mongod进程,像mysqld需要mysql用户一样,所以这里我推荐创建一个用户。如果不创建的话,也能运行,不过日志中会有相应的告警提示信息。# useradd mongodb# 注意:mongodb这个用户名可以取别的# passwd mongodbChanging password for user mongodb.New password: BAD PASSWORD: it is based on a dictionary wordBAD PASSWORD: is too simpleRetype new password: passwd: all authentication tokens updated successfully.#
创建需要的目录,并修改权限,切换到mongodb用户模式# su - mongodb$ mkdir -p {data,log,conf}$ lltotal 12drwxrwxr-x 2 mongodb mongodb 4096 Jan 25 14:03 confdrwxrwxr-x 2 mongodb mongodb 4096 Jan 25 14:03 datadrwxrwxr-x 2 mongodb mongodb 4096 Jan 25 14:03 log$
4.上传安装包,解压安装$ wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel62-v3.2-latest.tgz$ lltotal 64900drwxrwxr-x 2 mongodb mongodb 4096 Jan 25 14:03 confdrwxrwxr-x 2 mongodb mongodb 4096 Jan 25 14:03 datadrwxrwxr-x 2 mongodb mongodb 4096 Jan 25 14:03 log-rw-rw-r-- 1 mongodb mongodb 66443467 Jan 23 02:47 mongodb-linux-x86_64-rhel62-v3.2-latest.tgz$ tar -zxf mongodb-linux-x86_64-rhel62-v3.2-latest.tgz $ mv mongodb-linux-x86_64-rhel62-3.2.1-83-g187028f/ mongodb3.2.1$ rm -f mongodb-linux-x86_64-rhel62-v3.2-latest.tgz# 可以不用删除
5.创建配置文件$ vim /home/mongodb/.bash_profile export PATH=/home/mongodb/mongodb3.2.1/bin:$PATH$ source /home/mongodb/.bash_profile $ vim conf/mongodb.conf# bind_ip=192.168.0.100 # ip绑定port=37000# 端口号dbpath=/home/mongodb/data/logpath=/home/mongodb/log/mongodb.log # 输出日志文件名称pidfilepath=/home/mongodb/mongobd.pid # pid文件名称journal=truemaxConns=50000 # 最大连接数logappend=true# 日志输出方式fork=true # 以守护进程的方式运行,创建服务器进程# httpinterface=false # web界面noauth=true cpu=true$
6.启动并进行简单测试启动有两种方式启动,直接命令行启动,还有一种就是使用配置文件启动,强烈建议使用配置文件启动。
$ /home/mongodb/mongodb3.2.1/bin/mongod -f /home/mongodb/conf/mongodb.conf about to fork child process, waiting until server is ready for connections.forked process: 32485child process started successfully, parent exiting$ ps -ef|grep mongodbroot 32305 322610 13:56 pts/0 00:00:00 su - mongodbmongodb32485 11 14:43 ? 00:00:00 /home/mongodb/mongodb3.2.1/bin/mongod -f /home/mongodb/conf/mongodb.confmongodb32502 323060 14:44 pts/0 00:00:00 grep mongodb$ netstat -anltp|grep 37000(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)tcp 0 0 0.0.0.0:37000 0.0.0.0:* LISTEN 32485/mongod $ $ cat /home/mongodb/log/mongodb.log 2016-01-25T14:43:50.877+0800 I CONTROL MongoDB starting : pid=32485 port=37000 dbpath=/home/mongodb/data/ 64-bit host=aly-lww32016-01-25T14:43:50.877+0800 I CONTROL db version v3.2.1-83-g187028f2016-01-25T14:43:50.877+0800 I CONTROL git version: 187028f283545496b0254216d17822211fe5202c2016-01-25T14:43:50.877+0800 I CONTROL OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 20132016-01-25T14:43:50.877+0800 I CONTROL allocator: tcmalloc2016-01-25T14:43:50.877+0800 I CONTROL modules: none2016-01-25T14:43:50.877+0800 I CONTROL build environment:2016-01-25T14:43:50.877+0800 I CONTROL distmod: rhel622016-01-25T14:43:50.877+0800 I CONTROL distarch: x86_642016-01-25T14:43:50.877+0800 I CONTROL target_arch: x86_642016-01-25T14:43:50.877+0800 I CONTROL options: { config: "/home/mongodb/conf/mongodb.conf", cpu: true, net: { maxIncomingConnections: 50000, port: 37000 }, processManagement: { fork: true, pidFilePath: "/home/mongodb/mongobd.pid" }, security: { authorization: "disabled" }, storage: { dbPath: "/home/mongodb/data/", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/home/mongodb/log/mongodb.log" } }2016-01-25T14:43:50.877+0800 I STORAGE wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(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),2016-01-25T14:43:51.017+0800 I FTDC Initializing full-time diagnostic data capture with directory '/home/mongodb/data/diagnostic.data'2016-01-25T14:43:51.018+0800 I NETWORK Starting hostname canonicalization worker2016-01-25T14:43:51.045+0800 I NETWORK waiting for connections on port 37000$
创建一个快捷启动mongodb的方式$ vim start_mongodb.sh #!/bin/bash/home/mongodb/mongodb3.2.1/bin/mongod -f /home/mongodb/conf/mongodb.conf $ ./start_mongodb.sh about to fork child process, waiting until server is ready for connections.forked process: 32619child process started successfully, parent exiting$ $ cat /home/mongodb/log/mongodb.log 2016-01-25T15:07:04.483+0800 I CONTROL ***** SERVER RESTARTED *****2016-01-25T15:07:04.520+0800 I CONTROL MongoDB starting : pid=32619 port=37000 dbpath=/home/mongodb/data/ 64-bit host=aly-lww32016-01-25T15:07:04.520+0800 I CONTROL db version v3.2.1-83-g187028f2016-01-25T15:07:04.520+0800 I CONTROL git version: 187028f283545496b0254216d17822211fe5202c2016-01-25T15:07:04.520+0800 I CONTROL OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 20132016-01-25T15:07:04.520+0800 I CONTROL allocator: tcmalloc2016-01-25T15:07:04.520+0800 I CONTROL modules: none2016-01-25T15:07:04.520+0800 I CONTROL build environment:2016-01-25T15:07:04.520+0800 I CONTROL distmod: rhel622016-01-25T15:07:04.520+0800 I CONTROL distarch: x86_642016-01-25T15:07:04.520+0800 I CONTROL target_arch: x86_642016-01-25T15:07:04.520+0800 I CONTROL options: { config: "/home/mongodb/conf/mongodb.conf", cpu: true, net: { maxIncomingConnections: 50000, port: 37000 }, processManagement: { fork: true, pidFilePath: "/home/mongodb/mongobd.pid" }, security: { authorization: "disabled" }, storage: { dbPath: "/home/mongodb/data/", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/home/mongodb/log/mongodb.log" } }2016-01-25T15:07:04.521+0800 I - Detected data files in /home/mongodb/data/ created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.2016-01-25T15:07:04.521+0800 I STORAGE wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(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),2016-01-25T15:07:04.913+0800 I FTDC Initializing full-time diagnostic data capture with directory '/home/mongodb/data/diagnostic.data'2016-01-25T15:07:04.914+0800 I NETWORK waiting for connections on port 370002016-01-25T15:07:04.914+0800 I NETWORK Starting hostname canonicalization worker$
7. 关闭mongodb数据库关闭mongodb数据库,这里有三种安全的方式(1) kill -4 pid $ ps -ef|grep 'bin/mongod'|grep -v 'grep'mongodb32485 10 14:43 ? 00:00:07 /home/mongodb/mongodb3.2.1/bin/mongod -f /home/mongodb/conf/mongodb.conf$ ps -ef|grep 'bin/mongod'|grep -v 'grep'|awk -F" " '{print $2}'32485$ kill -4 `ps -ef|grep 'bin/mongod'|grep -v 'grep'|awk -F" " '{print $2}'`日志太多,这里省略...
再次启动看看$ ./start_mongodb.sh about to fork child process, waiting until server is ready for connections.forked process: 32654child process started successfully, parent exiting$ $ tail -f/home/mongodb/log/mongodb.log 2016-01-25T15:13:30.772+0800 I CONTROL ***** SERVER RESTARTED *****2016-01-25T15:13:30.809+0800 I CONTROL MongoDB starting : pid=32654 port=37000 dbpath=/home/mongodb/data/ 64-bit host=aly-lww32016-01-25T15:13:30.809+0800 I CONTROL db version v3.2.1-83-g187028f2016-01-25T15:13:30.810+0800 I CONTROL git version: 187028f283545496b0254216d17822211fe5202c2016-01-25T15:13:30.810+0800 I CONTROL OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 20132016-01-25T15:13:30.810+0800 I CONTROL allocator: tcmalloc2016-01-25T15:13:30.810+0800 I CONTROL modules: none2016-01-25T15:13:30.810+0800 I CONTROL build environment:2016-01-25T15:13:30.810+0800 I CONTROL distmod: rhel622016-01-25T15:13:30.810+0800 I CONTROL distarch: x86_642016-01-25T15:13:30.810+0800 I CONTROL target_arch: x86_642016-01-25T15:13:30.810+0800 I CONTROL options: { config: "/home/mongodb/conf/mongodb.conf", cpu: true, net: { maxIncomingConnections: 50000, port: 37000 }, processManagement: { fork: true, pidFilePath: "/home/mongodb/mongobd.pid" }, security: { authorization: "disabled" }, storage: { dbPath: "/home/mongodb/data/", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/home/mongodb/log/mongodb.log" } }2016-01-25T15:13:30.810+0800 I - Detected data files in /home/mongodb/data/ created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.2016-01-25T15:13:30.810+0800 W - Detected unclean shutdown - /home/mongodb/data/mongod.lock is not empty.2016-01-25T15:13:30.810+0800 W STORAGE Recovering data from the last clean checkpoint.2016-01-25T15:13:30.810+0800 I STORAGE wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(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),2016-01-25T15:13:31.145+0800 I FTDC Initializing full-time diagnostic data capture with directory '/home/mongodb/data/diagnostic.data'2016-01-25T15:13:31.145+0800 I NETWORK waiting for connections on port 370002016-01-25T15:13:31.146+0800 I NETWORK Starting hostname canonicalization worker2016-01-25T15:13:32.019+0800 I FTDC Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK
从启动日志看,自动回复了。
(2) kill -2 pid$ kill -2 `ps -ef|grep 'bin/mongod'|grep -v 'grep'|awk -F" " '{print $2}'`$ > /home/mongodb/log/mongodb.log $ tail -f/home/mongodb/log/mongodb.log 2016-01-25T15:14:49.415+0800 I CONTROL got signal 2 (Interrupt), will terminate after current cmd ends2016-01-25T15:14:49.415+0800 I FTDC Shutting down full-time diagnostic data capture2016-01-25T15:14:49.416+0800 I CONTROL now exiting2016-01-25T15:14:49.416+0800 I NETWORK shutdown: going to close listening sockets...2016-01-25T15:14:49.416+0800 I NETWORK closing listening socket: 62016-01-25T15:14:49.416+0800 I NETWORK closing listening socket: 72016-01-25T15:14:49.416+0800 I NETWORK removing socket file: /tmp/mongodb-37000.sock2016-01-25T15:14:49.417+0800 I NETWORK shutdown: going to flush diaglog...2016-01-25T15:14:49.417+0800 I NETWORK shutdown: going to close sockets...2016-01-25T15:14:49.417+0800 I STORAGE WiredTigerKVEngine shutting down2016-01-25T15:14:49.456+0800 I STORAGE shutdown: removing fs lock...2016-01-25T15:14:49.456+0800 I CONTROL dbexit:rc: 0
再次启动看看 $ cat /home/mongodb/log/mongodb.log 2016-01-25T15:15:22.822+0800 I CONTROL ***** SERVER RESTARTED *****2016-01-25T15:15:22.858+0800 I CONTROL MongoDB starting : pid=32681 port=37000 dbpath=/home/mongodb/data/ 64-bit host=aly-lww32016-01-25T15:15:22.858+0800 I CONTROL db version v3.2.1-83-g187028f2016-01-25T15:15:22.858+0800 I CONTROL git version: 187028f283545496b0254216d17822211fe5202c2016-01-25T15:15:22.858+0800 I CONTROL OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 20132016-01-25T15:15:22.858+0800 I CONTROL allocator: tcmalloc2016-01-25T15:15:22.858+0800 I CONTROL modules: none2016-01-25T15:15:22.858+0800 I CONTROL build environment:2016-01-25T15:15:22.858+0800 I CONTROL distmod: rhel622016-01-25T15:15:22.858+0800 I CONTROL distarch: x86_642016-01-25T15:15:22.858+0800 I CONTROL target_arch: x86_642016-01-25T15:15:22.858+0800 I CONTROL options: { config: "/home/mongodb/conf/mongodb.conf", cpu: true, net: { maxIncomingConnections: 50000, port: 37000 }, processManagement: { fork: true, pidFilePath: "/home/mongodb/mongobd.pid" }, security: { authorization: "disabled" }, storage: { dbPath: "/home/mongodb/data/", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/home/mongodb/log/mongodb.log" } }2016-01-25T15:15:22.858+0800 I - Detected data files in /home/mongodb/data/ created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.2016-01-25T15:15:22.858+0800 I STORAGE wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(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),2016-01-25T15:15:23.194+0800 I FTDC Initializing full-time diagnostic data capture with directory '/home/mongodb/data/diagnostic.data'2016-01-25T15:15:23.195+0800 I NETWORK waiting for connections on port 370002016-01-25T15:15:23.195+0800 I NETWORK Starting hostname canonicalization worker
从关闭和启动日志中看,相对使用kill -4 而言,使用kill -2 更安全些。(3) 使用db.shutdownServer()关闭mongodb数据库 $ /home/mongodb/mongodb3.2.1/bin/mongo --port 37000 MongoDB shell version: 3.2.1-83-g187028fconnecting to: 127.0.0.1:37000/testWelcome to the MongoDB shell.For interactive help, type "help".For more comprehensive documentation, seehttp://docs.mongodb.org/Questions? Try the support grouphttp://groups.google.com/group/mongodb-user> show dbslocal0.000GB> use adminswitched to db admin> db.shutdownServer();server should be down...2016-01-25T15:18:00.919+0800 I NETWORK trying reconnect to 127.0.0.1:37000 (127.0.0.1) failed2016-01-25T15:18:00.921+0800 I NETWORK Socket recv() errno:104 Connection reset by peer 127.0.0.1:370002016-01-25T15:18:00.921+0800 I NETWORK SocketException: remote: (NONE):0 error: 9001 socket exception server 2016-01-25T15:18:00.922+0800 I NETWORK reconnect 127.0.0.1:37000 (127.0.0.1) failed failed > exitbye$
查看关闭日志$ cat /home/mongodb/log/mongodb.log 2016-01-25T15:17:29.958+0800 I NETWORK connection accepted from 127.0.0.1:41509 #1 (1 connection now open)2016-01-25T15:18:00.915+0800 I COMMAND terminating, shutdown command received2016-01-25T15:18:00.915+0800 I FTDC Shutting down full-time diagnostic data capture2016-01-25T15:18:00.917+0800 I CONTROL now exiting2016-01-25T15:18:00.917+0800 I NETWORK shutdown: going to close listening sockets...2016-01-25T15:18:00.917+0800 I NETWORK closing listening socket: 62016-01-25T15:18:00.917+0800 I NETWORK closing listening socket: 72016-01-25T15:18:00.917+0800 I NETWORK removing socket file: /tmp/mongodb-37000.sock2016-01-25T15:18:00.917+0800 I NETWORK shutdown: going to flush diaglog...2016-01-25T15:18:00.917+0800 I NETWORK shutdown: going to close sockets...2016-01-25T15:18:00.917+0800 I STORAGE WiredTigerKVEngine shutting down2016-01-25T15:18:01.000+0800 I STORAGE shutdown: removing fs lock...2016-01-25T15:18:01.000+0800 I CONTROL dbexit:rc: 0$
再次启动看看 $ ./start_mongodb.sh about to fork child process, waiting until server is ready for connections.forked process: 32740child process started successfully, parent exiting$ $ tail -f/home/mongodb/log/mongodb.log 2016-01-25T15:19:00.369+0800 I CONTROL ***** SERVER RESTARTED *****2016-01-25T15:19:00.406+0800 I CONTROL MongoDB starting : pid=32740 port=37000 dbpath=/home/mongodb/data/ 64-bit host=aly-lww32016-01-25T15:19:00.406+0800 I CONTROL db version v3.2.1-83-g187028f2016-01-25T15:19:00.406+0800 I CONTROL git version: 187028f283545496b0254216d17822211fe5202c2016-01-25T15:19:00.406+0800 I CONTROL OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 20132016-01-25T15:19:00.406+0800 I CONTROL allocator: tcmalloc2016-01-25T15:19:00.406+0800 I CONTROL modules: none2016-01-25T15:19:00.406+0800 I CONTROL build environment:2016-01-25T15:19:00.406+0800 I CONTROL distmod: rhel622016-01-25T15:19:00.406+0800 I CONTROL distarch: x86_642016-01-25T15:19:00.407+0800 I CONTROL target_arch: x86_642016-01-25T15:19:00.407+0800 I CONTROL options: { config: "/home/mongodb/conf/mongodb.conf", cpu: true, net: { maxIncomingConnections: 50000, port: 37000 }, processManagement: { fork: true, pidFilePath: "/home/mongodb/mongobd.pid" }, security: { authorization: "disabled" }, storage: { dbPath: "/home/mongodb/data/", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/home/mongodb/log/mongodb.log" } }2016-01-25T15:19:00.407+0800 I - Detected data files in /home/mongodb/data/ created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.2016-01-25T15:19:00.407+0800 I STORAGE wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(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),2016-01-25T15:19:00.759+0800 I FTDC Initializing full-time diagnostic data capture with directory '/home/mongodb/data/diagnostic.data'2016-01-25T15:19:00.760+0800 I NETWORK waiting for connections on port 370002016-01-25T15:19:00.760+0800 I NETWORK Starting hostname canonicalization worker
可见,使用db.shutdownServer()和使用kill -2 比较放心些。所以我们最好能采用这两种的其中一个即可。
7.总结 本文档是基于目前相对较新的版本在测试过境中搭建的过程,而且是单机环境。目的是从头开始尝试mongodb这种NoSQL数据库的使用。个人看重的除了mongodb自身的优势外,最喜欢它的复制集和分片功能。在这两点上,目前关系型数据库实现起来相对较复杂,不易维护管理。
页:
[1]