mongodb installation and use



1. mongodb installation

Install C compiler

yum -y install gcc*

Upload the mongodb installation package Upload mongodb
to the /home directory to

Insert picture description here

create a mongodb database directory

mkdir -p /data/db
mkdir -p /var/mongodb
touch /var/mongodb/mongodb.log


Unzip the installation package

tar xf mongodb-linux-x86_64-rhel70-3.4.2.tgz


Folder rename

mv mongodb-linux-x86_64-rhel70-3.4.2 /home/mongodb


Set power-on auto-start

运行命令行:vim /etc/rc.d/rc.local

在rc.local文件最后面加入下面命令
/home/mongodb/bin/mongod --auth --fork --dbpath=/data/db/ --logpath=/var/mongodb/mongodb.log

保存退出后给此文件增加执行权限
运行命令行:chmod +x /etc/rc.d/rc.local



2. Mongodb configuration

2.1 Modify the mongodb version number

2.1.1 Log in to mongodb in non-authenticated mode

/home/mongodb/bin/mongod  (保存窗口)

Insert picture description here

/home/mongodb/bin/mongo

Insert picture description here

2.1.2 Log in to the administrator database

use admin

Insert picture description here

2.1.3 Create an administrator account

db.createUser({
 user: "dba",
 pwd: "dba",
 roles: 
 [ 
 { 
 role: "readWriteAnyDatabase", db: "admin" 
 } 
 ]})

Insert picture description here

2.1.4 Change the version number to 3

var schema = db.system.version.findOne({"_id":"authSchema"})
schema.currentVersion=3
db.system.version.save(schema)

2.1.5 View the revised version

db.system.version.find()

Insert picture description here


2.2 Create a database

use lyl

Insert picture description here

2.2.1 Insert data

db.usr.insert({'name':'bkty','id':2})

Insert picture description here

Note: A piece of data must be inserted here to keep the database, otherwise the creation will fail


2.3 Create an authenticated user

2.3.1 Login to the database

use lyl

Insert picture description here

2.3.2 Create Ordinary User

db.createUser({
 user: "sa",
 pwd: "sa",
 roles: 
 [ 
 { 
 role: "readWrite", db: "lyl" 
 } 
 ]})

Insert picture description here

2.3.3 Create a role under non-verified login

use admin

db.createRole({role:'sysadmin',roles:[],
privileges:[
{resource:{anyResource:true},actions:['anyAction']}
]})

Insert picture description here

2.4 Create an administrator user and grant the above role permissions

db.createUser({
user:'admin',
pwd:'admin',
roles:[
{role:'sysadmin',db:'admin'}
]})

Insert picture description here


2.5 Designated user authentication login

/home/mongodb/bin/mongod --auth --fork --dbpath=/data/db/ --logpath=/var/mongodb/mongodb.log 
/home/mongodb/bin/mongo

Insert picture description here

2.5.1 Login to the database

use lyl

Insert picture description here

2.5.2 Designated user authentication login

 db.auth('sa','sa')

Insert picture description here


3. Backup

Create automatic backup and delete scripts

vim /root/mongodbbackup.sh
在文件中增加如下内容
#!/bin/bash  
mkdir -p /home/mongodb_all/mongodb_bak_now /home/mongodb_all/mongodb_bak_list 
DUMP=/home/mongodb/bin/mongodump   
OUT_DIR=/home/mongodb_all/mongodb_bak_now  
TAR_DIR=/home/mongodb_all/mongodb_bak_list   
DATE=`date +%Y%m%d%H%M%S`  
DB_USER=admin
DB_PASS=admin
IP=192.168.168.12:27017
TAR_BAK="mongodb_bak_$DATE.tar.gz"  
cd $OUT_DIR  
rm -rf $OUT_DIR/*  
mkdir -p $OUT_DIR/$DATE  
$DUMP -h $IP -u $DB_USER -p $DB_PASS --authenticationDatabase "admin" -o $OUT_DIR/$DATE  
tar -zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE  
rm -rf $TAR_DIR/mongodb_bak_`date -d '-3 days' +%Y%m%d`*.tar.gz
exit

Note: The IP address of IP=192.168.168.12:27017 in the script is the IP address and port of the server where you installed mongodb. Remember to modify it, remember! ! !

4 Turn on the firewall

To check whether the firewall is turned on, run the following command line to
run the command line: systemctl status firewall.service
If it shows active, it means the firewall is turned on, you need to check if port 27017 of mongodb is open, run the following command line to
run the command line: firewall-cmd --list-all
If port 27017 is not in the list, you need to open the port, run the following command line
Run command line 1: firewall-cmd --permanent --add-port=27017/tcp
Run command line 2: firewall- cmd- -reload

Guess you like

Origin blog.csdn.net/zyy130988/article/details/110131087