W3cubDocs

/RethinkDB Ruby

ReQL command: connect

Command syntax

r.connect(opts={}) → connection

Description

Create a new connection to the database server. Accepts the following options:

  • host: the host to connect to (default localhost).
  • port: the port to connect on (default 28015).
  • db: the default database (default test).
  • user: the user account to connect as (default admin).
  • password: the password for the user account to connect as (default '', empty).
  • timeout: timeout period in seconds for the connection to be opened (default 20).
  • ssl: a hash of options to support SSL connections (default nil). Currently, there is only one option available, and if the ssl option is specified, this key is required:
    • ca_certs: a path to the SSL CA certificate.

If the connection cannot be established, a ReqlDriverError exception will be thrown.

Using SSL with RethinkDB requires proxy software on the server, such as Nginx, HAProxy or an SSL tunnel. RethinkDB will encrypt traffic and verify the CA certification to prevent man-in-the-middle attacks. Consult your proxy’s documentation for more details.

Alternatively, you may use RethinkDB’s built-in TLS support.

The RethinkDB Ruby driver includes support for asynchronous connections using EventMachine. Read the asynchronous connections documentation for more information.

Example: Open a connection using the default host and port, specifying the default database.

conn = r.connect(:db => 'marvel')

Example: Open a new connection to the database.

conn = r.connect(:host => 'localhost',
                 :port => 28015,
                 :db => 'heroes')

Example: Open a new connection to the database, specifying a user/password combination for authentication.

conn = r.connect(:host => 'localhost',
                 :port => 28015,
                 :db => 'heroes',
                 :user => 'herofinder',
                 :password => 'metropolis')

Example: Open a new connection to the database using an SSL proxy.

conn = r.connect(:host => 'localhost',
                 :port => 28015,
                 :ssl => {
                    :ca_certs => '/path/to/ca.crt'
                 })

Example: Open a connection and immediately pass it to a Ruby block. Using this style, the connection will be automatically closed when execution reaches the end of the block.

r.connect(:db => 'marvel') { |conn|
    r.table('superheroes').run(conn)
}

Related commands

Get more help

Couldn't find what you were looking for?

© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/api/ruby/connect/