1. 程式人生 > 資料庫 >Centos7原始碼安裝MongoDB 4.0.21

Centos7原始碼安裝MongoDB 4.0.21

MongoDB的下載

進入官網 https://www.mongodb.com/ 如圖點選,進入下載頁面
在這裡插入圖片描述
或者直接點選這個連線,進入下載頁面:https://www.mongodb.com/try/download/community
在這裡插入圖片描述

解壓至/usr/local

root@localhost:~# tar -zxvf mongodb-linux-x86_64-rhel70-4.0.21.tgz 
mongodb-linux-x86_64-rhel70-4.0.21/THIRD-PARTY-NOTICES.gotools
mongodb-linux-x86_64-rhel70-4.0.21/README
mongodb-linux-x86_64-rhel70-4.0.21/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-4.0.21/MPL-2
mongodb-linux-x86_64-rhel70-4.0.21/LICENSE-Community.txt
mongodb-linux-x86_64-rhel70-4.0.21/bin/mongodump
mongodb-linux-x86_64-rhel70-4.0.21/bin/mongorestore
mongodb-linux-x86_64-rhel70-4.0.21/bin/mongoexport
mongodb-linux-x86_64-rhel70-4.0.21/bin/mongoimport
mongodb-linux-x86_64-rhel70-4.0.21/bin/mongostat
mongodb-linux-x86_64-rhel70-4.0.21/bin/mongotop
mongodb-linux-x86_64-rhel70-4.0.21/bin/bsondump
mongodb-linux-x86_64-rhel70-4.0.21/bin/mongofiles
mongodb-linux-x86_64-rhel70-4.0.21/bin/mongoreplay
mongodb-linux-x86_64-rhel70-4.0.21/bin/mongod
mongodb-linux-x86_64-rhel70-4.0.21/bin/mongos
mongodb-linux-x86_64-rhel70-4.0.21/bin/mongo
mongodb-linux-x86_64-rhel70-4.0.21/bin/install_compass
root@localhost:~# mv mongodb-linux-x86_64-rhel70-4.0.21 /usr/local/mongodb

建立資料庫資料夾以及日誌資料夾

[root@localhost mongodb]# pwd
/usr/local/mongodb
[root@localhost mongodb]# mkdir -p data
[root@localhost mongodb]# mkdir -p log
[root@localhost mongodb]# touch log/mongodb.log
[root@localhost mongodb]# 

建立資料庫配置檔案

在 /usr/local/mongodb/bin 路徑下建立資料庫配置檔案mongodb.conf,並新增以下內容

# 資料庫路徑
dbpath=/usr/local/mongodb/data
# 日誌檔案路徑
logpath=/usr/local/mongodb/log/mongodb.log
# 表示日誌檔案末尾追加日誌
logappend=true
# 啟用埠號
port=27017
# 是否在後臺執行
fork=true

檢視版本

root@localhost:/usr/local/mongodb/bin# mongod -v
2021-01-03T13:04:23.039+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2021-01-03T13:04:23.047+0800 D NETWORK  [main] fd limit hard:4096 soft:1024 max conn: 819
2021-01-03T13:04:23.048+0800 I CONTROL  [initandlisten] MongoDB starting : pid=1330 port=27017 dbpath=/data/db 64-bit host=localhost.localdomain
2021-01-03T13:04:23.048+0800 I CONTROL  [initandlisten] db version v4.0.21
2021-01-03T13:04:23.048+0800 I CONTROL  [initandlisten] git version: 3f68a848c68e993769589dc18e657728921d8367
2021-01-03T13:04:23.048+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2021-01-03T13:04:23.048+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2021-01-03T13:04:23.048+0800 I CONTROL  [initandlisten] modules: none
2021-01-03T13:04:23.048+0800 I CONTROL  [initandlisten] build environment:
2021-01-03T13:04:23.048+0800 I CONTROL  [initandlisten]     distmod: rhel70
2021-01-03T13:04:23.048+0800 I CONTROL  [initandlisten]     distarch: x86_64
2021-01-03T13:04:23.048+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2021-01-03T13:04:23.048+0800 I CONTROL  [initandlisten] options: { systemLog: { verbosity: 1 } }
2021-01-03T13:04:23.048+0800 D NETWORK  [initandlisten] fd limit hard:4096 soft:1024 max conn: 819
2021-01-03T13:04:23.049+0800 E STORAGE  [initandlisten] Failed to set up listener: SocketException: Address already in use
2021-01-03T13:04:23.049+0800 I REPL     [initandlisten] Stepping down the ReplicationCoordinator for shutdown, waitTime: 10000ms
2021-01-03T13:04:23.049+0800 D -        [initandlisten] User Assertion: NotMaster: not primary so can't step down src/mongo/db/db.cpp 921
2021-01-03T13:04:23.049+0800 I NETWORK  [initandlisten] Shutting down the global connection pool
2021-01-03T13:04:23.049+0800 I -        [initandlisten] Killing all operations for shutdown
2021-01-03T13:04:23.049+0800 I NETWORK  [initandlisten] Shutting down the ReplicaSetMonitor
2021-01-03T13:04:23.049+0800 I CONTROL  [initandlisten] Shutting down free monitoring
2021-01-03T13:04:23.049+0800 I FTDC     [initandlisten] Shutting down full-time data capture
2021-01-03T13:04:23.049+0800 I STORAGE  [initandlisten] Shutting down the HealthLog
2021-01-03T13:04:23.049+0800 I -        [initandlisten] Dropping the scope cache for shutdown
2021-01-03T13:04:23.049+0800 I CONTROL  [initandlisten] now exiting
2021-01-03T13:04:23.049+0800 I CONTROL  [initandlisten] shutting down with code:48

看到以上資訊證明mongoDB安裝成功

啟動mongoDB

root@localhost:/usr/local/mongodb/bin# mongod -f ./mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1302
child process started successfully, parent exiting

看到以上訊息代表啟動成功

登入mongoDB

./mongo   登入

關閉mongoDB服務

注意:關閉的時候一定要正常關閉,否則下次會無法啟動服務,並且報錯。

這是我未正常關閉的後果,服務無法啟動,報錯如下:

about to fork child process, waiting until server is ready for connections.
forked process: 1268
ERROR: child process failed, exited with error number 1
To see additional information in this output, start without the "--fork" option.

正常關閉步驟:

> use admin
switched to db admin
> db.shutdownServer();
2021-01-03T13:19:10.375+0800 I NETWORK  [js] DBClientConnection failed to receive message from 127.0.0.1:27017 - HostUnreachable: Connection closed by peer
server should be down...
2021-01-03T13:19:10.383+0800 I NETWORK  [js] trying reconnect to 127.0.0.1:27017 failed
2021-01-03T13:19:10.383+0800 I NETWORK  [js] reconnect 127.0.0.1:27017 failed failed 
> exit
bye
2021-01-03T13:19:34.599+0800 I NETWORK  [js] trying reconnect to 127.0.0.1:27017 failed
2021-01-03T13:19:34.599+0800 I NETWORK  [js] reconnect 127.0.0.1:27017 failed failed 
2021-01-03T13:19:34.599+0800 I QUERY    [js] Failed to end session { id: UUID("32d6233c-d568-4e59-9694-6bee174bdd5e") } due to SocketException: socket exception [CONNECT_ERROR] server [couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused]

配置環境變數

root@localhost:~# vim /etc/profile

export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH

root@localhost:~# source /etc/profile