-
启动
mongod --dbpath=/home/data
-
带权限启动
-
连接 mongo admin 数据库
mongo admin
-
增加用户
db.createUser({user:'user',pwd:'123456',roles:[{role:'dbOwner',db:'userdb'}]})
-
以带权限方式启动
mongod --dbpath=/home/data --auth
-
带权限登录
mongo admin -u user -p 123456
-
-
mongod 启动后在后台运行
-
--fork 参数 ,必须指定日志 --logpath=文件,--logappend 用户日志文件向后追加
mongod --dbpath=/home/data --logappend --logpath=/home/log/mongod.log --fork -
fork 后台运行如何关闭
kill - 9 pid 或 进入 admin --> use admin --> db.shutdownServer();
-
-
推荐 windows 查看 mongodb 管理工具 robomongo,mongoVUE
-
开启权限后出现问题
- sping boot 连接不上
- mongodb 管理工具也连接不上
- mongo admin -u user -p 123456 可以连接
原因是:mongodb 3 后默认使用了 SCRAM-SHA-1 加密方式
解决方法:
修改数据库认证方式
* 关闭mongo服务,然后以未认证形式启动
* 修改system.version文档里面的authSchema版本为3,初始安装时候应该是5
> ` use admin `
> switched to db admin
> var schema = db.system.version.findOne({"_id" : "authSchema"})
> schema.currentVersion = 3
> db.system.version.save(schema)
> WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
* 原来创建的用户已经使用了SCRAM-SHA-1认证方式,需要删除后再新建
> db.dropUser("user")
> true
> db.createUser({user:'user',pwd:'123456',roles:[{role:'dbOwner',db:'userdb'}]})
* 叁考 http://21jhf.iteye.com/blog/2216103
修改代码方式(但是数据库管理工具还是连不上)
- spring boot 配置叁考: http://zjumty.iteye.com/blog/2198432?utm_source=tuicool
主要是两个类 ,一个 MongoDBConfiguration
@Configuration
@EnableConfigurationProperties(MongoProperties.class)
public class MongoDBConfiguration {
@Autowired
private MongoProperties properties;
@Autowired(required = false)
private MongoClientOptions options;
private Mongo mongo;
@PreDestroy
public void close() {
if (this.mongo != null) {
this.mongo.close();
}
}
@Bean
public Mongo mongo() throws UnknownHostException {
this.mongo = mongoClient();
return this.mongo;
}
public MongoClient mongoClient() throws UnknownHostException{
return this.properties.createMongoClient(this.options);
}
@Bean
public MongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(mongoClient(),"gsiao");
}
}
和修改MongoProperties源码后的类,在项目下新建类org.springframework.boot.autoconfigure.mongo.MongoProperties覆盖,
主要改地方是
public MongoClient createMongoClient(MongoClientOptions options)
throws UnknownHostException {
try {
if (hasCustomAddress() || hasCustomCredentials()) {
if (options == null) {
options = MongoClientOptions.builder().build();
}
List<MongoCredential> credentials = null;
if (hasCustomCredentials()) {
String database = this.authenticationDatabase == null ? getMongoClientDatabase()
: this.authenticationDatabase;
credentials = Arrays.asList(MongoCredential.createScramSha1Credential(
this.username, database, this.password));
}
String host = this.host == null ? "localhost" : this.host;
int port = this.port == null ? DEFAULT_PORT : this.port;
return new MongoClient(Arrays.asList(new ServerAddress(host, port)),
credentials, options);
}
// The options and credentials are in the URI
return new MongoClient(new MongoClientURI(this.uri, builder(options)));
}
finally {
clearPassword();
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于