准备
mkdir /data/mongodb/data -- 数据
mkdir /data/mongodb/log -- 日志
下载https://www.mongodb.com/download-center?jmp=nav#community
安装
tar zxvf mongodb-linux-x86_64-rhel70-3.4.3.tgz
mv mongodb-linux-x86_64-rhel70-3.4.3/ /usr/local/mongodb
配置环境变量:
export PATH=/user/local/mongodb/bin:$PATH
source /etc/profile
vim /etc/mongodb.conf
#数据文件存放目录,如果不指定,则/data/db是mongodb的默认启动的数据库路径
dbpath = /data/mongodb/data
#日志文件存放目录
logpath = /data/mongodb/log
#端口
port = 27017
#以守护程序的方式启用,即在后台运行
fork = true
#是否禁止http接口,即28017 端口开启的服务。默认false,支持
nohttpinterface = true
mongod --dbpath=/data/mongodb/data/ --logpath=/data/mongodb/log/mongodb.log --logappend
配置文件方式启动
mongod -f /etc/mongodb.conf
[root@centos7 mongodb]# netstat -tnlp | grep mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 36767/mongod
连接
[root@centos7 log]# mongo
MongoDB shell version v3.4.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.3
Welcome to the MongoDB shell.
消除警告
vi /etc/rc.local
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
ulimit -u 65535
[root@oracledb ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@oracledb ~]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
停止服务:
> use admin
switched to db admin
> db.shutdownServer();
server should be down...
> quit
function quit() {
[native code]
}
> exit
bye
[root@oracledb ~]# mongo
MongoDB shell version v3.4.0
connecting to: mongodb://127.0.0.1:27017
2016-12-13T16:22:32.757+0800 W NETWORK [main] Failed to connect to 127.0.0.1:27017, reason: Connection refused
2016-12-13T16:22:32.757+0800 E QUERY [main] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:234:13
@(connect):1:6
exception: connect failed
一些操作:
> show databases;
admin 0.000GB
local 0.000GB
> db.c1.insert({x:1})
WriteResult({ "nInserted" : 1 })
> db.c1.find()
{ "_id" : ObjectId("58e8c41841c89ff0cd873828"), "x" : 1 }
> show databases;
admin 0.000GB
local 0.000GB
test 0.000GB
> use test
switched to db test
> show tables;
c1
启动时候报错:
[root@centos7 log]# mongod -f /etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 36697
ERROR: child process failed, exited with error number 48
这时候删除lock文件,
[root@centos7 log]# rm /data/mongodb/data/mongod.lock
rm: remove regular empty file ‘/data/mongodb/data/mongod.lock’? y
[root@centos7 log]# rm /data/mongodb/log/mongodb.log
rm: remove regular file ‘/data/mongodb/log/mongodb.log’?
将进程关闭,重新启动即可
附,windows下安装
windows 下安装配置
1,安装软件,一步一步操作,然后添加环境变量path中
2,配置配置文件mongodb.conf
创建一个放置配置文件,数据文件,日志文件的路径,比如我的配置文件I:\mongodb\mongodb.conf
内容:
dbpath=I:\mongodb\data
logpath=I:\mongodb\logs\mongodb.log
logappend=true #错误日志采用追加模式
journal=true #启用日志文件,默认启用
quiet=true #这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
port=27017 #端口号 默认为27017
3,启动
普通启动
mongod --config I:\mongodb\mongodb.conf
注册为windows服务
C:\Users\Administrator>mongod --config I:\mongodb\mongodb.conf --install
4,访问
C:\Users\Administrator>mongo
MongoDB shell version v3.4.5
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.5
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-02T18:18:30.881+0800 I CONTROL [initandlisten]
2017-07-02T18:18:30.881+0800 I CONTROL [initandlisten] ** WARNING: Access contr
ol is not enabled for the database.
2017-07-02T18:18:30.881+0800 I CONTROL [initandlisten] ** Read and wri
te access to data and configuration is unrestricted.
2017-07-02T18:18:30.881+0800 I CONTROL [initandlisten]
2017-07-02T18:18:30.882+0800 I CONTROL [initandlisten] Hotfix KB2731284 or late
r update is not installed, will zero-out data files.
2017-07-02T18:18:30.882+0800 I CONTROL [initandlisten]
> show databases;
admin 0.000GB
local 0.000GB