CentOS8安装Mongodb 并开机启动

视频讲解地址  https://www.bilibili.com/video/BV1iK411R7ch/

mongodb服务端软件下载地址  https://www.mongodb.com/try/download/community

image.png


我下载的包是mongodb-linux-x86_64-rhel80-6.0.3.tgz   放在了ceontos   /opt 目录下

解压文件

tar -zxvf mongodb-linux-x86_64-rhel80-6.0.3.tgz

分别创建数据和日志文件目录,下面是数据和日志文件目录完全路径

/opt/mongodb-linux-x86_64-rhel80-6.0.3/data/
/opt/mongodb-linux-x86_64-rhel80-6.0.3/logs/

在/opt/mongodb-linux-x86_64-rhel80-6.0.3/目录下创建mongodb.conf 配置文件,配置内容如下

#添加以下内容
#可外部访问
bind_ip=0.0.0.0
#端口
port = 27017
# 数据库文件存放路径
dbpath = /opt/mongodb-linux-x86_64-rhel80-6.0.3/data/
# 日至文件存放路径
logpath = /opt/mongodb-linux-x86_64-rhel80-6.0.3/logs/mongodb.log
# 使用追加的方式写日志              
logappend = true
# 以守护进程的方式运行,创建服务器进程         
fork = true
# 最大同时连接数            
maxConns = 100
# 不启用验证
noauth = true
# 每次写入会记录一条操作日志(通过journal可以重新构造出写入的数据)。            
journal = true

启动mongodb命令如下,但重启会失效

/opt/mongodb-linux-x86_64-rhel80-6.0.3/bin/mongod --config /opt/mongodb-linux-x86_64-rhel80-6.0.3/mongodb.conf

如果想开机自动启动,需要创建开始启动服务mongodb.service(文件放在 /usr/lib/systemd/system 目录下),服务配置内容如下 

[Unit]  
Description=mongodb   
After=network.target remote-fs.target nss-lookup.target  
  
[Service]  
Type=forking  
ExecStart=/opt/mongodb-linux-x86_64-rhel80-6.0.3/bin/mongod --config /opt/mongodb-linux-x86_64-rhel80-6.0.3/mongodb.conf 
ExecReload=/bin/kill -s HUP $MAINPID  
ExecStop=/opt/mongodb-linux-x86_64-rhel80-6.0.3/bin/mongod  --shutdown --config  /opt/mongodb-linux-x86_64-rhel80-6.0.3/mongodb.conf 
PrivateTmp=true  
    
[Install]  
WantedBy=multi-user.target

相关的执行命令

# systemctl enable mongodb.service  #启动开机启动

# systemctl disable mongodb.service  #如果不想开机自启动了,可以使用下面的命令取消开机自启动

# systemctl start mongodb.service          启动mongodb服务
# systemctl stop mongodb.service           停止服务
# systemctl restart mongodb.service        重新启动服务


可视化工具下载地址 https://www.mongodbmanager.com/download


本文地址 http://www.luofenming.com/show.aspx?id=8da7eca46b7a48ff8ea5a632c88ace21