1. 程式人生 > >linux系統安裝MongoDB

linux系統安裝MongoDB

tails specified 成功 sta 安裝mongodb 分享圖片 系統 端口 ont

1.在官網https://www.mongodb.org/dl/linux/x86_64下載對應的安裝壓縮包,我用的是3.2.5,建議使用此版本,版本越高,可能會出現其它未知問題,比如需要glibc高版本,通過

ll /lib64/libc.so.6能查看本機版本為/lib64/libc.so.6 -> libc-2.17.so,安裝一次過。

2.把壓縮包放在/usr/download目錄下,進入download目錄,解壓,tar -zxvf mongodb-linux-x86_64-3.2.5.tgz。

3.把文件夾復制到/usr/local/mongodb目錄下,cp -r mongodb-linux-x86_64-3.2.5 /usr/local/mongodb。

4.編輯環境變量信息,sudo vi /etc/profile,在裏面加上下面的內容,然後source /etc/profile。

export MONGODB_HOME=/usr/local/mongodb/mongodb-linux-x86_64-3.2.5
export PATH=$PATH:$MONGODB_HOME/bin

5.進入/usr/local/mongodb/mongodb-linux-x86_64-3.2.5,按順序執行如下命令。

sudo mkdir -p data/db

sudo chmod -r 777 data/db

sudo mkdir logs

cd logs

touch mongodb.log

6.進入/usr/local/mongodb/mongodb-linux-x86_64-3.2.5/bin,執行sudo vi mongodb.conf,裏面添加如下內容。

dbpath = /usr/local/mongodb/mongodb-linux-x86_64-3.2.5/data/db #數據文件存放目錄
logpath = /usr/local/mongodb/mongodb-linux-x86_64-3.2.5/logs/mongodb.log #日誌文件存放目錄
port = 27017  #端口
fork = true  #以守護程序的方式啟用,即在後臺運行
nohttpinterface = true

7.在bin目錄下執行./mongod -f mongodb.conf以配置文件的方式啟動MongoDB數據庫服務。

8.執行./mongo,可以連接MongoDB數據庫。

MongoDB shell version: 3.2.5
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] ** WARNING: Insecure configuration, access control is not enabled and no --bind_ip has been specified.
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted, 
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] **          and the server listens on all available network interfaces.
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always‘.
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] **        We suggest setting it to ‘never‘
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always‘.
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] **        We suggest setting it to ‘never‘
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] 
>

9.設置mongodb.service啟動服務,設置開機啟動,按順序執行如下命令。

cd /lib/systemd/system

sudo vi mongodb.service

裏面的內容添加如下內容,然後保存。

[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/mongodb/mongodb-linux-x86_64-3.2.5/bin/mongod --config /usr/local/mongodb/mongodb-linux-x86_64-3.2.5/bin/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/mongodb-linux-x86_64-3.2.5/bin/mongod --shutdown --config /usr/local/mongodb/mongodb-linux-x86_64-3.2.5/bin/mongodb.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

10.授權chmod 754 mongodb.service。

11.系統mongodb.service的操作命令如下。

#啟動服務  
systemctl start mongodb.service  
#關閉服務  
systemctl stop mongodb.service  
#開機啟動  
systemctl enable mongodb.service

12.你會發現執行systemctl start mongodb.service時會有如下內容,沒關系,不影響,沒有更好。

Job for mongodb.service failed because the control process exited with error code. See "systemctl status mongodb.service" and "journalctl -xe" for details.

13.執行cd /usr/local/mongodb/mongodb-linux-x86_64-3.2.5/bin/到bin目錄下,執行./mongo,出現如下畫面,表示MongoDB數據庫啟動成功,可以去連接做任何操作了。

MongoDB shell version: 3.2.5
connecting to: test
Server has startup warnings: 
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] ** WARNING: Insecure configuration, access control is not enabled and no --bind_ip has been specified.
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted, 
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] **          and the server listens on all available network interfaces.
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-08-21T15:16:33.561+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always‘.
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] **        We suggest setting it to ‘never‘
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] 
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always‘.
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] **        We suggest setting it to ‘never‘
2018-08-21T15:16:33.562+0800 I CONTROL  [initandlisten] 
>

14.下載NoSQL Manager for MongoDB,輸入IP和端口(默認是27017),就可以連接進去,做任何你之前在oracle或者mysql的任何操作了,但是,語法不一樣。

技術分享圖片

linux系統安裝MongoDB