如何使嵌入式 MongoDB 持久化

huangapple go评论76阅读模式
英文:

How do I make embedded mongodb persist

问题

我有一个使用嵌入式MongoDB的Spring Boot项目。它可以工作,但是对象在不同会话之间的持久性有问题,即如果我停止并重新启动服务器,数据库将为空。是否有应该设置的Spring属性?从服务器启动的日志:

2020-09-05 16:46:12.149 INFO 14155 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : 在DEFAULT模式下引导Spring Data MongoDB存储库。
2020-09-05 16:46:12.152 INFO 14155 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Spring Data存储库扫描完成,耗时3毫秒。找到1个MongoDB存储库接口。
2020-09-05 16:46:12.192 INFO 14155 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : 使用端口8080(http)初始化Tomcat。
2020-09-05 16:46:12.192 INFO 14155 --- [ restartedMain] o.apache.catalina.core.StandardService : 正在启动服务[Tomcat]。
2020-09-05 16:46:12.192 INFO 14155 --- [ restartedMain] org.apache.catalina.core.StandardEngine : 正在启动Servlet引擎:[Apache Tomcat/9.0.37]
2020-09-05 16:46:12.200 INFO 14155 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : 初始化Spring嵌入式WebApplicationContext
...
2020-09-05 16:46:14.718 INFO 14155 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload服务器正在端口35729上运行
2020-09-05 16:46:14.749 INFO 14155 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : 正在初始化ExecutorService 'applicationTaskExecutor'
2020-09-05 16:46:14.756 INFO 14155 --- [ restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping : 添加欢迎页模板:index
2020-09-05 16:46:14.777 INFO 14155 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat已启动,端口:8080(http),上下文路径:''。

任何帮助将不胜感激,
Craig

英文:

I have a Spring Boot project that uses embedded MongoDB. It works except for the persistence of objects cross-session, i.e. if I stop and start the server the DB is empty. Is there a Spring property I should be setting? From server startup:

2020-09-05 16:46:12.149  INFO 14155 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2020-09-05 16:46:12.152  INFO 14155 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 3ms. Found 1 MongoDB repository interfaces.
2020-09-05 16:46:12.192  INFO 14155 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-09-05 16:46:12.192  INFO 14155 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-09-05 16:46:12.192  INFO 14155 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-09-05 16:46:12.200  INFO 14155 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-09-05 16:46:12.201  INFO 14155 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 129 ms
2020-09-05 16:46:12.284  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : note: noprealloc may hurt performance in many applications
2020-09-05 16:46:12.286  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:12.286-0400 I CONTROL  [initandlisten] MongoDB starting : pid=15816 port=27017 dbpath=/tmp/embedmongo-db-6da83f46-38a7-4749-8a83-16096d0b9055 64-bit host=notebook
2020-09-05 16:46:12.286  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:12.286-0400 I CONTROL  [initandlisten] db version v3.5.5
2020-09-05 16:46:12.286  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:12.286-0400 I CONTROL  [initandlisten] git version: 98515c812b6fa893613f063dae568ff8319cbfbd
2020-09-05 16:46:12.286  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:12.286-0400 I CONTROL  [initandlisten] allocator: tcmalloc
2020-09-05 16:46:12.286  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:12.286-0400 I CONTROL  [initandlisten] modules: none
2020-09-05 16:46:12.286  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:12.286-0400 I CONTROL  [initandlisten] build environment:
2020-09-05 16:46:12.286  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:12.286-0400 I CONTROL  [initandlisten]     distarch: x86_64
2020-09-05 16:46:12.286  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:12.286-0400 I CONTROL  [initandlisten]     target_arch: x86_64
2020-09-05 16:46:12.286  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:12.286-0400 I CONTROL  [initandlisten] options: { net: { bindIp: "127.0.0.1", http: { enabled: false }, port: 27017 }, security: { authorization: "disabled" }, storage: { dbPath: "/tmp/embedmongo-db-6da83f46-38a7-4749-8a83-16096d0b9055", journal: { enabled: false }, mmapv1: { preallocDataFiles: false, smallFiles: true }, syncPeriodSecs: 0.0 } }
2020-09-05 16:46:12.289  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:12.289-0400 I STORAGE  [initandlisten] 
2020-09-05 16:46:12.289  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:12.289-0400 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-09-05 16:46:12.290  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:12.289-0400 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-09-05 16:46:12.290  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:12.289-0400 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7423M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=0,log_size=2GB),statistics_log=(wait=0),verbose=(recovery_progress),,log=(enabled=false),
2020-09-05 16:46:13.626  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:13.626-0400 W STORAGE  [initandlisten] Detected configuration for non-active storage engine mmapv1 when current storage engine is wiredTiger
2020-09-05 16:46:13.627  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:13.626-0400 I CONTROL  [initandlisten] 
2020-09-05 16:46:13.627  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:13.626-0400 I CONTROL  [initandlisten] ** NOTE: This is a development version (3.5.5) of MongoDB.
2020-09-05 16:46:13.627  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:13.626-0400 I CONTROL  [initandlisten] **       Not recommended for production.
2020-09-05 16:46:13.627  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:13.626-0400 I CONTROL  [initandlisten] 
2020-09-05 16:46:13.627  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:13.626-0400 I CONTROL  [initandlisten] 
2020-09-05 16:46:13.627  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:13.626-0400 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 63244 processes, 1048576 files. Number of processes should be at least 524288 : 0.5 times number of files.
2020-09-05 16:46:14.299  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:14.299-0400 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/tmp/embedmongo-db-6da83f46-38a7-4749-8a83-16096d0b9055/diagnostic.data'
2020-09-05 16:46:14.688  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:14.687-0400 I INDEX    [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
2020-09-05 16:46:14.688  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:14.688-0400 I INDEX    [initandlisten] 	 building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2020-09-05 16:46:14.690  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:14.689-0400 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs
2020-09-05 16:46:14.691  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:14.690-0400 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.4
2020-09-05 16:46:14.691  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:14.691-0400 I NETWORK  [thread1] waiting for connections on port 27017
2020-09-05 16:46:14.691  INFO 14155 --- [  restartedMain] d.f.embed.mongo.MongodExecutable         : start de.flapdoodle.embed.mongo.config.MongodConfigBuilder$ImmutableMongodConfig@5869c82a
2020-09-05 16:46:14.696  INFO 14155 --- [  restartedMain] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2020-09-05 16:46:14.699  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:14.698-0400 I NETWORK  [thread1] connection accepted from 127.0.0.1:41658 #1 (1 connection now open)
2020-09-05 16:46:14.700  INFO 14155 --- [      Thread-41] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2020-09-05T16:46:14.700-0400 I NETWORK  [conn1] received client metadata from 127.0.0.1:41658 conn1: { driver: { name: "mongo-java-driver|sync|spring-boot", version: "4.0.5" }, os: { type: "Linux", name: "Linux", architecture: "amd64", version: "5.4.0-45-generic" }, platform: "Java/Private Build/14.0.1+7-Ubuntu-1ubuntu1" }
2020-09-05 16:46:14.702  INFO 14155 --- [localhost:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:7, serverValue:1}] to localhost:27017
2020-09-05 16:46:14.703  INFO 14155 --- [localhost:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=null, roundTripTimeNanos=809545}
2020-09-05 16:46:14.718  INFO 14155 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-09-05 16:46:14.749  INFO 14155 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-05 16:46:14.756  INFO 14155 --- [  restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index
2020-09-05 16:46:14.777  INFO 14155 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''

Any help appreciated,
Craig

答案1

得分: 0

özkan pakdil指引我去了另一篇帖子,其中有答案。默认数据库位置位于/tmp,但数据不会在那里保留。我在application.properties中添加了以下Spring属性:
spring.mongodb.embedded.storage.database-dir=/home/craig/mongodb/,现在数据在不同会话中都会保留。任务完成!

英文:

özkan pakdil pointed me to another post which held the answer. The default database location is /tmp and it does not persist there. I added the spring property, in application.properties:
spring.mongodb.embedded.storage.database-dir=/home/craig/mongodb/ and it now persists cross-session. Job done!

huangapple
  • 本文由 发表于 2020年9月6日 04:57:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63758450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定