Ruby Operation MongoDB (Advanced)-Create a database client connection

Source: Internet
Author: User
Tags openssl

In Ruby's MongoDB2.4.3 driver version, build a ruby database connection by creating a Mongo::client object. The Mongo::client constructor is available in two ways: one by providing a host list and some optional parameters, and through a connection URI. Create a good database connection by default to the admin database.

1. Create a database client connection using Mongo::client

1.1. Single-server mode creating a database connection

To create a database connection in single-server mode, simply provide a host connection parameter. In addition, you can force the cluster topology to be converted to stand-alone mode by eliminating the Autodiscover step. You can create a database connection in single-server mode in the following three ways

Mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' mydb ')

Mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' mydb ',:connect=>:d irect)

Mongo::client.new (' Mongodb://127.0.0.1:27017/mydb ')

1.2. Replica set mode creating a database connection

By passing one or more host address parameters and replica set names, you can create a database connection to the replica set. Even if all the information for the replica set is not fully provided at the time of creation, the MongoDB drive's auto-Explore mode can find all the members in the replica set. Creating a database connection in a replica set can be created in the following form:

Mongo::client.new ([' 127.0.0.1:27017 ', ' 127.0.0.1:27018 '],:d atabase=> ' mydb ',:replica_set=> ' MyApp ')

Mongo::client.new (' Mongdb://127.0.0.1:27017,127.0.0.1:27018/mydb?replicaset=myapp ')

1.3. Share cluster mode to create a database connection

Create a shared cluster mode by passing one or more MONGOs hosts. The auto-Explore mode can qualify the server as a MONGOs instance, but if you turn off auto-discovery mode, pass the shared parameter to the connection to create the cluster-mode database connection. You can create a database connection by using the following three examples:

Mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' mydb ')

Mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' MyDB ',: connect=>:shared)

Mongo::client.new (' mongodb://127.0.0.1:27017/mydb?connect=shared ')

2. Database Client Connection parameter options

When using mongo::client, you can configure the behavior of the driver with several different optional parameters, either by configuring them in the constructor or by providing them to mongo::client through a URI.

This is not the standard format for Ruby, given that the URI's parameter setting requires camel formatting. The following table lists the parameter configuration options for URI format and the corresponding parameter options for using the Ruby constructor. When using both of these parameter settings, it is important to note that the option time unit in the URI parameter setting is milliseconds, whereas in the Ruby constructor, the second is the float type.

3. Uri parameters and corresponding Ruby parameters


URI Options Ruby Options
Replicaset=string : replica_set=>string
Connect=string : Connect=>symbol
Ssl=boolean : Ssl=>true|false
Connecttimeoutms=integer : Connect_timeout=>float
Sockettimeoutms=integer : Socket_timeout=>float
Serverselectiontimeoutms=integer : Server_selection_timeout=>float
Localthresholdms=integer : Local_threshold=>float
Maxpoolsize=integer : Max_pool_size=>integer


Minpoolsize=integer : Min_pool_size=>integer
Waitqueuetimeoutms=integer : Wait_queue_timeout=>float
w=integer| String {: write=>{:w=>integer| String}}
Wtimeoutms=integer {: Write=>{:wtimeout=>float}}
Journal=boolean {: Write=>{:j=>true|false}}
Fsync=boolean {: Write =>{:fsync=>true|false}}
Readpreference=string {: Read=>{:mode=>symbol}}
Readpreferencetags=string {:read=>{:tag_sets=>array<string>}}



Authsource=string : auth_source=>string
Authmechanism=string : Auth_meth=>symbol
Authmechanismproperties=strings {: Auth_mech_properties=>{:service_realm=>string,:canonicalize_host_name=>true|false,:service_name= >string}}
4. The role of each option in Ruby is explained in detail below, with default values and types

Parameters Parameter description Parameter type Default value
: Replica_set When using replica set mode, this parameter sets the name of the replica set and can filter the server by name when auto-discovery mode is enabled

String

None
: SSL Notifies the client to connect to the server over SSL Boolean False
: Ssl_cert Used to set the certificate file path, which is used to identify whether the authentication to a MONGODB connection is valid. If this option is set, it will have a higher usage priority than the value of: ssl_cret_string and: Ssl_sret_object value String

