• Environment: centos 7.
  • Version: 4.4.1
  1. Download:
Liverpoolfc.tv: HTTPS://www.mongodb.com/try/download/communityDownload the corresponding version and upload it to the server or download it in WGET mode.Copy the code
Eg: the wget HTTPS:/ / fastdl.mongodb.org/linux/mongodb-linux-x86_70-4.4.1.tgz
Copy the code
  1. Extract:
/ /
tar -zxvf mongodb-linux-x86_64-rhel70-4.41..tgz
/ / renamed
mv mongodb-linux-x86_64-rhel70-4.41. /usr/local/mongodb
Copy the code
  1. Configuring environment Variables
// Open the environment variable configuration fileVi /etc/profile Add the following two statements to the last line:export MONGODB_HOME=/usr/local/mongodb 
exportPATH=$PATH:$MONGODB_HOME/bin Save the configuration and exit.// Restart the system configuration file to make the configuration take effect.
source /etc/profile 
Copy the code
  1. Add log and storage data space files and provide permissions.
cd /usr/local/mongodb
// Create a data folder.
mkdir -p data/db
// Grant read and write permissions
chmod -r 777 data/db 
// Create a log file
mkdir logs && cd logs && touch mongodb.log 
Copy the code
  1. Add a configuration file.
// Create and add a configuration file
/usr/local/mongodb && mkdir conf && cd conf && touch mongodb.conf
// Write the mongodb configuration
vi mongodb.conf
// Write the following configuration information
dbpath = /usr/Local /mongodb/data/db/usr/Local/mongo/logs/mongo. Log port = # log file storage directory27017# port fork =true# daemon enabled by running nohttpInterface = in the backgroundtrue# Open the web interface bind_ip =0.0. 0. 0# default is127.0. 01.If yes, only local access is allowed. Remote connection can be configured here. #auth =true# indicates whether to enable account password authentication.Copy the code
  1. Start or stop the service.
// Start the service
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf
// Stop the service
/usr/local/mongodb/bin/mongod --shutdown
Copy the code
  1. Centos 7.x can register service mode and set it to start automatically upon startup.

(1) Registration service

// Register service
/etc/systemd/system && touch mongodb.service
// Write service information.
vi mmongodb.service
// Write information
[Unit]  
Description=mongodb  
After=network.target remote-fs.target nss-lookup.target  
[Service]  
Type=forking  
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongodb.conf  
ExecReload=/bin/kill -s HUP $MAINPID  
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/conf/mongodb.conf  
PrivateTmp=true  
  
[Install]  
WantedBy=multi-user.target
Copy the code

(2) Application services

systemctl damon-reload
Copy the code

(3) Set the boot automatically

systemctl enable mongodb.service 
Copy the code

(4) Use commands

// Start the service
service mongodb start
// Stop the service
service mongodb stop
// Restart the service
service mongodb reload
// Check the status
service mongodb status
Copy the code
  1. Connect to the mongodb database.
./mongo 
Copy the code
  1. Set user
// Connect to the database
use admin;
// Create an account
db.createUser({user:"admin".pwd:"password".roles: ["root"]})
Copy the code
  1. Open the Auth configuration in the configuration file. After the service is restarted.
// Reconnect to validate entry
db.auth("admin"."password")
Copy the code