学习笔记
适用于Linux系统安装
By lumia98@vip.qq.com
1、安装MongoDB
[ root@test ~] $ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-4.0.21-rc1.tgz
[ root@test ~] $ tar xf mongodb-linux-x86_64-rhel80-4.0.21-rc1.tgz
[ root@test ~] $ mkdir -p /opt/app/mongodb-4021/{ log,data,conf}
[ root@test ~] $ cp -r /opt/soft/mongodb-linux-x86_64-rhel80-4.0.21-rc1/bin /opt/app/mongodb-4021/
[ root@test ~] $ useradd mongodb
[ root@test ~] $ chown -R mongodb /opt/app/mongodb-4021
[ root@test ~] $ su - mongodb
[ mongodb@test ~] $ echo "export PATH=/opt/app/mongodb-4021/bin:$PATH " >> .bash_profile
[ mongodb@test ~] $ source .bash_profile
2、YAML配置文件,注意格式缩进,可以用sublime编写
cat> /opt/app/mongodb- 4021/conf/mongodb.conf<<EOF
systemLog :
destination : file
path : "/opt/app/mongodb-4021/log/mongodb.log"
logAppend : true
storage :
journal :
enabled : true
dbPath : "/opt/app/mongodb-4021/data"
processManagement :
fork : true
pidFilePath : "/opt/app/mongodb-4021/log/mongodb.pid"
net :
port : 27017
bindIp : 127.0.0.1, 172.21.31.2
EOF
3、启动关闭MongoDB
cat> /etc/systemd/system/mongodb.service<< EOF
[Unit]
Description=Mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongodb
Type=forking
PIDFile=/opt/app/mongodb-4021/log/mongodb.pid
ExecStart=/opt/app/mongodb-4021/bin/mongod --config /opt/app/mongodb-4021/conf/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
#ExecStop=/bin/kill -s QUIT $MAINPID
ExecStop=/opt/app/mongodb-4021/bin/mongod --config /opt/app/mongodb-4021/conf/mongodb.conf --shutdown
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
[ mongodb@test ~] $ mongod -f /opt/app/mongodb-4021/conf/mongodb.conf
[ mongodb@test ~] $ mongod -f /opt/app/mongodb-4021/conf/mongodb.conf --shutdown
[ mongodb@test ~] $ mongod --dbpath= /opt/app/mongodb-4021/data --logpath= /opt/app/mongodb-4021/log/mongodb.log --port= 27017 --logappend --fork
4、登录MongoDB系统
[ mongodb@test ~] $ mongo
> show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
> use admin;
switched to db admin
> show tables;
system.version
5、创建库及表
> use test ;
switched to db test
> db.testdb.insert( { name:"测试数据" } ) ;
WriteResult( { "nInserted" : 1 } )
> show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
> show tables;
testdb
6、show命令
> show dbs;
> show databases;
> show tables;
> show collections;
7、查找数据
> db.testdb.find( ) ;
8、db.help()
> db.help( ) ;
9、插入数据
db.testdb.insert( { name:'测试' , age:'19' , sex:'男' } ) ;
插入数据是json格式的( ) 包围
10、查询数据
> db.testdb.find( { id:'101' } ) ;
> db.testdb.find( { id:'101' } ) .pretty( ) ;