当前位置:Gxlcms > 数据库问题 > MongoDB 基础

MongoDB 基础

时间:2021-07-01 10:21:17 帮助过:16人阅读

一.安装MongoDB

(1)redhat或centos6.2以上系统
(2)系统开发包完整
(3)ip地址和hosts文件解析正常
(4)iptables防火墙&SElinux关闭
(5)关闭大页内存机制
########################################################################

#关闭透明大页   root用户下
cat <<EOF >/etc/rc.local

#add for MongoDB
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
  echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
   echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
EOF

cat  /sys/kernel/mm/transparent_hugepage/enabled        
cat /sys/kernel/mm/transparent_hugepage/defrag 

#创建用户
useradd mongod
passwd mongod

#创建目录
mkdir -p /mongodb/conf
mkdir -p /mongodb/log
mkdir -p /mongodb/data
mkdir -p /mongodb/bin

#安装
cd /install
tar -zxvf mongodb-linux-x86_64-rhel70-3.6.20.tgz
cp -r /install/mongodb-linux-x86_64-rhel70-3.6.20/bin /mongodb/bin
chown -R mongod:mongod /mongodb

su - mongod
cat <<‘EOF‘>> .bash_profile

#add for mongodb
export PATH=/mongodb/bin:$PATH
EOF

source .bash_profile

#启动
mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --fork


#配置文件
#YAML例子
cat > /mongodb/conf/mongo.conf <<EOF
systemLog:
   destination: file
   path: "/mongodb/log/mongodb.log"
   logAppend: true
storage:
   journal:
      enabled: true
   dbPath: "/mongodb/data/"
processManagement:
   fork: true
net:
   port: 27017
   bindIp: 0.0.0.0
EOF

#用配置文件启动MongoDB
mongod -f /mongodb/conf/mongo.conf --shutdown
mongod -f /mongodb/conf/mongo.conf 


#配置systemd
cat > /etc/systemd/system/mongod.service <<EOF
[Unit]
Description=mongodb 
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongod
Type=forking
ExecStart=/mongodb/bin/mongod --config /mongodb/conf/mongo.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/mongodb/bin/mongod --config /mongodb/conf/mongo.conf --shutdown
PrivateTmp=true  
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl restart mongod
systemctl stop mongod
systemctl start mongod
systemctl status mongod

二、基本操作

user mydb
show dbs
show databases
db.c1.insert({"name":"zhangsan","age":"16"})
show tables
db.c1.find()
for(i=0;i<10000;i++){db.log.insert({ "uuid":i,"name":"mongodb","age":6,"date":new Date()})}
db.log.find({uuid:100})
db.log.find({uuid:100}).pretty()

DBQuery.shellBatchSize=50
db.log.findOne()
db.log.remove({})
db.log.distinct("age")
db.log.stats()
db.log.datasize()
db.log.totalIndexSize()
db.log.storageSize()

#创建用户     (在database中创建)
#1.创建管理员用户
use admin
db.createUser(
{
    user : "root",
    pwd : "root123",
    roles : [ { role : "root", db : "admin" } ]
}
)

#2.创建APP用户
use appdb01
db.createUser(
{
    user : "app01",
    pwd : "app01pwd",
    roles : [ { role : "readWrite", db : "appdb01" } ]
}
)

#3.管理多个库
use appdb01
db.createUser(
{
    user : "app01",
    pwd : "app01pwd",
    roles : [ { role : "readWrite", db : "appdb01" } ],
            [ { role : "read", db : "appdb02" } ],
}
)


#验证
db.auth(‘root‘,‘root123‘)


#删除用户
use app01
db.dropUser("root1")
#开启验证功能 /mongodb/conf/mongo.conf
security:
  authorization: enabled

#查看用户信息
db.system.users.find().pretty()

#远程连接
mongo -uroot -p123456 192.168.10.161/admin    # ip/admin   登录后使用admin库
mongo -uapp01 -papp01pwd 192.168.10.161/appdb01

MongoDB 基础

标签:RoCE   启动   host   linu   users   i++   hup   stat   main   

人气教程排行