服务器下载安装包node
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-4.0.6.tgz
;tar -zxvf mongodb-linux-x86_64-rhel62-4.0.6/
;mv mongodb-linux-x86_64-rhel62-4.0.6/ /usr/local/mongodb
配置环境变量:export PATH=/usr/local/mongodb/bin:$PATH
,直接执行此命令,只会建立出临时的环境变量,即从新断开链接服务器后会失效;linux
/etc/profile
文件中,添加 export PATH=/usr/local/mongodb/bin:$PATH
;验证是否安装成功:mongod --version
git
db version v4.0.6 git version: caa42a1f75a56c7643d0b68d3880444375ec42e3 OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013 allocator: tcmalloc modules: none build environment: distmod: rhel62 distarch: x86_64 target_arch: x86_64
/data/db
;固然也能够在其余目录下建立,而后经过--dbpath
来指定;mkdir -p /data/db
;这里为了后期好查找,就不建立在根目录下,而是放在mongodb目录下/usr/local/mongodb/data/db
/usr/local/mongodb/logs
/usr/local/mongodb/etc
下建立配置文件mongod.config
:dbpath=/usr/local/mongodb/data # 数据库存放位置(以前建立的) logpath=/usr/local/mongodb/logs/mongodb.log # 数据库日志存放位置(以前建立的) port=27017 fork=true #后台运行 auth=false # 初次配置先关了权限验证登录模式 journal=false
mongod -f /usr/local/mongod/etc/mongod.config
;mongo
db.createUser({user:'root',pwd:'1234567',roles:[{'role':'userAdminAnyDatabase','db':'admin'}]})
Successfully added user: { "user" : "root", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
db.shutdownServer()
关闭.mongod -f /usr/local/mongod/etc/mongod.config
use admin
并进行验证db.auth('root','123456')
,验证成功返回1
失败返回0
;若是不验证或验证失败,是作不了任何操做的vim /etc/rc.d/init.d/mongod
,输入:start() { /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/etc/mongod.config } stop() { /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/etc/mongod.config --shutdown } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; *) echo $"Usage: $0 {start|stop|restart}" exit 1 esac
chmod +x /etc/rc.d/init.d/mongod
;service mongod [start|stop|restart|try-restart|reload|force-reload| status]
来直接管理MongoDB服务啦;[lwh@insnce-4ep /]# service mongod stop 2019-03-10T16:45:22.360+0800 I CONTROL [main] log file "/usr/local/mongodb/logs/mongodb.log" exists; moved to "/usr/local/mongodb/logs/mongodb.log.2019-03-10T08-45-22". killing process with pid: 10652
service mongod start
;ok!!mongo 42.114.29.206:27017/test -u user -p password
报错链接失败,拒绝链接;mongodb
bind_ip=0.0.0.0
绑定公网IP;service mongod restart
about to fork child process, waiting until server is ready for connections. forked process: 11300 ERROR: child process failed, exited with error number 48
决解:shell
data/mongod.lock
文件,并删除mongod.lock
;mongod -f usr/local/mongodb/etc/mongod.config --repair
;service mongod start
“积跬步、行千里”—— 持续更新中~,喜欢的话留下个赞和关注哦!
往期经典好文:数据库