WARNING: Access control is not enabled for the database.

MongoDB shell version v3.4.24

WARNING: Access control is not enabled for the database.
Read and write access to data and configuration is unrestricted.
1)未启用访问控制
2)读写访问不受限制

D:\MongoDB\Server\3.4\bin>mongo
MongoDB shell version v3.4.24
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.24
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
Server has startup warnings:
2023-11-29T16:56:25.314+0800 I CONTROL  [initandlisten]
2023-11-29T16:56:25.314+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2023-11-29T16:56:25.314+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2023-11-29T16:56:25.314+0800 I CONTROL  [initandlisten]
>
> db
test
>
>

MongoDB: Server has startup warnings ''Access control is not enabled for the database'' - Stack Overflow

Mongodb v3.4
You need to do the following to create a secure database:
Make sure the user starting the process has permissions and that the directories exist (/data/db in this case).
1) Start MongoDB without access control.
mongod --port 27017 --dbpath /data/db
2) Connect to the instance.
mongo --port 27017
3) Create the user administrator (in the admin authentication database).
use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
4) Re-start the MongoDB instance with access control.
mongod --auth --port 27017 --dbpath /data/db
5) Connect and authenticate as the user administrator.
mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"
6) Create additional users as needed for your deployment (e.g. in the test authentication database).
use test
db.createUser(
  {
    user: "myTester",
    pwd: "xyz123",
    roles: [ { role: "readWrite", db: "test" },
             { role: "read", db: "reporting" } ]
  }
)
7) Connect and authenticate as myTester.
mongo --port 27017 -u "myTester" -p "xyz123" --authenticationDatabase "test"
I basically just explained the short version of the official docs here: https://docs.mongodb.com/master/tutorial/enable-authentication/

==

1)

D:\MongoDB\Server\3.4\bin>mongo
MongoDB shell version v3.4.24
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.24
Server has startup warnings:
2023-11-29T16:56:25.314+0800 I CONTROL  [initandlisten]
2023-11-29T16:56:25.314+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2023-11-29T16:56:25.314+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2023-11-29T16:56:25.314+0800 I CONTROL  [initandlisten]
>
>
> use admin
switched to db admin
>
> db.createUser({user:"admin",pwd:"123456",roles:[{role:"userAdminAnyDatabase", db:"admin"}]})
Successfully added user: {
        "user" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}
>
>

2)

mongod --auth --port 27017 --dbpath D:\MongoDB\Server\3.4\data\db

【. Is a mongod instance already running?, terminating】

D:\MongoDB\Server\3.4\bin>
D:\MongoDB\Server\3.4\bin>
D:\MongoDB\Server\3.4\bin>mongod --auth --port 27017 --dbpath D:\MongoDB\Server\3.4\data\db
2023-11-30T06:07:07.775+0800 I CONTROL  [initandlisten] MongoDB starting : pid=38192 port=27017 dbpath=D:\MongoDB\Server\3.4\data\db 64-bit host=thinkpad-t440p-zwf
2023-11-30T06:07:07.779+0800 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2023-11-30T06:07:07.780+0800 I CONTROL  [initandlisten] db version v3.4.24
2023-11-30T06:07:07.780+0800 I CONTROL  [initandlisten] git version: 865b4f6a96d0f5425e39a18337105f33e8db504d
2023-11-30T06:07:07.783+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2023-11-30T06:07:07.783+0800 I CONTROL  [initandlisten] modules: none
2023-11-30T06:07:07.784+0800 I CONTROL  [initandlisten] build environment:
2023-11-30T06:07:07.784+0800 I CONTROL  [initandlisten]     distmod: 2008plus
2023-11-30T06:07:07.785+0800 I CONTROL  [initandlisten]     distarch: x86_64
2023-11-30T06:07:07.785+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2023-11-30T06:07:07.786+0800 I CONTROL  [initandlisten] options: { net: { port: 27017 }, security: { authorization: "enabled" }, storage: { dbPath: "D:\MongoDB\Server\3.4\data\db" } }
2023-11-30T06:07:07.788+0800 I STORAGE  [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: D:\MongoDB\Server\3.4\data\db\mongod.lock 另一个程序正在使用此文件,进程无法访问。. Is a mongod instance already running?, terminating
2023-11-30T06:07:07.788+0800 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2023-11-30T06:07:07.788+0800 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2023-11-30T06:07:07.789+0800 I CONTROL  [initandlisten] now exiting
2023-11-30T06:07:07.789+0800 I CONTROL  [initandlisten] shutting down with code:100

D:\MongoDB\Server\3.4\bin>

3)任务管理器结束掉

4)

mongod --auth --port 27017 --dbpath D:\MongoDB\Server\3.4\data\db


Successfully authenticated as principal admin on admin from client 127.0.0.1:52564
Unauthorized: not authorized on admin to execute command { getLog: "startupWarnings" }
Unauthorized: not authorized on admin to execute command { getCmdLineOpts: 1.0 }
Unauthorized: not authorized on admin to execute command { replSetGetStatus: 1.0, forShell: 1.0 }

Unauthorized: not authorized on admin to execute command-CSDN博客

