標籤:admin roles dev real any cat start username ret
一、配置mongodb串連池
屬性類
import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;import lombok.Getter;import lombok.Setter;@Component@PropertySource(value = "classpath:application-dev.properties")@ConfigurationProperties(prefix = "mongo")@Getter@Setterpublic class MongoSettingsProperties { private String host; private int port; private String database; private Integer minConnectionsPerHost = 0; private Integer maxConnectionsPerHost = 100; private Integer maxWaitTime = 120000; private Integer connectTimeout = 10000; private String username; private String password; private String authenticationDatabase;}
設定檔
mongo.host=42.159.xx.xxmongo.port=27018mongo.database=loreal-mdmmongo.username=mongo.password=mongo.options.min-connections-per-host=20mongo.options.max-connections-per-host=20mongo.options.max-wait-time=120000mongo.options.connect-timeout=10000mongo.authentication-database: admin
串連池 bean配置
import java.util.ArrayList;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.mongodb.MongoDbFactory;import org.springframework.data.mongodb.core.SimpleMongoDbFactory;import com.mongodb.MongoClient;import com.mongodb.MongoClientOptions;import com.mongodb.MongoCredential;import com.mongodb.ServerAddress;import cn.com.connext.commenservice.bean.MongoSettingsProperties;@Configurationpublic class MongoConfig { //覆蓋預設的MongoDbFacotry @Bean @Autowired public MongoDbFactory mongoDbFactory(MongoSettingsProperties properties) { //用戶端配置(串連數,複本集群驗證) MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); builder.connectionsPerHost(properties.getMaxConnectionsPerHost()); builder.minConnectionsPerHost(properties.getMinConnectionsPerHost()); builder.maxWaitTime(properties.getMaxWaitTime()); builder.connectTimeout(properties.getConnectTimeout()); MongoClientOptions mongoClientOptions = builder.build(); List<ServerAddress> serverAddresses = new ArrayList<>(); // MongoDB地址清單 ServerAddress serverAddress = new ServerAddress(properties.getHost(), properties.getPort()); serverAddresses.add(serverAddress); // 串連認證 List<MongoCredential> mongoCredentialList = new ArrayList<>(); MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(properties.getUsername(), properties.getAuthenticationDatabase() != null ? properties.getAuthenticationDatabase() : properties.getDatabase(), properties.getPassword().toCharArray()); mongoCredentialList.add(mongoCredential); //建立用戶端和Factory MongoClient mongoClient = new MongoClient(serverAddresses,mongoClientOptions); //new MongoClient(serverAddresses, mongoCredential, mongoClientOptions); MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, properties.getDatabase()); return mongoDbFactory; }}
參考:
80585004
http://www.xiaoqiangge.com/aritcle/1522033666922.html
二、docker 操作
找到mongodb 的設定檔,建立使用者名稱密碼
進入dockerdocker exec -it <容器名>bashwhereis mongomongo: /usr/bin/mongo[email protected]:/var/local# cd /usr/bin進入mongo./mongo 127.0.0.1:27017db.createUser({user:"lorealmdm",pwd:"[email protected]",roles:[{role:"readWriteAnyDatabase",db:"admin"}]})退出docker restart 容器ID或容器名 :不管容器是否啟動,直接重啟容器
ctrl+d 退出docker互動式
docker 中不能用vim編輯檔案
更新來源
apt-get update
安裝vim
apt-get install -y vim
參考:
52441807
78983741
關於角色:
53889990
8月23 配置mongodb串連池 | docker 操作