1.导入包管理系统使用的公钥。
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
执行此命令,如果收到的是OK就是正常的。但是,如果收到指示gnupg
未安装的错误,则可以:
//如果反馈不是OK 则执行这步
sudo apt-get install gnupg
//在执行
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
2.为MongoDB 创建文件
echo "deb [ arch=amd64,arm64,s390x ] http://repo.mongodb.com/apt/ubuntu bionic/mongodb-enterprise/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-enterprise.list
3.安装MongoDB Enterprise软件包
sudo apt-get update
4.安装MongoDB Enterprise软件包
sudo apt-get install -y mongodb-enterprise
注意:安装完软件包之后,需要给文件一些权限。
官方说明:
By default, MongoDB runs using the mongodb
user account. If you change the user that runs the MongoDB process, you must also modify the permission to the /var/lib/mongodb
and /var/log/mongodb
directories to give this user access to these directories.
To specify a different log file directory and data file directory, edit the systemLog.path
and storage.dbPath
settings in the /etc/mongod.conf
. Ensure that the user running MongoDB has access to these directories.
sudo -R mongodb:mongodb /var/lib/mongodb
此处很关键,不然会出错 :mongodb运行状态 Active 等于failed。
更改mongod.conf配置文件中的bindIp地址。将127.0.0.1本地地址改为.0.0.0.0通用地址
5.检查mongodb安装启动是否成功
sudo systemctl start mongod
如果出现:Failed to start mongod.service: Unit mongod.service not found.
sudo systemctl daemon-reload
在执行一次启动(sudo systemctl start mongod)
查看状态:
sudo systemctl status mongod
确保服务器重启后,自启动MongoDB:
sudo systemctl enable mongod
停止MongoDB:
sudo systemctl stop mongod
重启MongoDB:
sudo systemctl restart mongod
6.验证Mongodb
在服务器终端输入:
mongo use adminshow dbs
出现如下:
注意:如果有人出现更改mongod.conf文件配置中的authorization:enabled之后,show不出库,是因为开启了验证模式。
需要执行db.auth('root','123456'),在show dbs。然而我没搞明白的是设置authorization:enabled之后Navicat使用SSH连接不上,因为不知道在那个地方验证。所以我没设置。
创建账号:
db.createUser({ user: "root", pwd: "123456", roles: [ { role: "root", db: "admin" } ] })
db.auth("root","123456")
//反馈1正确。
7.连接Navicat 管理工具。
我使用的是阿里云服务器:控制台-->实例-->(找到所属服务器)-->更多-->网络和安全组-->安全组配置-->添加安全组规则
将端口防火墙打开之后,连接Navicat: