这里为了方便使用yum库进行安装,
系统本身的服务器是阿里云服务器,yum源已经设置为了阿里云
安装命令:
yum -y install mongodb-org
查看Mongdb 的安装路径和配置路径
[root@iZm5e400nzm6ck0cz0u32aZ ~]# whereis mongod
mongod: /usr/bin/mongod /etc/mongod.conf /usr/share/man/man1/mongod.1
安装完成后需要启动
[root@iZm5e400nzm6ck0cz0u32aZ ~]# systemctl start mongod.service
[root@iZm5e400nzm6ck0cz0u32aZ ~]# systemctl stop mongod.service
[root@iZm5e400nzm6ck0cz0u32aZ ~]# systemctl status mongod.service
登录mongd的客户端
[root@iZm5e400nzm6ck0cz0u32aZ ~]# mongo 127.0.0.1:27017
MongoDB shell version v4.0.12
connecting to: mongodb://127.0.0.1:27018/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("93f3ae76-1539-4d70-ad68-4097eaf0ea2c") }
MongoDB server version: 4.0.12
新建用户和数据库
输入如下信息,对admin 数据库创建管理员账号,对test书库创建读写账号
Use admin
db.createUser(
{
user: "admin",
pwd: "admin123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
use test
db.createUser(
{
user:'test',
pwd:'test123',
roles:[{role:'readWrite',db:'test'}]
})
设置配置文件
# network interfaces
net:
port: 27017 # 设置端口号
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
security:
authorization: enabled
需要注意的是bindIp 设置为 0.0.0.0 任何ip都可以访问,如果设置固定的,只能固定ip 访问。
然后重启服务
[root@iZm5e400nzm6ck0cz0u32aZ ~]# systemctl restart mongod.service
设置服务开机启动
[root@iZm5e400nzm6ck0cz0u32aZ ~]# systemctl enable mongod.service
如果有防火墙的话,把对应的端口号放开即可,默认是27017