gpt4 book ai didi

ruby-on-rails - 在开发中连接到 mongolab 的 mongoid.yml 设置

转载 作者:行者123 更新时间:2023-12-02 09:32:42 25 4
gpt4 key购买 nike

对 Rails 很陌生。

使用 Rails 4.2.3

使用 Mongoid 连接到 Mongolab 数据库(不是本地 mongo 实例)

当应用程序运行时,我在开发环境中遇到以下错误。

Problem:
No configuration could be found for a session named 'default'.
Summary:
When attempting to create the new session, Mongoid could not find a session configuration for the name: 'default'. This is necessary in order to know the host, port, and options needed to connect.
Resolution:
Double check your mongoid.yml to make sure under the sessions key that a configuration exists for 'default'. If you have set the configuration programatically, ensure that 'default' exists in the configuration hash.

使用下面的 mongoid.yml。

development:
# Configure available database sessions. (required)
sessions:
default:
uri: 'mongodb://[username]:[password]@ds043497.mongolab.com:43497/[database]'
options:
consistency: :strong
max_retries: 30
retry_interval: 1
timeout: 15
test:
sessions:
default:
database: my_pm_app_test
hosts:
- localhost:27017
options:
read: primary
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0
production:
# Configure available database sessions. (required)
sessions:
default:
# The standard MongoDB connection URI allows for easy replica set
# connection setup.
# Use environment variables or a config file to keep your
# credentials safe.
uri: 'mongodb://[username]:[password]@ds043497.mongolab.com:43497/[database]'

options:
# The default consistency is :eventual, which reads from
# secondaries when possible.
# Strong forces reads to primary.
# We recommend using strong consistency.
consistency: :strong

# max_retries specifies the number of times to attempt an
# operation before giving up. Default 30 seconds
max_retries: 30

# retry_interval specifies the number of seconds to wait before
# retrying a single operation. Default 1 second.
retry_interval: 1

# The default timeout is 5, which is the time in seconds for an
# operation to time out.
# We recommend 15 because it allows for plenty of time in most
# operating environments.
# Mongoid doubles the configured value (known issue) so 15
# results in a timeout of 30s.
# Note that if you have a long-running query (over 30 seconds),
# it will time out.
# See our example for long-running queries in the blog post
# referenced above.
timeout: 15

# Set this to ensure that your writes are a round-trip operation
# and are confirmed by the system. Default (false).
safe: true

# refresh_interval specifies the number of seconds to cache information
# about a node. Default is 300 seconds (5 minutes).
refresh_interval: 10

如果我使用下面的 yml 连接到我的本地数据库实例,它工作正常

    development:
# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: my_pm_development
# Provides the hosts the default session can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- localhost:27017
options:
# Change the default write concern. (default = { w: 1 })
# write:
# w: 1

# Change the default consistency model to primary, secondary.
# 'secondary' will send reads to secondaries, 'primary' sends everything
# to master. (default: primary)
# read: secondary_preferred

# How many times Moped should attempt to retry an operation after
# failure. (default: The number of nodes in the cluster)
# max_retries: 20

# The time in seconds that Moped should wait before retrying an
# operation on failure. (default: 0.25)
# retry_interval: 0.25

# The connection pool size per-node. This should match or exceed the
# number of threads for a multi-threaded application. (default: 5)
# pool_size: 5

# The time in seconds that Moped should wait for the pool to provide
# an available connection. This number should probably remain at the
# default, unless for some reason you absolutely need to limit the
# pool_size, as this wait is only used when the pool is saturated.
# (default: 0.5)
# pool_timeout: 0.5

# The time in seconds before Moped will timeout connection and node
# operations. (default: 5)
# timeout: 5

# The amount of time in seconds between forced refreshes of node
# information including the discovery of new peers. (default: 300)
# refresh_interval: 300

# The amount of time in seconds that a node will be flagged as down.
# (default: 30)
# down_interval: 30

# Whether connections should use SSL. (default: nil/false)
# ssl: false

# Whether Moped will use the existing seeds/nodes to find other peers.
# (default: true)
# auto_discover: true


# Configure Mongoid specific options. (optional)
options:
# Includes the root model name in json serialization. (default: false)
# include_root_in_json: false

# Include the _type field in serialization. (default: false)
# include_type_for_serialization: false

# Preload all models in development, needed when models use
# inheritance. (default: false)
# preload_models: false

# Protect id and type from mass assignment. (default: true)
# protect_sensitive_fields: true

# Raise an error when performing a #find and the document is not found.
# (default: true)
# raise_not_found_error: true

# Raise an error when defining a scope with the same name as an
# existing method. (default: false)
# scope_overwrite_exception: false

# Use Active Support's time zone in conversions. (default: true)
# use_activesupport_time_zone: true

# Ensure all times are UTC in the app side. (default: false)
# use_utc: false
test:
sessions:
default:
database: my_pm_test
hosts:
- localhost:27017
options:
read: primary
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0

为什么它抛出 mongoid 在开发中找不到 session 。

谢谢

最佳答案

这是因为 yml 文件中的缩进应该始终是 2 个空格,看起来你有 3 个。

development:
# Configure available database sessions. (required)
sessions:
default:
uri: 'mongodb://[username]:[password]@ds043497.mongolab.com:43497/[database]'
options:
consistency: :strong
max_retries: 30
retry_interval: 1
timeout: 15
test:
sessions:
default:
database: my_pm_app_test
hosts:
- localhost:27017
options:
read: primary
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0
production:
# Configure available database sessions. (required)
sessions:
default:
# The standard MongoDB connection URI allows for easy replica set
# connection setup.
# Use environment variables or a config file to keep your
# credentials safe.
uri: 'mongodb://[username]:[password]@ds043497.mongolab.com:43497/[database]'
options:
# The default consistency is :eventual, which reads from
# secondaries when possible.
# Strong forces reads to primary.
# We recommend using strong consistency.
consistency: :strong

# max_retries specifies the number of times to attempt an
# operation before giving up. Default 30 seconds
max_retries: 30

# retry_interval specifies the number of seconds to wait before
# retrying a single operation. Default 1 second.
retry_interval: 1

# The default timeout is 5, which is the time in seconds for an
# operation to time out.
# We recommend 15 because it allows for plenty of time in most
# operating environments.
# Mongoid doubles the configured value (known issue) so 15
# results in a timeout of 30s.
# Note that if you have a long-running query (over 30 seconds),
# it will time out.
# See our example for long-running queries in the blog post
# referenced above.
timeout: 15

# Set this to ensure that your writes are a round-trip operation
# and are confirmed by the system. Default (false).
safe: true

# refresh_interval specifies the number of seconds to cache information
# about a node. Default is 300 seconds (5 minutes).
refresh_interval: 10

关于ruby-on-rails - 在开发中连接到 mongolab 的 mongoid.yml 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31102181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com