D:\MongoDB\Server\3.4\bin>mongod --auth --port 27017 --dbpath D:\MongoDB\Server\3.4\data\db
2023-11-30T06:15:18.468+0800 I CONTROL  [initandlisten] MongoDB starting : pid=9644 port=27017 dbpath=D:\MongoDB\Server\3.4\data\db 64-bit host=thinkpad-t440p-zwf
2023-11-30T06:15:18.472+0800 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2023-11-30T06:15:18.472+0800 I CONTROL  [initandlisten] db version v3.4.24
2023-11-30T06:15:18.472+0800 I CONTROL  [initandlisten] git version: 865b4f6a96d0f5425e39a18337105f33e8db504d
2023-11-30T06:15:18.472+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2023-11-30T06:15:18.473+0800 I CONTROL  [initandlisten] modules: none
2023-11-30T06:15:18.473+0800 I CONTROL  [initandlisten] build environment:
2023-11-30T06:15:18.473+0800 I CONTROL  [initandlisten]     distmod: 2008plus
2023-11-30T06:15:18.473+0800 I CONTROL  [initandlisten]     distarch: x86_64
2023-11-30T06:15:18.473+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2023-11-30T06:15:18.473+0800 I CONTROL  [initandlisten] options: { net: { port: 27017 }, security: { authorization: "enabled" }, storage: { dbPath: "D:\MongoDB\Server\3.4\data\db" } }
2023-11-30T06:15:18.475+0800 I -        [initandlisten] Detected data files in D:\MongoDB\Server\3.4\data\db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2023-11-30T06:15:18.476+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7523M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),verbose=(recovery_progress),
2023-11-30T06:15:18.781+0800 I STORAGE  [initandlisten] WiredTiger message [1701296118:781568][9644:140709756164912], txn-recover: Main recovery loop: starting at 2/5504
2023-11-30T06:15:18.946+0800 I STORAGE  [initandlisten] WiredTiger message [1701296118:945168][9644:140709756164912], txn-recover: Recovering log 2 through 3
2023-11-30T06:15:19.068+0800 I STORAGE  [initandlisten] WiredTiger message [1701296119:67846][9644:140709756164912], txn-recover: Recovering log 3 through 3
2023-11-30T06:15:20.419+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'D:/MongoDB/Server/3.4/data/db/diagnostic.data'
2023-11-30T06:15:20.427+0800 I NETWORK  [thread1] waiting for connections on port 27017
2023-11-30T06:15:25.062+0800 I NETWORK  [thread1] connection accepted from 127.0.0.1:52506 #1 (1 connection now open)
2023-11-30T06:15:25.065+0800 I NETWORK  [conn1] received client metadata from 127.0.0.1:52506 conn1: { driver: { name: "mongo-java-driver|sync|spring-boot", version: "4.6.1" }, os: { type: "Windows", name: "Windows 10", architecture: "amd64", version: "10.0" }, platform: "Java/Oracle Corporation/1.8.0_341-b10" }
2023-11-30T06:15:26.157+0800 I NETWORK  [thread1] connection accepted from 127.0.0.1:52510 #2 (2 connections now open)
2023-11-30T06:15:26.162+0800 I NETWORK  [conn2] received client metadata from 127.0.0.1:52510 conn2: { driver: { name: "mongo-java-driver|sync|spring-boot", version: "4.6.1" }, os: { type: "Windows", name: "Windows 10", architecture: "amd64", version: "10.0" }, platform: "Java/Oracle Corporation/1.8.0_341-b10" }
2023-11-30T06:16:48.976+0800 I NETWORK  [thread1] connection accepted from 127.0.0.1:52564 #3 (3 connections now open)
2023-11-30T06:16:48.977+0800 I NETWORK  [conn3] received client metadata from 127.0.0.1:52564 conn3: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "3.4.24" }, os: { type: "Windows", name: "Microsoft Windows 8", architecture: "x86_64", version: "6.2 (build 9200)" } }
2023-11-30T06:16:48.999+0800 I ACCESS   [conn3] Successfully authenticated as principal admin on admin from client 127.0.0.1:52564
2023-11-30T06:16:49.022+0800 I ACCESS   [conn3] Unauthorized: not authorized on admin to execute command { getLog: "startupWarnings" }
2023-11-30T06:16:49.038+0800 I ACCESS   [conn3] Unauthorized: not authorized on admin to execute command { getCmdLineOpts: 1.0 }
2023-11-30T06:16:49.040+0800 I ACCESS   [conn3] Unauthorized: not authorized on admin to execute command { replSetGetStatus: 1.0, forShell: 1.0 }

5)mongo --port 27017 -u "admin" -p "123456" --authenticationDatabase "admin"

Microsoft Windows [版本 10.0.19045.2965]
(c) Microsoft Corporation。保留所有权利。

C:\Users\Administrator>d:

D:\>
D:\>cd D:\MongoDB\Server\3.4\bin

D:\MongoDB\Server\3.4\bin>
D:\MongoDB\Server\3.4\bin>
D:\MongoDB\Server\3.4\bin>mongo --port 27017 -u "admin" -p "123456" --authenticationDatabase "admin"
MongoDB shell version v3.4.24
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 3.4.24

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值