mkdir -p /data/db
Log file path
sudo mkdir -p /var/log/mongodb
Use background running mode:
sudo mongod --dbpath /data/db --logpath /var/log/mongodb/mongod.log --fork
Cut to the bin directory of the mongodb installation directory and execute:
./mongo 127.0.0.1 No problem, you can execute the mongo shell directory,
But when I use a specific IP, such as: ./mongo/192.168.1.123, it prompts an error
connection attempt failed: SocketException: Error connecting to 192.168.1.123:27017 :: caused by :: Connection refuse
After tossing for a long time, I couldn't find the reason. Later I found out that it was because of the setting of the IP binding of the conf configuration file. Note that the new version of the mongodb configuration file is in the bin directory, so I created mongodb.conf
The main configuration is as follows:
dbpath=/data/db
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 0.0.0.0
port = 27017
fork = true
I thought it was OK this time, but the result still didn’t work, the same error...
My startup command came from the official website: mongod --dbpath /data/db --logpath /var/log/mongodb/mongod.log --fork
or remote Can't connect, the setting has no effect? why?
It suddenly occurred to me that the startup command did not specify to read the mongodb.conf configuration, and of course it would not work.
So changed to:
mongod --config mongodb.conf --dbpath /data/db --logpath /var/log/mongodb/mongod.log --fork
Everything is normal.
Refer to the official website documentation, installation steps: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu-tarball/
Supplement
mongodb does not require authentication by default, and authentication is definitely required to access the database in production. For this reason, you need to add users to the added database and grant permissions.
Switch to the bin directory and type mongo
use admin
db.createUser({user:"admin",pwd:"admin",roles:["root"]})
If you need to create a custom database, use the following:
use mydatabase
db.createUser({user: "kta ”, pwd: “kta”, roles: [{role: “dbOwner”, db: “mydatabase”} ], mechanisms: [“SCRAM-SHA-1”]})
Now we have created two users and a database
Next, you need to change mongod.cfg, you can add one in the bin directory, and add the following configuration:
security:
authorization: enabled
Attach the whole content:
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
Restart the mongo service
mongod --config mongodb.conf --auth --dbpath /data/db --logpath /var/log/mongodb/mongod.log --fork
This completes the authentication configuration required for login.