docker install MongoDB, configure the user name and password.

Took over learning docker install MongoDB.

Download MongoDB Mirror

docker pull mongo

Here Insert Picture Description

Run the install command (container mounting MongoDB):

docker run --name mongodb -p 27017:27017 -d mongo --auth

MongoDB administrator to add users
to enter MongoDB

docker exec -it 51a5b5e05fe4 mongo admin

51a5b5e05fe4: id MongoDB container

Create an admin administrator account:

db.createUser({ user: 'root', pwd: 'root', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });

Exit exit
of the operation record

docker exec -it 51a5b5e05fe4 mongo admin
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("04ebc64f-df58-4931-8a68-4824ed032dd4") }
MongoDB server version: 4.0.10
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

> db.createUser({ user: 'root', pwd: 'root', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
Successfully added user: {
	"user" : "root",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}
> exit
bye

Create a regular user, password, and database

Enter the admin user mongo

docker exec -it 51a5b5e05fe4 mongo admin

To authenticate admin:
PS: Administrator account password is created in the previous step

db.auth("root","root");

Create a user, password, and database:

db.createUser({ user: 'zy', pwd: 'zy123456', roles: [ { role: "readWrite", db: "app" } ] });

drop out

exit

The above-described recording operation

[root@localhost ~]# docker exec -it 51a5b5e05fe4 mongo admin
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("eccdd06a-f2e0-40f8-924d-173a3cbb7198") }
MongoDB server version: 4.0.10

> db.auth("root","root");
1
> db.createUser({ user: 'zy', pwd: 'zy123456', roles: [ { role: "readWrite", db: "app" } ] });
Successfully added user: {
	"user" : "swen",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "app"
		}
	]
}
> exit
bye

Login APP database

The admin user to enter mongo:

docker exec -it 51a5b5e05fe4  mongo admin

Zy for identity authentication:

db.auth("zy","zy123456");

Switching Database

use app

adding data

db.test.save({name:"zhangsan"});

The above-described recording operation

[root@localhost ~]# docker exec -it 51a5b5e05fe4 mongo admin
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("48474e4e-b226-401c-ab00-62b1bae80def") }
MongoDB server version: 4.0.10
> db.auth("zy","zy123456");
1
> use app
switched to db app
> db.test.save({name:"zhangsan"});
WriteResult({ "nInserted" : 1 })
> exit
bye



Guess you like

Origin blog.csdn.net/weixin_44591832/article/details/91953189