Ubuntu MongoDB LDAP配置

Ubuntu LDAP 配置可以參考另一篇文章 Ubuntu 下LDPA服務(wù)搭建配置

在Ubuntu上安裝MongoDB

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

Ubuntu 12.04
echo "deb [ arch=amd64 ] http://repo.mongodb.com/apt/ubuntu precise/mongodb-enterprise/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-enterprise.list

Ubuntu 14.04
echo "deb [ arch=amd64 ] http://repo.mongodb.com/apt/ubuntu trusty/mongodb-enterprise/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-enterprise.list

Ubuntu 16.04
echo "deb [ arch=amd64,arm64,ppc64el,s390x ] http://repo.mongodb.com/apt/ubuntu xenial/mongodb-enterprise/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-enterprise.list

sudo apt-get update
sudo apt-get install -y mongodb-enterprise
sudo service mongod start

MongoDB 的log文件默認(rèn)在 /var/log/mongodb
MongoDB的data文件默認(rèn)在 /var/lib/mongodb

安裝依賴

sudo apt-get install sasl2-bin

配置mongoDB

修改 /etc/default/saslauthd 文件,改成下面的值:
MECHANISMS="ldap"
START=yes

然后啟動(dòng)saslauthd :

/etc/init.d/saslauthd  start

可以通過(guò)命令 ps wax | grep saslauthd 查看saslauthd 服務(wù)是否啟動(dòng)

更改/etc 下的 saslauthd.conf 文件,如果沒(méi)有這個(gè)文件,就創(chuàng)建一個(gè)。加入下面的內(nèi)容

ldap_servers: ldap://localhost:389
ldap_search_base: ou=people,dc=example,dc=com
ldap_filter: (uid=%u)

好了,我們現(xiàn)在來(lái)測(cè)試下能否通過(guò)用戶名和密碼連接上LDAP

先用錯(cuò)誤的密碼:
testsaslauthd -u user1 -p testpassword -f /var/run/saslauthd/mux
0: NO "authentication failed"

然后試試正確的密碼:
testsaslauthd -u user1 -p user1 -f /var/run/saslauthd/mux
0: OK "Success."
給MongoDB增加一個(gè)LDAP用戶,比如:

精品電影網(wǎng):手機(jī)電影迅雷下載,手機(jī)在線電影,手機(jī)高清電影,手機(jī)迅雷觀看

service mongod start
mongod
用第三方管理工具(Robo)或者mongo終端執(zhí)行下面命令:

db.getSiblingDB("$external").createUser(
    {
      user : "user1",
      roles: [ { role: "read", db: "kundatabase" } ]
    }
)

關(guān)閉mongo服務(wù),ctrl+c ,然后重新已LDAP認(rèn)證方式啟動(dòng):

mongod --auth --setParameter saslauthdPath=/var/run/saslauthd/mux --setParameter authenticationMechanisms=PLAIN

在mongoDB驗(yàn)證LDAP 用戶名和密碼:

db.getSiblingDB("$external").auth(
   {
     mechanism: "PLAIN",
     user: "user1",
     pwd:  "user1",
     digestPassword: false
   }
)

另外,如果想通過(guò)JAVA以LDAP 認(rèn)證的方式連接MongoDB:

       
       MongoCredential credential2 = MongoCredential.createPlainCredential("user1", "$external", "user1".toCharArray());
       MongoClient  mongoClient2 = new MongoClient(new ServerAddress("mo-xxxx.mo.xxx.xxx", 27017),
               Arrays.asList(credential2));

由于mongodb 會(huì)自動(dòng)啟一個(gè)demo thread 來(lái)捕獲異常,我們要捕獲連接異??梢杂孟旅娴姆绞剑?
   /**
    * Since the mongoclient will not throw an exception when connection failed. We need check the the connection after create a mongoclient
    * @param mongoClient
    * @param databaseName if not null ,then validate the connection to this database ,otherwise ,validate the connection to the admin database
    * @throws AdapterException
    */
   private static void testMongoClient(MongoClient mongoClient , String databaseName)
           throws AdapterException
   {
       try
       {
           if(databaseName != null || databaseName.isEmpty()){
               
               MongoDatabase database = mongoClient.getDatabase(databaseName);
               
               MongoIterable<String> collections = database.listCollectionNames();
               
               //if the authentication failed, will throw an exception here
               collections.iterator();
           }else{
               MongoIterable<String> databases = mongoClient.listDatabaseNames();
               if (null == databases || databases.first() == null)
               {
                   throw new AdapterException(
                           "Can not connect to MongoDB ,please check the remote source properties");
               }
           }
       }
       catch (Exception e)
       {
           throw new AdapterException(
                   "Can not connect to MongoDB ,please check the remote source properties: " + e.getMessage());
       }
   }

納米蟲分布式網(wǎng)頁(yè)爬蟲:http://nanoworm.jpmovie.cn

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容