windows安裝MongoDB副本集,經過Java程序實現數據的插入與查詢

我本地的環境html

MongoDB 4.0java

jdk 1.7.xspring

 

安裝參考主要博客mongodb

http://www.javashuo.com/article/p-txhqnuxf-ka.html記得安裝完對解壓包作個備份數據庫

按照該網址安裝過程當中,遇到一些問題的解決方法:app

1.測試

多了一個"g",應爲spa

config_set={"_id":"mySet",members:[{_id:0,host:"127.0.0.1:27111"},{_id:1,host:"127.0.0.1:27112"},{_id:2,host:"127.0.0.1:27113"}]}

 

2.啓動MongoDB的時候,要進入相應的bin目錄.net

mongod -port 27111 -dbpath D:\MongoDB\matserdb -logpath D:\MongoDB\matserlog\mongod.log -replSet mySet -logappend

mongod -port 27112 -dbpath D:\MongoDB\slavedb -logpath D:\MongoDB\slavelog\mongod.log -replSet mySet -logappend

mongod -port 27113 -dbpath D:\MongoDB\arbiterdb -logpath D:\MongoDB\arbiterlog\mongod.log -replSet mySet -logappend

 

3.設置仲裁節點code

rs.addArb("192.168.32.138:27017") 

或者 

rs.add("host:port",{arbiterOnly:true})

 

4.默認狀況下SECONDARY節點不能讀寫,要設置slaveOK爲true才行

db.getMongo().setSlaveOk()

rs.slaveOk()

 

5.中止MongoDB服務

db.shutdownServer()

 

7.若是某一個節點不能啓動了,刪除db下的全部文件以及文件夾,將備份中的data中的全部複製到剛刪除的db中,重啓便可成功

 

8.插入測試數據

db.say.insert({"text":"hello world"});

 

9.建立用戶名、密碼

mongo --port 27017

use admin

db.createUser(
{
user: "adminUser",
pwd: "adminPass",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

 

10.Java代碼

package com.alibaba.fescar.dubbo;

import java.util.ArrayList;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;



public class ReplSetTest {

    public static void main(String[] args) {
        try {
            List<ServerAddress> addresses = new ArrayList<ServerAddress>();
            ServerAddress address1 = new ServerAddress("127.0.0.1", 27111); 
            ServerAddress address2 = new ServerAddress("127.0.0.1", 27112);
            ServerAddress address3 = new ServerAddress("127.0.0.1", 27113);
            addresses.add(address1);
            addresses.add(address2);
            addresses.add(address3);
            MongoClient client = new MongoClient(addresses);
            DB db = client.getDB( "test" );
            DBCollection coll = db.getCollection("say");
            BasicDBObject object = new BasicDBObject();
            object.append( "name" , "user28");
            DBObject dbObject = coll.findOne(object);
            
            BasicDBObject obj = new BasicDBObject();

            obj.put("name", "3333");

            obj.put("age", 330);

            obj.put("sex", 2);

            coll.insert(obj);
            
            System.out .println(dbObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
}

 

pom.xml

    <dependency>

        <groupId>org.springframework.data</groupId>

        <artifactId>spring-data-mongodb</artifactId>

        <version>1.10.0.RELEASE</version>

    </dependency>

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.2.2</version>
    </dependency>

 

數據庫顯示

 

參考的網址:

http://www.cnblogs.com/ivictor/p/6795418.html

https://blog.csdn.net/a463620704/article/details/83790566

https://blog.csdn.net/wanght89/article/details/77677271#commentBox

https://blog.csdn.net/fuck487/article/details/78287362

https://blog.csdn.net/com_it/article/details/84702046

https://blog.csdn.net/com_it/article/details/84702046

https://blog.csdn.net/yisun123456/article/details/79161279

https://blog.csdn.net/sunbocong/article/details/78642947

https://blog.csdn.net/dickysun1987/article/details/81077421

https://blog.csdn.net/com_it/article/details/84702046

https://blog.csdn.net/q36835109/article/details/78831598/

https://blog.csdn.net/caofeiliju/article/details/80194815

相關文章
相關標籤/搜索