Centos7 installation mongodb record

Time:2021-10-17

In order to demonstrate the remote installation of mongodb, Tencent cloud recently bought a campus cloud 1-core 2G server at a real price, so it bought one. The activity address is very suitable for students. It seems that not students can experience it for a year, so Xiaobian will experience it with a trumpet.

Cloud + Campus – Tencent cloud

Download decompression

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.2.tgz
tar -zxvf mongodb-linux-x86_64-3.6.2.tgz
mv mongodb-linux-x86_64-3.6.2.tgz/ /usr/local/mongodb
cd /usr/local/mongodb
mkdir data
mkdir data/db
mkdir data/logs
cd bin/
/Usr / local / mongodb / bin / mongod -- dbpath = / usr / local / mongodb / data / DB -- logpath = / usr / local / mongodb / data / logs / mongodb.log -- fork -- start mongodb
PS aux | grep mongodb -- view mongodb process
Kill - 15 PID -- close mongodb service
Netstat - lap | grep 27017 -- check whether the port has been started
Another profile startup method
mkdir /usr/local/mongodb/conf
cd /usr/local/mongodb/conf
vim mongodb.conf
   dbpath=/usr/local/mongodb/data/db
   Logpath = / usr / local / mongodb / data / logs / mongodb. Log # this file needs to be specified
   fork=true
   Logappend = true # log append
   port=27017
   auth=true
   bind_ IP = 0.0.0.0 # remote connection. Specify the IP, or you cannot connect. 0.0.0.0 does not restrict IP access and opens the corresponding port

If:

warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
It can be solved in the following two ways:
1: If the database fails to connect as above, it may be the mongod.lock file in the data directory. You can use the following command to repair it:
./bin/mongod --repair
2: Or delete mongod.lock directly
rm -f /usr/local/mongodb/data/db/mongod.lock  
Then start the mongodb service:
./mongod --config mongodb.conf
If the above two parts still cannot be solved, it is the path file. We can delete the / usr / local / mongodb / mongodb3.2.4/bin/data directory and its subdirectories and adopt the absolute path method:
./mongod /usr/local/mongodb/mongodb3.2.4/bin/mongod --dbpath=/usr/local/mongodb/mongodb3.2.4/bin/data/test/db --logpath=/usr/local/mongodb/mongodb3.2.4/bin/data/test/logs/mongodb.log --fork

Parameter interpretation: 
--Dbpath database path (data file)
--Logpath log file path
--Master specifies the master machine
--Slave is specified as the slave machine
--Source specifies the IP address of the master machine
--Pologsize specifies that the log file size should not exceed 64M. Because resync is very expensive and time-consuming, it is best to set a large enough oplogsize to avoid resync (the default oplog size is 5% of the free disk size).
--Logappend is added at the end of the log file, that is, the log is written by appending
--Journal enable logging
--Port enable port number
--Fork runs in the background
--Only specifies which database to copy only
--Slavedelay is the time interval detected from replication
--Does auth need to verify login permissions (username and password)
--Syncdelay data writing time (seconds) to the hard disk. 0 is not waiting and is written directly
--Notablescan does not allow table scanning
--Maxconns the maximum number of concurrent connections. The default is 2000  
--Pidfilepath specifies the process file. If it is not specified, no process file will be generated
--bind_ IP is bound to IP. After binding, only the bound IP can access the service

Power on

Centos7 boot configuration

vim /lib/systemd/system/mongodb.service

The contents are as follows

 [Unit]
     Description=mongodb
     After=network.target remote-fs.target nss-lookup.target
 [Service]
     Type=forking
     ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf
     ExecReload=/bin/kill -s HUP $MAINPID
     ExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /usr/local/mongodb/conf/mongodb.conf
     PrivateTmp=true
  [Install]
    WantedBy=multi-user.target

Systemctl operation parameters

#Start
systemctl start mongodb.service
#View status
systemctl status mongodb.service
#Power on
systemctl enable mongodb.service
#If mongodb.service is incorrectly configured, remember to reload the file after the change
systemctl daemon-reload