一 整合
由於本人的碼雲太多太亂了,於是決定一個一個的整合到一個springboot項目里面。
附上自己的github項目地址 https://github.com/247292980/spring-boot
附上匯總博文地址 https://www.cnblogs.com/ydymz/p/9391653.html
以整合功能
spring-boot,FusionChart,thymeleaf,vue,ShardingJdbc,mybatis-generator,微信分享授權,drools,spring-security,spring-jpa,webjars,Aspect,drools-drt,rabbitmq,zookeeper
這次就來整合下簡單的mongodb 安裝和java使用
二 安裝
基於菜鳥 http://www.runoob.com/mongodb/mongodb-tutorial.html
1.官網安裝,沒什么好說的新手msi直接一路點下去,老手解壓安裝,那就更不用說了...
2.環境變量設置,這部很多教程不寫,其實多個這玩意cmd用起來很舒服的
3.創建數據目錄,data文件夾,conf文件夾,db文件夾,log文件夾
4.創建配置文件mongod.cfg和日志文件。配置文件自己修改成相應的地址
systemLog: destination: file path: D:\mongodb-4.0.3\data\log\mongod.log storage: dbPath: D:\mongodb-4.0.3\data\db
5.安裝成服務
mongod --config "D:\mongodb-4.0.3\conf\mongod.cfg" --install
6.啟動服務
net start MongoDB
ps.
net stop MongoDB 停止服務
mongod --remove 卸載服務
三 使用
1.啟動后台shell,
mongo
第一次是這樣的,他提示你要加個密碼
2.選擇admin數據庫
use admin
3.創建用戶
db.createUser( { user: "admin", //用戶名 pwd: "123456", //密碼 roles: [ { role: "root", db: "admin" } ] //權限 } )
ps.
user文檔字段介紹: user字段,為新用戶的名字; pwd字段,用戶的密碼; cusomData字段,為任意內容,例如可以為用戶全名介紹; roles字段,指定用戶的角色,可以用一個空數組給新用戶設定空角色; Built-In Roles(內置角色): 1. 數據庫用戶角色:read、readWrite; 2. 數據庫管理角色:dbAdmin、dbOwner、userAdmin; 3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager; 4. 備份恢復角色:backup、restore; 5. 所有數據庫角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase 6. 超級用戶角色:root // 這里還有幾個角色間接或直接提供了系統超級用戶的訪問(dbOwner 、userAdmin、userAdminAnyDatabase) 7. 內部角色:__system
3.卸載服務,重裝再啟動,注意--auth
mongod --auth --config "D:\mongodb-4.0.3\conf\mongod.cfg" --install
net start MongoDB
net stop MongoDB 停止服務
mongod --remove 卸載服務
此時啟動mongo不使用密碼登錄則看起來成功進入
實際
4.正確的啟動
mongo --port 27017 -u "admin" -p "123456" --authenticationDatabase "admin"
四 語法
有興趣的建議直接菜鳥找吧,了解一下即可
五 java使用
官方api我喜歡這樣的官方! http://mongodb.github.io/mongo-java-driver/3.7/javadoc/
坑點一
百度上大多數教程只給了代碼,但是依然不成功,因為少導了包,導致java.lang.NoClassDefFoundError: com/mongodb/DBObject
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver</artifactId> <version>3.8.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-core --> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-core</artifactId> <version>3.8.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mongodb/bson --> <dependency> <groupId>org.mongodb</groupId> <artifactId>bson</artifactId> <version>3.8.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
坑點二
哪怕你繞開的包的坑,你還會進入一個權限驗證的坑,主要是因為百度上的版本太低了,需要修改一下校驗的版本...而我是直接從官網高最新版本的,這個bug就修復了。但是,配置方面有些許不兼容。例如,mongodb和spring-mongodb默認的認證機制不同。
1、mongodb的認證機制有2種:SCRAM-SHA-1和MONGODB-CR。3.0之后版本默認為:SCRAM-SHA-1; 2、spring-mongodb默認為:MONGODB-CR,並不支持設置認證方式;但是,最新的包已修復這個問題
網上的教程,大多太舊了,所以出現jar包太老而是mongodb太老的問題,或者相反的問題,orz....其實一句話說就是注意版本或者最簡單的就是直接用最新
所以,若是你中途覺得菜鳥寫的很好,難免會進坑然后又來看我的文章,這時候,我建議你從來來一次...因為,我把前面的不兼容的錯修改,但並沒有重點指出,因為這只是版本問題。你直接再搞個低版本基本就不會有事了。
代碼
public class MongoDBConnect { public static String HOST = "127.0.0.1"; public static String PORT = "27017"; public static void main(String[] args) { try { System.out.println("MongoDBConnect to database begin"); //連接到MongoDB服務 如果是遠程連接可以替換“localhost”為服務器所在IP地址 //ServerAddress()兩個參數分別為 服務器地址 和 端口 ServerAddress serverAddress = new ServerAddress("localhost", 27017); List<ServerAddress> addrs = new ArrayList<ServerAddress>(); addrs.add(serverAddress); //MongoCredential.createScramSha1Credential()三個參數分別為 用戶名 數據庫名稱 密碼 MongoCredential credential = MongoCredential.createScramSha1Credential("admin", "admin", "123456".toCharArray()); List<MongoCredential> credentials = new ArrayList<MongoCredential>(); credentials.add(credential); //通過連接認證獲取MongoDB連接 MongoClient mongoClient = new MongoClient(addrs, credentials); //連接到數據庫 MongoDatabase mongoDatabase = mongoClient.getDatabase("admin"); System.out.println("MongoDBConnect to database successfully"); //創建集合 // mongoDatabase.createCollection("test"); // System.out.println("集合創建成功"); //選擇集合 MongoCollection<Document> collection = mongoDatabase.getCollection("test"); System.out.println("集合 test 選擇成功"); /**插入文檔 * 1. 創建文檔 org.bson.Document 參數為key-value的格式 * 2. 創建文檔集合List<Document> * 3. 將文檔集合插入數據庫集合中 mongoCollection.insertMany(List<Document>) 插入單個文檔可以用 mongoCollection.insertOne(Document) * */ Document document = new Document("title", "MongoDB"). append("description", "database"). append("likes", 100). append("by", "Fly"); List<Document> documents = new ArrayList<Document>(); documents.add(document); collection.insertMany(documents); System.out.println("文檔插入成功"); /**檢索所有文檔 * 1. 獲取迭代器FindIterable<Document> * 2. 獲取游標MongoCursor<Document> * 3. 通過游標遍歷檢索出的文檔集合 * */ FindIterable<Document> findIterable = collection.find(); MongoCursor<Document> mongoCursor = findIterable.iterator(); while (mongoCursor.hasNext()) { System.out.println(mongoCursor.next()); } System.out.println("檢索所有文檔成功"); //更新文檔 將文檔中likes=100的文檔修改為likes=200 collection.updateMany(Filters.eq("likes", 100), new Document("$set", new Document("likes", 200))); //檢索查看結果 findIterable = collection.find(); mongoCursor = findIterable.iterator(); while (mongoCursor.hasNext()) { System.out.println(mongoCursor.next()); } System.out.println("更新文檔成功"); //刪除符合條件的第一個文檔 collection.deleteOne(Filters.eq("likes", 200)); //刪除所有符合條件的文檔 collection.deleteMany(Filters.eq("likes", 200)); //檢索查看結果 findIterable = collection.find(); mongoCursor = findIterable.iterator(); while (mongoCursor.hasNext()) { System.out.println(mongoCursor.next()); } System.out.println("刪除文檔成功"); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } } }
六 總結
關於mongodb,redis,mysql 簡要對比,其實這篇文章寫得很好,我給個結論就行了
https://www.cnblogs.com/lovychen/p/5613986.html
mongodb:
它是一個內存數據庫,操作的數據都是放在內存里面的。
但實際數據存在硬盤中,mmap的方式可以說是索引在內存中。
持久化方式:
mongodb的所有數據實際上是存放在硬盤的,所有要操作的數據通過mmap的方式映射到內存某個區域內。mongodb就在這塊區域里面進行數據修改,避免了零碎的硬盤操作。
至於mmap上的內容flush到硬盤就是操作系統的事情了,所以如果mongodb在內存中修改了數據后,mmap數據flush到硬盤之前,系統宕機了,數據就會丟失。
redis:
它就是一個不折不扣的內存數據庫了。
redis所有數據都是放在內存中的,持久化是使用RDB方式或者aof方式。
mysql:
無論數據還是索引都存放在硬盤中。到要使用的時候才交換到內存中。能夠處理遠超過內存總量的數據。
總結就是
虛擬內存不夠是 選擇mongodb和mysql
虛擬內存夠是 選擇mongodb和redis
但實際上,更多公司選擇redis和mysql,這就是技術棧的問題,畢竟nosql的定義和開發設計沒幾個程序員了解