MongoDB 関連の知識

1. 共通操作

1.1 パスワードの変更

  • 最初に元のアカウントのパスワードを使用してログインします。
  • db.changeUserPassword を通じてパスワードを変更します。
[root@localhost ~]# docker exec -it mongodb /bin/bash
root@93d0002a2f65:/# mongosh
Current Mongosh Log ID: 64b4909af6f5b1620fdc058d
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.1
Using MongoDB:          6.0.8
Using Mongosh:          1.10.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test> use admin
switched to db admin
admin> db.changeUserPassword('xxxx','123456')
MongoServerError: command updateUser requires authentication
admin> db.auth("xxxx","dba.123456")
{
    
     ok: 1 }
admin> db.changeUserPassword('xxxx','123456')
{
    
     ok: 1 }
admin>

1.2 データベースを削除する

ここに画像の説明を挿入

  • 次のコマンドラインを使用して、Windows で mongDB を入力します。
D:\mongodb\mongodb-win32-x86_64-2008plus-ssl-3.6.23-8-gc2609ed3ed\bin>mongod.exe --dbpath D:\mongodb\mongodb-win32-x86_64-2008plus-ssl-3.6.23-8-gc2609ed3ed\data\db
  • 上記のコマンドラインのウィンドウは閉じられていません。cmd を使用してウィンドウを再起動し、mongo と入力します。
    ここに画像の説明を挿入
  • 削除操作、削除に失敗しました、管理データベースを削除できないようです
> show dbs
admin        0.000GB
config       0.000GB
local        0.000GB
mongdb_test  0.000GB
> use admin
switched to db admin
> db.dropDatabase()
{
    
    
        "ok" : 0,
        "errmsg" : "Dropping the 'admin' database is prohibited.",
        "code" : 20,
        "codeName" : "IllegalOperation"
}
> show dbs
admin        0.000GB
config       0.000GB
local        0.000GB
mongdb_test  0.000GB

ただし、他のものは削除できます

D:\mongodb\mongodb-win32-x86_64-2008plus-ssl-3.6.23-8-gc2609ed3ed\bin>mongo 192.168.3.13:27017
MongoDB shell version v3.6.23-8-gc2609ed3ed
connecting to: mongodb://192.168.3.13:27017/test?gssapiServiceName=mongodb
Implicit session: session {
    
     "id" : UUID("4d847aa7-6b6b-4dfb-ac6e-7fd9fcacb95b") }
MongoDB server version: 3.6.23-8-gc2609ed3ed
Server has startup warnings:
2023-07-09T16:47:02.248+0800 I CONTROL  [initandlisten]
2023-07-09T16:47:02.248+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2023-07-09T16:47:02.248+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2023-07-09T16:47:02.248+0800 I CONTROL  [initandlisten]
> show dbs
admin        0.000GB
local        0.000GB
mongdb_test  0.000GB
> use local
switched to db local
> db.dropDatabase()
{
    
     "dropped" : "local", "ok" : 1 }
>

1.3スタート

  • mongoDB インストール ディレクトリの bin ディレクトリで、管理者として cmd を起動し、その中に「net start net start MongoDB」と入力します。

2. 質問セット

2.1 mongDB データベースにリモートでアクセスする方法

  1. サーバー A の IP: 192.168.3.13。MongoDB はすでにインストールされています。データはこの
    サーバー B ip: 192.168.3.5 にあります。
  2. コアは「--bind_ip」の下にあります
.\mongod.exe --bind_ip 192.168.3.13 --port 27017 --config D:\mongodb\mongodb-win32-x86_64-2008plus-ssl-3.6.23-8-gc2609ed3ed\etc\mongodb.conf --dbpath=D:\mongodb\mongodb-win32-x86_64-2008plus-ssl-3.6.23-8-gc2609ed3ed\data\db"
  1. cmd を開き、mongo 192.168.3.13:27017 と入力して mongoDB にアクセスします。
D:\mongodb\mongodb-win32-x86_64-2008plus-ssl-3.6.23-8-gc2609ed3ed\bin>mongo 192.168.3.13:27017
MongoDB shell version v3.6.23-8-gc2609ed3ed
connecting to: mongodb://192.168.3.13:27017/test?gssapiServiceName=mongodb
Implicit session: session {
    
     "id" : UUID("ab5c7e95-4b79-437d-9f9a-ea0ba12f0b78") }
MongoDB server version: 3.6.23-8-gc2609ed3ed
Server has startup warnings:
2023-07-09T16:47:02.248+0800 I CONTROL  [initandlisten]
2023-07-09T16:47:02.248+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2023-07-09T16:47:02.248+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2023-07-09T16:47:02.248+0800 I CONTROL  [initandlisten]
>

2.2 widows のセキュリティ ポリシーにより、Linux は Windows の mongDB にアクセスできません

  • まず、「コントロール パネル\システムとセキュリティ\Windows Defender ファイアウォール-詳細設定-新しいルール-ポート-tcp-特定のローカル ポート番号」で 27017 を選択します。
  • Linux で Telnet 192.168.3.13 27017 を使用します。192.168.3.13 が私の勝利 IP であり、表示は次のようになり、アクセスできることを示しています。
    ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/eettttttt/article/details/131356527