None

: ssl_cert_string The string field contains information about the certificate used to authenticate connection validity using PEM encoding. If this parameter is set, it has a higher usage priority than the value of: Ssl_cret_object String None
: Ssl_cert_object Identify a connection to MongoDB using the Openssl::x509::certificate certificate Openssl::x509::certificate None
: Ssl_key The private key file that identifies the connection validity. Although the secret key file is stored in the same file as the certificate, the two need to be precisely matched. If this parameter is set, it has a higher usage priority than: Ssl_key_string and: Ssl_key_object String None
: ssl_key_string Contains information about using PEM-encoded private key strings for authentication of connection validity. If this parameter is set, it has a higher usage priority than: Ssl_key_object String None
: Ssl_key_object Private key object for authentication of connection validity OpenSSL::P Key None
: Ssl_key_pass_phrase Password information for the private key String None

: Ssl_ca_cert A set of storage paths for certificate information that connects the authorization certificate to verify that the certificate will pass from the other end of the connection. Parameters: Ssl_verfiy need to be set: Ssl_ca_cret,:ssl_ca_cret_string,:ssl_ca_cret_object one of three parameters. String None
: ssl_ca_cert_string A string that contains a set of certificate information that connects the authorization certificate for verifying that the certificate will pass from the other end of the connection. Parameters: Ssl_verfiy need to be set: Ssl_ca_cret,:ssl_ca_cret_string,:ssl_ca_cret_object one of three parameters. String None
: Ssl_ca_cert_object Represents the openssl::x509::certificate array of certificate information that connects the authorization certificate, which is used to verify that the certificate will pass from the other end of the connection. Parameters: Ssl_verfiy need to be set: Ssl_ca_cret,:ssl_ca_cret_string,:ssl_ca_cret_object one of three parameters. Array<openssl::x509::certificate> None
: ssl_verify Used to set whether peer certificate validation is performed Boolean False
: Connect_timeout The wait time (in seconds) to create a socket connection before throwing an exception Float 10 seconds
: Socket_timeout The wait time (in seconds) before a socket execution operation throws an exception Float 5 Seconds
: max_pool_size The maximum number of connections for a single server connection pool Integer 5
: min_pool_size The minimum number of connections for a single server connection pool Integer 1
: Wait_queue_timeout Wait time waiting for connection in connection pool to become active Float 1
: Write

Specify the parameters of the hash type associated with the write. The value can be: W,:wtimeout,:j,:fsync

such as {: Write =>{: W=>2}}

Hash {: w=>1}

: Read A hash type parameter that sets the preferred read mode and the label collection for the selected server. Its value can be set to: Mode and: Tag_sets. For example {: read=>{: mode=>secondary,:tag_sets=>["Berlin"]}} Hash {: mode =:p Rimary}
: Auth_source The authorization source information is set specifically String 2.6 and later, if an authorization certificate is provided, the default is admin; otherwise, the current database
: Auth_mech The authorization mechanism used for the specific settings. Can be one of the following values:: mongodb_cr,:mongodb_x509,:p Lain,:scram Symbol After the MongoDB3.0 version, if the user provided a qualification certificate but did not set: Auth_mech property, the default is: Scram. Prior to version 2.6, the default is: MONGODB_CR
: auth_mech_properties Additional authorization mechanism properties are provided Hash None
: User User name used for authentication
String None
:p Assword User password used for authentication String None
: Connect Override the auto-Explore feature of the drive and force the cluster topology to be transformed into a specific type. Includes::d Irect,:replica_set or: Shared Symbol None
: heartbeat_frequency Time for multiple monitors to flush server status asynchronously (in seconds) Float 30
:d atabase Database name that needs to be connected String Admin
: Server_selection_timeout Select an appropriate server to perform certain operations, waiting time before throwing an exception Float 30

: Local_threshold Set the maximum latency for switching between the closest server and the server that can be selected Float 0.015
5. Setting details for timeout related parameters




This article from "Techfuture" blog, declined reprint!

Ruby Operation MongoDB (Advanced)-Create a database client connection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.