Realizing remote connection and setting account and password in mongodb on Linux

Time:2021-2-20

Local system: Fedora 28
Server system: CentOS 7

install

Install client locally

sudo dnf install mondodb

Install the server on the server

sudo dnf install mongodb-server

There are many basic operations on the Internet (such as specifying the database path — dbpath, specifying the port — port). I don’t want to describe them here. I think the rookie tutorial is good. Give me a linkhttp://www.runoob.com/mongodb…

Set up remote connection

Add — bind when opening the service_ ip_ The all parameter will be set. You can connect remotely. You can use netstat – nltp to view it

mongod –bind_ip_all
netstat -nltp
Realizing remote connection and setting account and password in mongodb on Linux

If the IP in front of port number 27017 is 0.0.0.0 instead of 127.0.0.1, it means it is correct

Now you can connect on the client side

Set account password

After mongodb is installed by default, only local connection is allowed. At the same time, you can directly connect to mongodb without using any account and password. This is very insecure, so we need to set a password

The server starts the service in non auth mode

mongod –bind_ip_all

Client connection

mongo [yourIP]:27017
use admin

Create administrator account

db.createUser({ user: “useradmin”, pwd: “adminpassword”, roles: [{ role: “userAdminAnyDatabase“, db: “admin” }] })

The user in mongodb is based on the identity role, and the role of the administrator account is useradminanydatabase. “Useradmin” represents the user’s identity and “anydatabase” represents that any database can be managed

Create ordinary users (for security, each database or business should create its own account, so that even if a business password is known by others, other databases will not be affected)

use yourdatabase
db.createUser({ user: “youruser”, pwd: “yourpassword”, roles: [{ role: “dbOwner”, db: “yourdatabase” }] })

After the account is created on the server, the CTRL C is dropped and the service is started again by password verification

Server restart service, auth authentication mode

mongod –auth –bind_ ip_ All / / add — auth to indicate that the user name and password need to be verified

Client connection

Client connection

mongo [yourIP]:27017
Use admin / / you need to select the admin database to verify
db.auth (‘useradmin ‘,’adminpassword’) / / a return of 1 indicates that the verification is successful and all permissions have been obtained

PS: the above method is too cumbersome. In fact, the client user name password connection is similar to MySQL

mongo localhost:27017/admin -U useradmin – P / / if you are an ordinary user, the admin will be changed to your database

–The fork parameter indicates the background startup

mongod –fork –logpath /var/log/mongodb/mongod.log

Background startup is the only way to shut down after startup

mongod –shutdown

Change Password

Open mongod service in non authentication mode

mongod
mongo
use admin
db.changeUserPassword (‘user ‘,’newpassword’); / / the user name must exist

This article updates my personal blog at the same timehttps://blog.yjqing.xin/linux…