生产中的Rails 5 + MongoDB问题

时间:2019-05-28 00:45:51

标签: ruby-on-rails mongodb mongoid

我刚刚将Rails 5.2.2应用程序移至生产服务器,而MongoDB出现了问题,所有查询均不起作用。这里的一些信息:

foreach($errors['text'] as $e) text($e);
foreach($errors['select'] as $e) select($e);

启用了MongoDB身份验证,已创建具有适当权限的用户,数据库不为空(已从本地副本还原),这是CentOS 7 Ruby 2.6.3 Rails (5.2.2) Mongoid (7.0.2) $ mongo --version MongoDB shell version v4.0.9 git version: fc525e2d9b0e4bceff5c2201457e564362909765 OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013 allocator: tcmalloc modules: none build environment: distmod: rhel70 distarch: x86_64 target_arch: x86_64 文件的生产部分:

mongoid.yml

我在生产日志中继续看到以下production: clients: default: database: mydb_production hosts: - localhost:27017 username: myuser password: "mypass" options: auth_mech: :scram auth_source: admin 调试消息:

[rails_root] /log/production.log

topology

连接到控制台时,任何尝试读取或写入记录的尝试都会引发以下错误:

D, [2019-05-27T20:01:58.272760 #27444] DEBUG -- : MONGODB | EVENT: #<TopologyOpening topology=Unknown[]>
D, [2019-05-27T20:01:58.272934 #27444] DEBUG -- : MONGODB | Topology type 'unknown' initializing.
D, [2019-05-27T20:01:58.273748 #27444] DEBUG -- : MONGODB | EVENT: #<TopologyChanged prev=Unknown[] new=Unknown[localhost:27017]>
D, [2019-05-27T20:01:58.273848 #27444] DEBUG -- : MONGODB | Topology type 'Unknown' changed to type 'Unknown'.
D, [2019-05-27T20:01:58.274028 #27444] DEBUG -- : MONGODB | EVENT: #<ServerOpening address=localhost:27017 topology=Unknown[localhost:27017]>
D, [2019-05-27T20:01:58.274091 #27444] DEBUG -- : MONGODB | Server localhost:27017 initializing.
D, [2019-05-27T20:01:58.365308 #27444] DEBUG -- : MONGODB | EVENT: #<ServerDescriptionChanged address=localhost:27017 topology=Single[localhost:27017] prev=#<Mongo::Server:Description:0x70049014837980 config={} average_round_trip_time=> new=#<Mongo::Server:Description:0x70048970460940 config={"ismaster"=>true, "maxBsonObjectSize"=>16777216, "maxMessageSizeBytes"=>48000000, "maxWriteBatchSize"=>100000, "localTime"=>2019-05-28 00:01:58 UTC, "logicalSessionTimeoutMinutes"=>30, "minWireVersion"=>0, "maxWireVersion"=>7, "readOnly"=>false, "ok"=>1.0} average_round_trip_time=0.088107942>>
D, [2019-05-27T20:01:58.365596 #27444] DEBUG -- : MONGODB | Server description for localhost:27017 changed from 'unknown' to 'standalone'.
D, [2019-05-27T20:01:58.365843 #27444] DEBUG -- : MONGODB | EVENT: #<TopologyChanged prev=Unknown[localhost:27017] new=Single[localhost:27017]>
D, [2019-05-27T20:01:58.366024 #27444] DEBUG -- : MONGODB | Topology type 'Unknown' changed to type 'Single'.

知道这里发生了什么吗?通过GUI远程或通过mongo命令行连接到数据库,我可以很好地阅读集合,因此我假设这是Rails和mongo之间的问题,而不是mongo本身。

2 个答案:

答案 0 :(得分:0)

首先获取异常的完整堆栈跟踪,然后从那里将问题缩小到应用程序或它正在使用的库。

要在IRB控制台中获取完整的堆栈跟踪,可以使用以下技巧:

begin; <your code here>; rescue => e; puts e.backtrace.join("\n"); end

示例:

averagest% irb
irb(main):001:0> hai
Traceback (most recent call last):
        2: from /home/w/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
        1: from (irb):1
NameError (undefined local variable or method `hai' for main:Object)
Did you mean?  hash

irb(main):003:0> begin; hai; rescue => e; puts e.backtrace.join("\n"); end
(irb):3:in `irb_binding'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb/workspace.rb:85:in `eval'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb/workspace.rb:85:in `evaluate'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb/context.rb:380:in `evaluate'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb.rb:491:in `block (2 levels) in eval_input'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb.rb:623:in `signal_status'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb.rb:488:in `block in eval_input'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb/ruby-lex.rb:246:in `block (2 levels) in each_top_level_statement'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb/ruby-lex.rb:232:in `loop'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb/ruby-lex.rb:232:in `block in each_top_level_statement'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb/ruby-lex.rb:231:in `catch'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb/ruby-lex.rb:231:in `each_top_level_statement'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb.rb:487:in `eval_input'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb.rb:428:in `block in run'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb.rb:427:in `catch'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb.rb:427:in `run'
/home/w/.rbenv/versions/2.5.1/lib/ruby/2.5.0/irb.rb:383:in `start'
/home/w/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
=> nil
irb(main):004:0> 

答案 1 :(得分:0)

结果证明我的mongoid.yml配置错误,usernamepassword应该在options组中,而不是以前版本中的外部。