Ubuntu MongoDB LDAP配置
2017-11-01 本文已影响350人
30而立人
Ubuntu LDAP 配置可以参考另一篇文章 Ubuntu 下LDPA服务搭建配置
在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文件默认在 /var/log/mongodb
MongoDB的data文件默认在 /var/lib/mongodb
安装依赖
sudo apt-get install sasl2-bin
配置mongoDB
修改 /etc/default/saslauthd 文件,改成下面的值:
MECHANISMS="ldap"
START=yes
然后启动saslauthd :
/etc/init.d/saslauthd start
可以通过命令 ps wax | grep saslauthd 查看saslauthd 服务是否启动
更改/etc 下的 saslauthd.conf 文件,如果没有这个文件,就创建一个。加入下面的内容
ldap_servers: ldap://localhost:389
ldap_search_base: ou=people,dc=example,dc=com
ldap_filter: (uid=%u)
好了,我们现在来测试下能否通过用户名和密码连接上LDAP
先用错误的密码:
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增加一个LDAP用户,比如:
精品电影网:手机电影迅雷下载,手机在线电影,手机高清电影,手机迅雷观看
service mongod start
mongod
用第三方管理工具(Robo)或者mongo终端执行下面命令:
db.getSiblingDB("$external").createUser(
{
user : "user1",
roles: [ { role: "read", db: "kundatabase" } ]
}
)
关闭mongo服务,ctrl+c ,然后重新已LDAP认证方式启动:
mongod --auth --setParameter saslauthdPath=/var/run/saslauthd/mux --setParameter authenticationMechanisms=PLAIN
在mongoDB验证LDAP 用户名和密码:
db.getSiblingDB("$external").auth(
{
mechanism: "PLAIN",
user: "user1",
pwd: "user1",
digestPassword: false
}
)
另外,如果想通过JAVA以LDAP 认证的方式连接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 会自动启一个demo thread 来捕获异常,我们要捕获连接异常可以用下面的方式:
/**
* 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());
}
}
纳米虫分布式网页爬虫:http://nanoworm.jpmovie.cn