Docs Menu
Docs Home
/ / /
Ruby MongoDB Driver
/

Common Errors

On this page

  • No Server is Available Matching Preference
  • OperationFailure When Reading local Database With Auth On
  • SocketTimeoutError and name resolution errors on EC2
  • Mongo::Auth::Unauthorized User is Not Authorized to Access

If you receive the following error:

> client[:contacts].insert_one({ vpa: 'cool@cool'})
Mongo::Error::NoServerAvailable: No server is available matching preference: #<Mongo::ServerSelector::Primary:0x14987080 tag_sets=[] max_staleness=nil> using server_selection_timeout=30 and local_threshold=0.015
from /rvm/gems/ruby-2.4.2/gems/mongo-2.5.0/lib/mongo/server_selector/selectable.rb:115:in `select_server'
from /rvm/gems/ruby-2.4.2/gems/mongo-2.5.0/lib/mongo/cluster.rb:246:in `next_primary'
from /rvm/gems/ruby-2.4.2/gems/mongo-2.5.0/lib/mongo/retryable.rb:150:in `legacy_write_with_retry'
from /rvm/gems/ruby-2.4.2/gems/mongo-2.5.0/lib/mongo/retryable.rb:103:in `write_with_retry'
from /rvm/gems/ruby-2.4.2/gems/mongo-2.5.0/lib/mongo/collection.rb:422:in `block in insert_one'
from /rvm/gems/ruby-2.4.2/gems/mongo-2.5.0/lib/mongo/client.rb:485:in `with_session'
from /rvm/gems/ruby-2.4.2/gems/mongo-2.5.0/lib/mongo/collection.rb:421:in `insert_one'
from (irb):6
from /rvm/rubies/ruby-2.4.2/bin/irb:11:in `<main>'

This error was produced on Ruby Driver version 2.6.x and earlier. This error is raised when the driver is unable to connect to the server. In order to solve this, you can try the following:

  • Update the driver to the most recent 2.x release.

  • Ensure that the port number is correct in the URI/connection string, or the host list.

  • If you are connecting to Atlas, ensure that the firewall rules are configured correctly. See the documentation on Cluster Security.

  • If your application uses a forking web server, see the documentation on Forking.

See more on this issue here: RUBY-1281.

If you are getting the following error when trying to read the local database:

Mongo::Error::OperationFailure: not authorized on local to execute command { find: "oplog.rs", filter: { ts:
{ $gte: Timestamp 1497449043000|0 } }, sort: { $natural: 1 } } (13)

You can fix this by adjusting the roles your user has and ensuring that it has privileges to the local database. If you specifically want to access the oplog, you can also create a custom role with read access to the local database's oplog.rs collection. You can find more information about role management here.

You can find more information about this issue here: MONGOID-4446.

Some users reported getting the following error:

Exception
Mongo::Error::SocketTimeoutError
Error
execution expired
/var/lib/gems/2.3.0/gems/mongo-2.5.0/lib/mongo/socket/ssl.rb:57:in `pack_sockaddr_in'
/var/lib/gems/2.3.0/gems/mongo-2.5.0/lib/mongo/socket/ssl.rb:57:in `block (2 levels) in connect!'
/var/lib/gems/2.3.0/gems/mongo-2.5.0/lib/mongo/socket.rb:199:in `handle_errors'
/var/lib/gems/2.3.0/gems/mongo-2.5.0/lib/mongo/socket/ssl.rb:57:in `block in connect!'
/usr/lib/ruby/2.3.0/timeout.rb:101:in `timeout'
/var/lib/gems/2.3.0/gems/mongo-2.5.0/lib/mongo/socket/ssl.rb:56:in `connect!'
/var/lib/gems/2.3.0/gems/mongo-2.5.0/lib/mongo/address.rb:172:in `connect_socket!'
/var/lib/gems/2.3.0/gems/mongo-2.5.0/lib/mongo/server/connection.rb:86:in `connect!'
/var/lib/gems/2.3.0/gems/mongo-2.5.0/lib/mongo/server/connectable.rb:84:in `ensure_connected'
/var/lib/gems/2.3.0/gems/mongo-2.5.0/lib/mongo/server/connection.rb:256:in `write'

This error was last reported on Ruby Driver version 2.5.1, so updating the driver can potentially solve this issue. A user has reported that they solved this issue as follows:

DNS servers on EC2 are generated in /etc/resolv.conf by default. Following the answer and setting the nameservers to Google NS I was able to fix this issue.

You can find more information about this issue here: MONGOID-4527.

Some users reported getting the following error:

Mongo::Auth::Unauthorized: User ... is not authorized to access ...

After upgrading to Ruby Driver version 2.5+, the driver was changed to use the scram authorization mechanism by default. This error might happen if the authorization mechanism your user was created with does not match the authorization mechanism used by the driver. You can specify the preferred authorization mechanism using the auth_mech option in your mongoid.yml file.

See more on this issue here: RUBY-1684.

Back

Ruby Driver Quick Start