阿里云服务器安装 MongoDB 实践。
系统:Ubuntu 16.04
远程工具:putty
安装数据库
1. 导入公匙
1
|
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
|
2.创建 MongoDB 列表
1
2
|
#### Ubuntu 16.04 (Xenial)
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
|
3.重新加载本地包
4.安装 MongoDB
1
|
sudo apt-get install -y mongodb-org
|
5.启动服务
1
|
sudo service mongod start
|
6.检查是否启动成功
检查/var/log/mongodb/mongod.log 是否包含以下日志:
1
|
[initandlisten] waiting for connections on port 27017
|
远程连接
1.修改配置文件
fileZilla 访问并修改 /etc/mongodb.conf
1
2
3
4
5
6
|
net:
port: 27017
bindIp: 0.0.0.0 #修改为0.0.0.0 允许任意ip访问
security:
authorization: enabled #开启访问授权
|
重启服务
1
|
sudo service mongod restart
|
阿里云后台打开 27017 端口访问权限,然后就可以直接远程远程访问了。
2.访问密码
任何人都可以直接访问数据库是不安全的,所以我们还需要添加账号验证。
第一步:创建管理员
1
2
3
4
5
6
7
8
|
use admin
db.createUser(
{
user: "myUserAdmin",
pwd: "abc123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)
|
第二步:创建用户
首先用管理员权限登陆控制台
1
|
mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"
|
然后为指定数据库创建新用户
1
2
3
4
5
6
7
8
|
use test
db.createUser(
{
user: "myTester",
pwd: "xyz123",
roles: [ { role: "readWrite", db: "test" }]
}
)
|
其他常用命令
停止
1
|
sudo service mongod stop
|
重启
1
|
sudo service mongod restart
|
进入命令行
官方文档
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/#overview
https://docs.mongodb.com/manual/tutorial/manage-users-and-roles/f