1. 程式人生 > >在CentOS6.5中安裝MongoDB

在CentOS6.5中安裝MongoDB

最近因CasServer需要將Service Registry和Ticket Registry儲存在MongoDB,於是就需要在測試環境中安裝MongoDB,本次安裝的是官網最新版的MongoDB。

1. 下載MongoDB的二進位制包

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.9.tgz

2. 安裝MongoDB

1) 提前建立目錄和檔案

mkdir -p /opt/mongodb/data/db
mkdir -p /opt/mongodb/data/logs
touch /opt/mongodb/data/logs/mongodb.log

2) 解壓

tar -zxvf mongodb-linux-x86_64-rhel62-3.4.9.tgz -C /opt/mongodb/

3) 在/opt/mongodb/data/中新建配置檔案(名稱隨意)

vim /opt/mongodb/data/mongo.conf

填入如下內容

#埠號
port = 27017
#資料目錄
dbpath = /opt/mongodb/data/db
#日誌目錄
logpath = /opt/mongodb/data/logs/mongodb.log
#設定後臺執行
fork = true
#日誌輸出方式
logappend = true
#開啟認證
#auth = true

3. 啟動MongoDB

在/opt/mongodb目錄中執行如下命令

./bin/mongod --config /opt/mongodb/data/mongodb.conf

啟動完成後進入mongo

bin/mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9

新增使用者

> show dbs
admin         0.000GB
cas           0.000GB
cas-database  0.000GB
local         0.000GB
> use admin
switched to db admin
> db.createUser({ user: "admin", pwd: "111111", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
> db.system.users.find() 
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "vmdMmYIF0bAZ8m8yPqLQJQ==", "storedKey" : "1kC+35ZqftcxiVX+mgLrIkU73oQ=", "serverKey" : "af+s46hVg9kZYnzJb8zg46PFVO8=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
{ "_id" : "admin.cas-user", "user" : "cas-user", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "+90vvMyFNyP/vIfHJ+DQRg==", "storedKey" : "kLEbwHKoVaffSb5PQjRUbeT/sAI=", "serverKey" : "FIHpVUEnxdvMIK2Ako9yvE+g2BE=" } }, "roles" : [ { "role" : "readWrite", "db" : "cas-database" } ] }
{ "_id" : "cas.cas-user", "user" : "cas-user", "db" : "cas", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "SxxINflDqgqDru0CwG9Dug==", "storedKey" : "0x4kVmSUoVau5QdxIIHQrIhAFeQ=", "serverKey" : "8AKvCK0GY0Jke9sZXcxpv7691xU=" } }, "roles" : [ { "role" : "readWrite", "db" : "cas-database" } ] }
{ "_id" : "cas-database.cas-user", "user" : "cas-user", "db" : "cas-database", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "WT2FauMfxk1GxBeI91fSIw==", "storedKey" : "NNHWX00hqF+4v9j43Nk1ch/qduc=", "serverKey" : "2Qm4ry5GEPK2sgpK7HjbKibvZ4M=" } }, "roles" : [ { "role" : "readWrite", "db" : "cas-database" } ] }

建立完使用者將配置檔案的最後一行配置的註釋去掉,重啟mongodb,那麼此時登入mongodb就需要通過賬戶密碼登入了!

> db.shutdownServer();