一直以为是防火墙问题,其实不是,由于mongodb默认bindIp是本机,需要修改配置文件mongodb.conf。
参考:https://juejin.im/post/5b84e4e5e51d4538b406de85
博主的配置
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /usr/local/mongodb/data/db
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /usr/local/mongodb/log/mongodb.log
# network interfaces
net:
port: 27017
bindIpAll: true
# how the process runs
processManagement:
fork: true
#security:
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
修改后重启服务
cd /usr/local/mongodb/ //切换到mongodb
./bin/mongod --config ./conf/mongodb.conf //启动服务
使用控制台连接:
cd /usr/local/mongodb/bin
./mongo
到这里用游览器访问27017端口应该会有文字:
说明可以用数据库工具进行连接了。
如果要创建管理员
进入数据库admin
use admin
添加账号密码
db.createUser({user: "test",pwd: "123", roles: ["root"]})
返回 Successfully added user: { "user" : "test","roles" : [ "root" ] }说明用户创建成功了。
如果想要验证登陆是否成功
db.auth("test",123)
如果返回1说明登陆成功