(一)、简介
MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构很是松散,是相似json的bson格式,所以能够存储比较复杂的数据类型。Mongo最大的特色是它支持的查询语言很是强大,其语法有点相似于面向对象的查询语言,几乎能够实现相似关系数据库单表查询的绝大部分功能,并且还支持对数据创建索引。mysql
(二)、安装
一、下载,地址:https://www.mongodb.com/download-center/community 。目前使用最新版本4.0.9linux
[root@otrs004097 opt]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.9.tgz --2019-05-29 09:59:20-- https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.9.tgz Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 54.192.151.54, 54.192.151.117, 54.192.151.31, ... Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|54.192.151.54|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 84969698 (81M) [application/x-gzip] Saving to: ‘mongodb-linux-x86_64-4.0.9.tgz’ 100%[===================================================================================================================================================>] 84,969,698 11.6MB/s in 7.2s 2019-05-29 09:59:29 (11.2 MB/s) - ‘mongodb-linux-x86_64-4.0.9.tgz’ saved [84969698/84969698]
二、解压和移动目录sql
[root@otrs004097 tmp]# tar -xf mongodb-linux-x86_64-4.0.9.tgz [root@otrs004097 tmp]# mv mongodb-linux-x86_64-4.0.9 /usr/local/mongodb [root@otrs004097 tmp]# cd /usr/local/mongodb/ [root@otrs004097 mongodb]# ll total 116 drwxr-xr-x 2 root root 231 May 29 10:00 bin -rw-r--r-- 1 root root 30608 Apr 11 09:59 LICENSE-Community.txt -rw-r--r-- 1 root root 16726 Apr 11 09:59 MPL-2 -rw-r--r-- 1 root root 2601 Apr 11 09:59 README -rw-r--r-- 1 root root 60005 Apr 11 09:59 THIRD-PARTY-NOTICES
三、配置环境变量并生效mongodb
[root@otrs004097 ~]#vim /etc/profile PATH=/usr/local/mongodb/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin:/usr/local/mysql/bin [root@otrs004097 ~]#source /etc/profile
四、建立相关的数据库目录数据库
[root@otrs004097 ~]# mkdir -p /data/mongodb/{db,log} [root@otrs004097 ~]# mkdir /usr/local/mongodb/conf
五、建立配置文件json
[root@otrs004097 mongodb]# cat /usr/local/mongodb/conf/mongodb.conf port=27017 dbpath= /data/mongodb/db logpath= /data/mongodb/log/mongodb.log logappend=true fork=true maxConns=100 auth=true journal=true storageEngine=wiredTiger bind_ip = 0.0.0.0
备注:以上配置解释vim
port=27017 #端口 dbpath= /data/mongodb/db #数据库存文件存放目录 logpath= /data/mongodb/log/mongodb.log #日志文件存放路径 logappend=true #使用追加的方式写日志 fork=true #以守护进程的方式运行,建立服务器进程 maxConns=100 #最大同时链接数 noauth=true #不启用验证 ,若是启用验证的话,改为false或直接用#注释掉 journal=true #每次写入会记录一条操做日志(经过journal能够从新构造出写入的数据)。 #即便宕机,启动时wiredtiger会先将数据恢复到最近一次的checkpoint点,而后重放后续的journal日志来恢复。 storageEngine=wiredTiger #存储引擎有mmapv一、wiretiger、mongorocks bind_ip = 0.0.0.0 #这样就可外部访问了,例如从win10中去连虚拟机中的MongoDB nohttpinterface=true默认是27017,再加100,会开启一个网络端口,不让其开启,为了更加的安全
六、启动和中止安全
[root@otrs004097 mongodb]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 56710 child process started successfully, parent exiting [root@otrs004097 mongodb]# netstat -lntp|grep 27017 tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 56710/mongod [root@otrs004097 mongodb]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf --shutdown killing process with pid: 56674 [root@otrs004097 mongodb]# netstat -lntp|grep 27017
七、建立启动服务服务器
[root@otrs004097 system]# cat /lib/systemd/system/mongod.service [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 --config /usr/local/mongodb/conf/mongodb.conf --shutdown PrivateTmp=true #####以普通用户启动添加下边配置 User=mongo Group=mongo [Install] WantedBy=multi-user.target
[root@otrs004097 ~]# chown -R mongo:mongo /usr/local/mongodb/ [root@otrs004097 ~]# chown -R mongo:mongo /data/mongodb/ [root@otrs004097 system]#chmod 754 mongod.service [root@otrs004097 system]#systemctl enable mongod [root@otrs004097 system]#systemctl start mongod [root@otrs004097 system]#systemctl stop mongod [root@otrs004097 ~]# systemctl status mongod ● mongod.service - mongodb Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2019-05-31 15:55:22 CST; 10min ago Process: 128209 ExecStop=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongodb.conf --shutdown (code=exited, status=0/SUCCESS) Process: 128215 ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongodb.conf (code=exited, status=0/SUCCESS) Main PID: 128217 (mongod) CGroup: /system.slice/mongod.service └─128217 /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongodb.conf May 31 15:55:20 otrs004097 systemd[1]: Starting mongodb... May 31 15:55:20 otrs004097 mongod[128215]: about to fork child process, waiting until server is ready for connections. May 31 15:55:20 otrs004097 mongod[128215]: forked process: 128217 May 31 15:55:22 otrs004097 mongod[128215]: child process started successfully, parent exiting May 31 15:55:22 otrs004097 systemd[1]: Started mongodb. [root@otrs004097 ~]# ps -ef|grep mongod mongo 128217 1 0 15:55 ? 00:00:05 /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongodb.conf root 129152 125585 0 16:06 pts/1 00:00:00 grep --color=auto mongod 备注:原先指定配置文件是经过--config来指定的,新版本能够直接经过-f或--config均可以来指定配置文件进行启动。
七、受权登录。因为安全性的需求,咱们须要为mongodb数据库建立登录的用户名和密码,这就须要修改mongodb配置文件了。把noauth注释掉,添加auth=true。总的配置以下,而后启动mongodb网络
port=27017 dbpath= /data/mongodb/db logpath= /data/mongodb/log/mongodb.log logappend=true fork=true maxConns=100 auth=true journal=true storageEngine=wiredTiger bind_ip = 0.0.0.0
use admin
switched to db admin
db.createUser({user:"admin",pwd:"123456789",roles:["root"]})
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
db.auth("admin","123456789")
1
use yz
switched to db yz
db.createUser({user:"yz",pwd:"123456",roles:[{role:"dbOwner",db:"yz"}]})
Successfully added user: {
"user" : "yz",
"roles" : [
{
"role" : "dbOwner",
"db" : "yz"
}
]
}
建立用户管理员: use admin db.createUser({user:"root",pwd:"root123456",roles:["userAdminAnyDatabase"]}) db.auth('root','root123456') 以用户管理员身份登陆,并切换数据库,建立数据库用户: 切换到test数据库 use test 建立用户名、密码、角色 db.createUser({user:"username",pwd:"@user123456*",roles:[{role:"readWrite",db:"securitydata"}]}) 设置mongodb配置中的auth为true(/etc/mongod.conf): security: authorization: enabled 验证mongodb数据库权限。 db.auth('user','@user123456*')