Ruby Sinatra – 在mongoHQ上连接到mongoDB失败了
发布时间:2020-12-17 03:34:08 所属栏目:百科 来源:网络整理
导读:这只是我周末的项目/研究,我是Sinatra和MongoDB的新手. 我已经为mongoDB安装了宝石,例如:mongo,mongo_mapper和mongoid. 当我尝试从localhost连接到MongoHQ上的数据库时,遇到了这样的错误: Mongo::ConnectionFailure at /failed to connect to any given ho
这只是我周末的项目/研究,我是Sinatra和MongoDB的新手.
我已经为mongoDB安装了宝石,例如:mongo,mongo_mapper和mongoid. 当我尝试从localhost连接到MongoHQ上的数据库时,遇到了这样的错误: Mongo::ConnectionFailure at / failed to connect to any given host:port * file: connection.rb * location: connect * line: 489 我找到了a similar thread on SO,但坦率地说,我不太明白答案…… 这是我的代码片段: require 'rubygems' require 'sinatra' require 'mongo' require 'mongo_mapper' get '/' do MongoMapper.connection = Mongo::Connection.new('flame.mongohq.com',27044) MongoMapper.database = 'notes' MongoMapper.database.authenticate('foo','bar') erb :list end 我从here开始使用上面的代码,但似乎没有用…… 哪部分错了?还有另一种方法吗?最后,这个测试Web应用程序将部署到heroku上,所以我希望该解决方案可以同时使用localhost和我的heroku服务器. 更新: I just created a minimal code snippet to test the mongodb connection: require 'rubygems' require 'mongo' db = Mongo::Connection.new("flame.mongohq.com",27044).db("notes") 但是在超时后仍然出现错误: $ruby mongodbtest.rb /Library/Ruby/Gems/1.8/gems/mongo-1.0.8/lib/../lib/mongo/connection.rb:489:in `connect': failed to connect to any given host:port (Mongo::ConnectionFailure) from /Library/Ruby/Gems/1.8/gems/mongo-1.0.8/lib/../lib/mongo/connection.rb:137:in `initialize' from mongodbtest.rb:4:in `new' from mongodbtest.rb:4 主机名和端口是根据mongoHQ文档,因此它们必须是正确的. 我在这里先向您的帮助表示感谢. 第二次更新: 我刚用终端测试了mongodb连接字符串: mongo mongodb://flame.mongohq.com:27044/notes -u foo -p bar 不幸的是,这会让我连接失败错误,老实说,我不知道为什么… 解决方法
我用
uri = URI.parse(ENV['MONGOHQ_URL']) @mongo_connection = Mongo::Connection.from_uri( uri ) @mongo_db = @mongo_connection.db(uri.path.gsub(/^//,'')) @mongo_db.authenticate(uri.user,uri.password) 您可以使用heroku config –long命令查找您的mongo URL (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |