Ruby Operation MongoDB (step five)-database management administration

Source: Internet
Author: User
Tags ldap ticket mongodb support ssl connection

Through the previous four chapters, we began to learn the data management administration in the Ruby Operation MongoDB database.

    1. Database related

The Ruby driver performs commands for database objects, gets a list of collections, and provides a variety of help for administrative tasks.

1.1 Get Collection List Collection

The name of the collection list and collection can be obtained by collections and collection_names two instructions. For example:

Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ')

Database=client.database

Database.collections

Database.collection_names

To execute any command on the database, use the Commond method

Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ')

Database=client.database

Result=database.commond (: ismaster=>1)

Result.first

1.2 Database Delete Drop databases

Use the drop method to delete a database. Call the drop method on a Database object

Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ')

Client.database.drop


2. Collection-related

The driver also provides the corresponding auxiliary function for the collection management work. Create a collection with parameters (such as creating a fixed collection), pass the parameters while getting the collection from the client connection, and then call the Create method

Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ')

ACTORS=CLIENT[:ACTORS,:CAPPED=>TRUE,:SIZE=>1024]

Actors.create

Actors.capped?

The result is true because the set: Capped=>true property

2.1 Collection Delete Drop collection

The delete function of the collection is also implemented by invoking the Drop method, but by invoking it on the collection object.

Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ')

Actors=client[:actors]

Actors.drop

2.2 Modifying read-write Preferences

To change the default read preference parameters and write preference parameters for a particular operation, we can use the With method on the collection.

Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ')

Actors=client[:actors]

Actors.with (:read=>{:mode=>:p rimary_preferred}). find.to_a

Actors.with (: Write=>{:w=>:3}). Insert_one ({:name=> ' Depeche Mode '})

3 Licensing-related

MongoDB supports multiple types of authorization mechanisms.

3.1 Create a user

To create a user for a particular database, use the Create method to pass the user name username, password password, and user role parameters roles parameters to the Create method.

Client.database.users.create (' Peter ', Password: ' Password ', Roles:[mongo::auth::roles:read_write])

3.2 Providing a certificate of entitlement

If the authorization feature is turned on, an authorization certificate is provided when creating a client connection.

Client=mongo::client.new ([' 127.0.0.1:27017 '],user: ' Test ', Password: ' 123 ')

For MongoDB versions 2.6 and later, the Auth_source default is admin, or the currently used database. If you want to switch the currently used database, you can use the using command

Client=mongo::client.new ([' 127.0.0.1:27017 '])

Film_client=client.use (' film ')

When you create an authorization certificate, you can create a new client connection, such as:

Authenticated_client=client.with (User: ' Test ', Password: ' 123 '), this creates a new client connection authenticated_client.

Similarly, setting the current database and the authorization certificate can be done in one operation.

Authenticated_film_client=client.with (:d atabase=> ' film ',user=> ' Test ',password=> ' 123 ')

3.3 MONGODB-CR mechanism

MONGODB-CR is the default authentication mechanism for versions 2.6 and above. Authentication mechanism settings can be set at the time of certificate setting

Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ',user=> ' Test ',password=> ' 123 ',: Auth_ METH=>:MONGODB_CR)

3.4 Client certificate (x509)

Support is required for version 2.6 and above.

During the SSL negotiation, the driver provides x509 authentication. The client Certificate (X509) mechanism validates the user name derived from the current certificate with the distinguished subject name. The application of this verification mechanism is based on the use of SSL connection methods. For example:
Client=mongo::client.new ([' 127.0.0.1:27017 '],:auth_mech=>:mongodb_x509,ssl:true,ssl_cert: '/path/to/ Client.pem ', Ssl_ca_cert: '/path/to/ca.pem ')

3.5 LDAP (SASL PAIN) authentication mechanism

Requires 2.6 or more enterprise versions to support.

2.6 And above enterprise versions of MongoDB support the use of LDAP authentication mechanism, that is, the use of LDAP (Lightweight Directory Access Protocol) server as a certificate of authentication.

Note: With the LDAP authentication mechanism, the password is sent to the server in plain text. Therefore, when using the love authorization authentication mechanism, it is strongly recommended to turn on SSL.

Client=mongo::client.new ([' 127.0.0.1:27017 '],:auth_mech=>:p lain,ssl:true,ssl_verify:true,ssl_cert: '/path/to /client.pem ', Ssl_ca_cert: '/path/to/ca.pem ')

3.6 Kerbero (GSSAPI) authentication mechanism

Requires 2.4 or more enterprise versions to support. Using Kerbero with JRuby in the Ruby driver requires the following steps:

1. Set a few specific system properties so that the underlying GSSAPI Java library can get a Kerberos ticket;

2. You can index to a keytab file by providing a password in the configuration file, or by setting the ' Java.security.auth.login.config ' system property. Kerberos is used in the Ruby driver through Matz's Ruby interpreter (MRI). Create a grant ticket through kinit.

Client=mongo::client.new ([' 127.0.0.1:27017 '),

: Auth_mech=>:gssapi

User: ' Test ',

Password: ' 123 ')

4th Zhi Logger Related

In addition to using the default global driver log, you can also set your own log service. The following example sets up its own log service: Mongo::logger.logger=other_logger

4.1 Modifying log levels

To modify the log level:

Mongo::logger.logger.level=logger::warn

For more control, we can pass a logger parameter when creating a client connection, providing log control for each client based on the global log.

My_logger=logger.new ($stdout)

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

4.2 Log truncation function truncate

The default log truncation feature is truncated at 250 characters, which can be turned off by passing a parameter to the client instance.

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

5 Monitoring Monitoring

The time that is generated by the instructions sent to the server when all users are initialized can be described as good fine-grained information. In order to monitor the API for each instruction issued a guaranteed quality of the starting event, that is, either success or failure. A user must implement one of the three methods of started,succeeded and failed, each as a simple parameter to the current event. The default log user in the drive is an instance

Module Mongo

Class monitoring

Class Commandlogsubscriber

Include Loggable

Attr_reader=>:options

Log_string_limit =250

def initialize (option ={})

@options =option

End

Def started (event)

Log_debug ("#{prefix" (event)}| Started|#{format_command (Event.command)} ")

End

Def succeeded (event)

Log_debug ("#{prefix" (event)}| Succeeded|#{format_command (Event.command)} ")

End

Def failed (event)

Log_debug ("#{prefix" (event)}| Failed|#{format_command (Event.command)} ")

End

Private

def format_command (args)

Begin

Truncating?truncate (args): Args.inspect

Rescue Exception

' <unable to inspect arguments> '

End

End

def prefix (event)

"#{event.address,to_s}|#{event.database_name}.#{event.command_name}"

End

def truncate (command)

((s=command.inspect). Length>log_string_limit? "#{s[0..log_string_limit]} ...": s

End

def truncating?

@truncating | | = (Options[:truncate_logs]!=false)

End

End

End

End

If you want to register a generic user, you can either global properties for all clients or the underlying settings for a single client

Mongo::monitoring::global.subscribe (Mongo::monitoring::command,my_subscriber)

Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' test ')

Client.subscribe (Mongo::monitoring::command,my_subscriber)

Turn off monitoring just set the monitoring parameter to False

Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' test ',: Monitoring=>false)


This article describes the database management knowledge and parameter settings, mainly including the database level, collection level, authentication mechanism, log, monitoring five aspects of knowledge explained.

This article from "Techfuture" blog, declined reprint!

Ruby Operation MongoDB (step five)-database management administration

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.