简体   繁体   English

MongoDb和morphia密码和用户名

[英]MongoDb and morphia password and username

playing around with this and cannot get a grip 玩这个,不能抓
on how to connect with password. 关于如何使用密码连接。

In MongoDb it's quite easy to understand but when i use morphia it's not 在MongoDb中,它很容易理解,但是当我使用吗?
so quite obvious because documentation is minimal or i missed it. 非常明显,因为文档很少,或者我错过了。

I already have a Collection with 5 documents. 我已经有一个包含5个文档的收藏集。
Can i apply a user/pass on that one? 我可以申请一个用户/通行证吗?

How can i modify this code so logging uses a username and password? 如何修改此代码,以便日志记录使用用户名和密码?

Took the code from this post (Thanks @Lennart Koopmann) 摘自这篇文章的代码(感谢@Lennart Koopmann)
MongoDB Java driver tutorial MongoDB Java驱动程序教程

public final class MongoConnectionManager {

 private static final MongoConnectionManager
 INSTANCE = new MongoConnectionManager();
 private final Datastore db;
 public static final String DB_NAME = "mongo_database";

 private MongoConnectionManager() {
            try {
                Mongo m = new Mongo("localhost", 27017);
            db = new Morphia().map(UserData.class)
     .map(Statistic.class)
     .map(FriendList.class)
     .map(ServerData.class)
     .map(BatchData.class).createDatastore(m, DB_NAME);

                db.ensureIndexes();
            }
            catch (Exception e) {
                throw new RuntimeException("Error initializing mongo db", e);
            }
        }

        public static MongoConnectionManager instance() {
            return INSTANCE;
        }

        public Datastore getDb() {

            return db;
        }   
    }

maybe you could use the following code: 也许您可以使用以下代码:

Morphia morphia = new Morphia();
ServerAddress addr = new ServerAddress("host", 27017);
List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();
MongoCredential credentia = MongoCredential.createCredential(
    "username", "dbname", "password".toCharArray());
credentialsList.add(credentia);
MongoClient client = new MongoClient(addr, credentialsList);
datastore = morphia.createDatastore(client, "dbname");
createDatastore(m, DB_NAME, username, password)

This overloaded method has been introduced here . 这里介绍 这种重载方法。

Maybe you are using older Moprhia client? 也许您正在使用较旧的Moprhia客户端?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM