前置说明
本机ip(mongo服务器ip):10.217.232.36
向mongo中新增一个授权用户
单机情况下的mongo 默认是免认证登录。
# /usr/bin/mongo
新增一个授权用户,访问授权db:actionview
db.createUser({ user: "panxin", pwd: "123dsfdsa", roles: [{ role: "dbOwner", db: "actionview" }] })
修改mongo的配置文件
# vim /www/server/mongodb/config.conf
# network interfaces
net:
port: 27017
#bindIp: 127.0.0.1
security:
authorization: enabled
javascriptEnabled: false
注释bindIp,修改authorization的值为“enabled”
重启mongod服务
# /usr/bin/mongod -f /www/server/mongodb/config.conf --bind_ip=0.0.0.0
如果在启动mongo服务时,不显示给定选项“--bind_ip=0.0.0.0”,将无法监听其他ip发来的连接请求,导致无法被非本机客户端连接。
这点尤其重要,我在启动时没有给定显示选项“--bind_ip=0.0.0.0”,导致网上漫查,浪费了2个小时的宝贵生命。还是要回归官方文档,
我是通过耐心阅读繁细的帮助文档才发现如下信息,解决了这个问题:
# /usr/bin/mongod -h
帮助文档中有这样一段:
Options:
General options:
-v [ --verbose ] [=arg(=v)] be more verbose (include multiple times
for more verbosity e.g. -vvvvv)
--quiet quieter output
--port arg specify port number - 27017 by default
--logpath arg log file to send write to instead of
stdout - has to be a file, not
directory
--syslog log to system's syslog facility instead
of file or stdout
--syslogFacility arg syslog facility used for mongodb syslog
message
--logappend append to logpath instead of
over-writing
--logRotate arg set the log rotation behavior
(rename|reopen)
--timeStampFormat arg Desired format for timestamps in log
messages. One of ctime, iso8601-utc or
iso8601-local
--setParameter arg Set a configurable parameter
-h [ --help ] show this usage information
--version show version information
-f [ --config ] arg configuration file specifying
additional options
--bind_ip arg comma separated list of ip addresses to
listen on - localhost by default
******
重点看选项“--bind_ip arg”的说明,其中有一句“ip addresses to listen on - localhost by default”。
所以不显示指定,就是只监听本机的连接。
重点心得:珍惜生命,多阅读官方文档,少浪费生命在上网查找那些东边抄西边拼、你抄我我抄你的同质化、同内容文章。
通过其他mongo客户端进行连接测试
mongo 10.217.232.36:27017/actionview -u "panxin" -p "123dsfdsa"