8月23 配置mongodb串連池 | docker 操作

來源:互聯網
上載者:User

標籤: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 操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.