PHP driver methods and techniques of MongoDB

Source: Internet
Author: User
Tags configuration settings failover ini mongodb time interval

Creating an index sometimes blocks new connections.

When establishing a connection with the mongodb replica set, the driver first tries to connect and verify each non-hidden node in the cluster. If a node is in the "down" status, it will be skipped. However, if a node is in the "UP" status but holding a write lock, the verification will not be executed and the driver will be suspended.

Common faults in index creation before mongodb 2.6. All indexes are created on the front-end or back-end, and on the secondary end, they are created on the front-end.

The PHP driver version 1.5.3 will be improved and can be verified to the second node during index creation.

Reduce is_master_interval value

For applications with high availability requirements, we recommend that you check the configuration settings when the default driver is running.

The mongo. is_master_interval option controls how fast recovery is driven when the replica set is re-elected.

The default value of the is_master_interval option is 15 s. It sets the time interval for the driver to send an "isMaster" request to each mongod instance. These requests help the driver determine the topology of the replica set. Specifically, the request detects which node is primary and can receive write operations.

We recommend that you set this value to 1 or 2 seconds so that the driver can quickly locate the primary node during cluster election or failover. Of course, this also depends on the number of clients and the frequency of ping.

Note that when a primary node changes, such as an election or failover, the driver will always receive a "MongoConnectionException" message "No candidate servers found" in seconds ". These exceptions must be handled in your code; otherwise, the application will be terminated.

Understand the connection processing method and configure the connection TimeoutMS

The PHP driver does not use the connection pool. Therefore, we recommend that you create a connection for each PHP process. However, if a web application has many PHP working processes, many new database connections will be created, and the PHP driver cannot share the connections between processes. Therefore, when the network node is slow and the server is busy, PHP applications are particularly vulnerable to initial database connections.

In this case, we recommend that you customize the connectionTimeoutMS option and pay attention to the mongo. ping_interval option in php. ini.

ConnectionTimeoutMS

The PHP driver does not display the definition of a default connection timeout. On the contrary, the default value is determined by the default_socket_timeout option in the php. Ini file. The default value is 60 seconds. The connection will be closed for 60 seconds, which takes a long time and needs to be reduced.

It is strongly recommended that you set the connectionTimeoutMS option in the URI option of the connection string. Set it to a value between 5 and 30 seconds.

Mongo. ping_interval

Mongo. ping_interval the default value is 5 seconds. This option sets the interval at which the driver sends a ping request to each mongod instance to find the "down" node. It is used to track the server blacklist of the driver. Tells the driver which nodes are ignored.

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.