最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】阿里云ECS服务器中已有的MongoDB的用户名密码和端口

MongoDB crifan 529浏览 0评论

折腾:

【已解决】把本地的音频字幕等数据存储到远程服务器的MongoDB数据库中

期间,需要去为了Mac本地去连接这个远程的阿里云ECS服务器中的MongoDB,需要搞清楚这台服务器中MongoDB的端口和用户名和密码

IP应该就是这个主机的外网IP:x.x.x.x

而用户名和密码,去

mongodb 用户名和密码

MongoDB 用户名密码登录 – 简书

服务器中看了看版本:

<code>[root@naturling-general-01 ~]# mongo
mongo         mongod        mongodump     mongoexport   mongofiles    mongoimport   mongooplog    mongoperf     mongorestore  mongos        mongostat     mongotop      
[root@naturling-general-01 ~]# which mongo
/usr/bin/mongo
[root@naturling-general-01 ~]# which mongod
/usr/bin/mongod
[root@naturling-general-01 ~]# mongo --version
MongoDB shell version: 3.2.19
[root@naturling-general-01 ~]# mongod --version
db version v3.2.19
git version: a9f574de6a566a58b24d126b44a56718d181e989
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64
</code>

是3.2.19的

本地Mac试了半天,连不上外网默认端口:

<code>➜  Homebrew git:(stable) telnet x.x.x.x 27017
Trying x.x.x.x...
^C
</code>

这个倒是希望的。

不过需要确认当前是否真的已经运行了MongoDB的服务了:

【已解决】确认服务器是否已经正在运行MongoDB的服务mongod

看了半天,结果变成了:

发现服务器中的MongoDB的用户名和密码是没有的。

需要自己去新建用户才行:

【已解决】给MongoDB数据库新建用户和权限

然后也先要解决:

【已解决】如何允许外网IP远程访问MongoDB数据库

但是发现了:

只绑定到了本机IP,只运行本机访问:

<code>[root@naturling-general-01 ~]# cat /etc/mongod.conf 
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.


#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
[root@naturling-general-01 ~]# 
</code>

所以外网无法访问。

然后试了试服务器本机,是可以访问和操作的:

<code>[root@naturling-general-01 ~]# mongo
MongoDB shell version: 3.2.19
connecting to: test
Server has startup warnings: 
2018-03-20T22:05:01.142+0800 I CONTROL  [initandlisten] 
2018-03-20T22:05:01.142+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-03-20T22:05:01.142+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-03-20T22:05:01.142+0800 I CONTROL  [initandlisten] 
2018-03-20T22:05:01.142+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-03-20T22:05:01.142+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-03-20T22:05:01.142+0800 I CONTROL  [initandlisten] 
2018-03-20T22:05:01.142+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2018-03-20T22:05:01.142+0800 I CONTROL  [initandlisten] 
&gt; show dbs
local  0.000GB
&gt; help
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        sh.help()                    sharding helpers
        rs.help()                    replica set helpers
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce

        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time &gt;= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use &lt;db_name&gt;                set current database
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell
&gt; use local
switched to db local
&gt; show collections
startup_log
&gt; db.local.find_one()
2018-04-02T09:12:10.325+0800 E QUERY    [thread1] TypeError: db.local.find_one is not a function :
@(shell):1:1

&gt; db.local.find()
&gt; db.test.insertOne({x: 2})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5ac18438bcf33b2035d6269b")
}
&gt; db.test.find()
{ "_id" : ObjectId("5ac18438bcf33b2035d6269b"), "x" : 2 }
&gt; 
</code>

然后试试本地Mac能否连接上远程的Mongo

然后:

【已解决】连接远程mongoDB失败:Failed to connect to after 5000ms milliseconds giving up

另外,参考:

MongoDB添加用户和密码 – CSDN博客

看看对应数据库的用户有哪些:

<code>&gt; show users
&gt; use admin
switched to db admin
&gt; show users
{
        "_id" : "admin.naturling",
        "user" : "naturling",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "gridfs"
                }
        ]
}
&gt; use gridfs
switched to db gridfs
&gt; show users
{
        "_id" : "gridfs.naturling",
        "user" : "naturling",
        "db" : "gridfs",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "gridfs"
                }
        ]
}
&gt; 
</code>

然后尝试delete的操作看看是否正常:

【已解决】mongo命令行中如何删除文件

然后确保了此处的用户是有读写操作的。

【总结】

此处为了知道在线的MongoDB数据库:

(1)端口的话,主要是自己在配置文件中:

<code>/etc/mongod.conf
</code>

的设置:

<code># network interfaces
net:
  port: 27017
</code>

决定的。

(2)用户名和密码

实际上发现是需要自己单独去添加的。

而添加的逻辑是:

mongo shell进去后,先切换到要添加的数据库,然后再去创建用户,设置对应的用户名、密码、操作的权限:

<code>&gt; use gridfs
switched to db gridfs
&gt; db.createUser({
...   user: "naturling",
...   pwd: "pwd",
...   roles: [ { role: "readWrite", db: "gridfs"}]
... })
Successfully added user: {
        "user" : "naturling",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "gridfs"
                }
        ]
}
</code>

然后再切换到对应的数据库去授权该用户,即可:

<code>&gt; use gridfs
switched to db gridfs
&gt; db.auth("naturling", "pwd")
1
</code>

最后可以用:

<code>&gt; use gridfs
switched to db gridfs
&gt; show users
{
        "_id" : "gridfs.naturling",
        "user" : "naturling",
        "db" : "gridfs",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "gridfs"
                }
        ]
}
</code>

去确定权限的确已经添加了。

转载请注明:在路上 » 【已解决】阿里云ECS服务器中已有的MongoDB的用户名密码和端口

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
65 queries in 0.121 seconds, using 18.92MB memory