配置Mongodb两种方式

Posted Kurt-Code

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了配置Mongodb两种方式相关的知识,希望对你有一定的参考价值。

手动上传方式

Mongodb-Centos7


安装之前准备工作

下载地址:https://www.mongodb.org/dl/linux

环境说明:下载地址
1系统虚拟机信息:CentOS7 X86_64位;
2软件及版本:mongodb-linux-x86_64-3.6.3.tgz;Xshell工具
MongoDB 提供了 linux 各发行版本 64 位的安装包,你可以在官网下载安装包,地址如下:mongodb官网,进入官网,如下图所示:


点击Download,进入下载页面:

这里写图片描述

选择版本:Linux 64-bit legacy x64,点击DOWNLOAD,点击下载。保存到本地。
安装
启动CentOS7,xshell连接到CentOS7,并且将mongodb-linux-x86_64-3.6.3.tgz上传到CentOS7的/usr/local,具体上传filezilla工具。上传以后列表如下图所示:
这里写图片描述

解压缩:tar zxvf mongodb-linux-x86_64-3.6.3.tgz
重命名: mv mongodb-linux-x86_64-3.6.3 mongodb
删除: rm -rf mongodb-linux-x86_64-3.6.3.tgz

这里写图片描述

解压后然后将mongodb移动到/usr/local下的文件如下图所示

这里写图片描述

然后将mongodb移动到/usr/local下

mv mongodb /usr/local

进入mongodb目录,创建data目录,logs目录和log文件

cd mongodb && mkdir data logs && touch /usr/local/mongodb/logs/log && mkdir conf && cd conf && touch mongo.conf && vim mongo.conf
touch conf/mongo.conf

进入/usr/local/mongodb/bin目录下,启动mongodb服务

方式1:配置启动,将以下配置写入mongo.conf

# 配置文件
# 数据指定位置
dbpath = /usr/local/mongodb/data
# 日志指定位置
logpath = /usr/local/mongodb/logs/log
# 每个数据库将被保存在一个单独的目录
directoryperdb = false
# 日志追加形式写入
logappend = true
port = 27017
# 守护进程启动
fork = true
# 指定可连接的主机
bind_ip = 0.0.0.0
# 启动验证  [配置完验证用户后启用]
# auth = false

同时修改/etc/mongod.conf文件,对其进行修改,mongodb安装好后开机自启会加载
logpth dbpath bindip
之后重启

开机自启脚本

vim /etc/init.d/mongodb 

脚本

#!/bin/sh  
#  
#chkconfig: 2345 80 90  
#description: mongodb  
start() {  
 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongo.conf  
}  
  
stop() {  
  /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown  
}  
  
case "$1" in  
  start)  
 start  
 ;;  
  stop)  
 stop  
 ;;  
  restart)  
 stop  
 start  
 ;;  
  *)  
 echo $"Usage: $0 {start|stop|restart}"  
 exit 1  
esac  

生效

cd /etc/init.d  
chkconfig --add mongodb  
chmod +x  mongodb  
chkconfig mongodb on  

启动

mongod -f /usr/local/mongodb/conf/mongo.conf

方式2:参数启动

cd bin/ && ./mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs/log --logappend --bind_ip=0.0.0.0  --port=27017 --fork
其中的--bind_ip=0.0.0.0是为了开启远程客户端连接

这里写图片描述

启动客户端,进入/usr/local/mongodb/bin目录下

./mongo

这里写图片描述


解决MongoDB登录的WARNING

1.安装完成运行mongodb警告内容如下:

2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] 
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] 
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] 
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is \'always\'.
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **        We suggest setting it to \'never\'
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] 
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is \'always\'.
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **        We suggest setting it to \'never\'

警告一:

2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is \'always\'.
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **        We suggest setting it to \'never\'
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] 
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is \'always\'.
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **        We suggest setting it to \'never\'

1.原因自查--解决:

# 在系统 /etc/rc.local 设置 transparent_hugepage 为 never
[root@server_node01#>> /data/db]#cat /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

2.添加该文件的可执行权限:

chmod +x /etc/rc.d/rc.local 

3.重启下系统:

reboot

警告二:

2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-01-17T15:41:50.578+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.

设置允许访问该mongodb的地址 --bind_ip:

# 配置文件 添加 --bind_ip 指定可连接的主机

# 数据指定位置
dbpath = /data
# 日志指定位置
logpath = /data/db/logs/log
# 每个数据库将被保存在一个单独的目录
directoryperdb = true
# 日志追加形式写入
logappend = true
port = 27017
# 守护进程启动
fork = true
# 指定可连接的主机
bind_ip = 0.0.0.0
# 启动验证  [配置完验证用户后启用]
# auth = true

或者以这种格式启动
./mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs/log --logappend --bind_ip=0.0.0.0  --port=27017 --fork

警告三:

WARNING: Access control is not enabled for the database.
         Read and write access to data and configuration is unrestricted.

设置密码访问auth=True:
1.登录mongo设置admin 密码

[root@server_node01#>> /usr/local/mongodb]#mongo
> use admin
> db.createUser({user:"root",pwd:"root",roles:[{role:\'root\',db:\'admin\'}]})

2.将配置文件 auth 认证打开

# 数据指定位置
dbpath = /data/db
# 日志指定位置
logpath = /data/db/logs/mongodb.log
# 每个数据库将被保存在一个单独的目录
directoryperdb = true
# 日志追加形式写入
logappend = true
port = 27017
# 守护进程启动
fork = true
# 指定可连接的主机
bind_ip = 0.0.0.0
# 启动验证  [配置完验证用户后启用]
auth = false

重启mongodb 服务

[root@server_node01#>> /data/db]#mongod --shutdown
[root@server_node01#>> /data/db]#mongod -f /usr/local/mongodb/conf/mongod.conf

登录:

[root@server_node01#>> /data/db]#mongo 127.0.0.1:27017/admin -u root -p

警告四:不要用root用户启动 MongoDB服务

2019-01-17T16:13:10.778+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

以上是关于配置Mongodb两种方式的主要内容,如果未能解决你的问题,请参考以下文章

如何将代码片段存储在 mongodb 中?

Mongodb后台daemon方式启动

MongoDB的配置和使用

MongoDB下载安装与测试连接(windows)

Mac下安装和配置mongoDB

MongoDB启动文件配置参数详解