MongoDB 3.0.6 Start script, user management, import and export Libraries

Preface: Installation steps, please check This blog(https://blog.csdn.net/tingfengqianqu/article/details/101381150)

1. Import database

1) Modify configuration files
Before importing data, you need to annotate the password authentication of mongodb.conf, and you cannot execute the mongodb service before modifying the configuration file.

2) Enter the bin directory for execution
Restart mongodb to execute import statements

./mongorestore --db DBNAME  --dir DBPATH

3) Setting passwords for newly created databases
Stop mongodb and cancel the password comment. Enter the bin directory

 #Start the client
 ./mongo
 #Select amdin
 use admin
 #Authentication authority
 db.("root","XXX")
 #Switch to the newly created database
 use DBNAME
 #Create user
 db.createUser({user:"USER",pwd:"PASSWORD",roles:["readWrite"]})
 #To grant authorization
 db.auth("USER", "PASSWORD")
 #Enter admin
 use admin
 #View users
 db.system.users.find()
 

Commands that may be used

#Delete Library
db.dropDatabase()
#delete user
db.dropUser('DBNAME')

2. Export database

To be updated

3. Start script

Note: The script is difficult to operate. Please execute it carefully.

(1) Enter / etc/init.d / directory

cd /etc/init.d/

(2) Create mongodb files

vim mongodb

(3) Add the following code

#!/bin/sh 
chkconfig:   2345 90 10
description:  Redis is a persistent key-value database
  
PROGRAM=/usr/local/mongodb/bin/mongod
MONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'`
test -x $PROGRAM || exit 0
case "$1" in
  start)
     ulimit -n 3000
     echo "Starting MongoDB server"
     $PROGRAM --fork --quiet -f /usr/local/mongodb/mongodb.conf
     echo "Started MongoDB server"
     ;;
  stop)
     echo "Stopping MongoDB server"
     if [ ! -z "$MONGOPID" ]; then
        kill -15 $MONGOPID
     fi
     echo "Stopped MongoDB server"
     ;;
  status)
     ;;
  *)
     echo "Usage: mongodb {start|stop|status}"
     exit 1
esac

exit 0

(4) After saving, you can execute the following commands

 chmod 777 /etc/init.d/mongodb
 chkconfig --add mongodb
 chkconfig mongodb on

(5) Start mongodb with services

service mongodb start

Keywords: MongoDB Database vim Redis

Added by Jeyush on Mon, 30 Sep 2019 13:11:08 +0300