MongoDB(一)安裝和相關配置

(一)、簡介
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*')
相關文章
相關標籤/搜索