mongodb 环境搭建

1 apt 安装

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

2 命令

2.1 启动 MongoDB

sudo service mongod start

2.2 查看状态 MongoDB

sudo service mongod status

2.3 重新启动 MongoDB

sudo service mongod restart

2.4 停止 MongoDB

sudo service mongod stop

3 卸载 MongoDB

sudo service mongod stop
sudo apt-get purge mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

4 设置开机自启动

sudo systemctl enable mongod

5 开启远程访问

sudo vim /etc/mongod.conf
# 把 bindIp:127.0.0.1 修改为 bindIp:0.0.0.0

# 之后重启服务
sudo service mongod restart

6 故障问题

  • 遇到连接拒绝问题 Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused,执行下面命令可解决

    sudo rm /var/lib/mongodb/mongod.lock
    sudo service mongod restart
    

相关