Table of Contents
创建索引有时会阻塞新的连接
减少is_master_interval值
理解连接处理方式并配置连接TimeoutMS
Home Database Mysql Tutorial MongoDB的PHP驱动方法与技巧

MongoDB的PHP驱动方法与技巧

Jun 07, 2016 pm 04:37 PM
mongodb php create Skill method index drive

创建索引有时会阻塞新的连接 当与mongodb复制集建立连接时,驱动程序首先尝试连接并验证集群中每个非 隐藏的节点。如果一个节点是“down”状态,将跳过。然而,如果一个节点是“UP”状态,但是持有写锁,那么验证将没法执行下去,因此该驱动程序将被挂起了。

创建索引有时会阻塞新的连接

当与mongodb复制集建立连接时,驱动程序首先尝试连接并验证集群中每个非 隐藏的节点。如果一个节点是“down”状态,将跳过。然而,如果一个节点是“UP”状态,但是持有写锁,那么验证将没法执行下去,因此该驱动程序将被挂起了。 在mongodb 2.6版本前,在建立索引时的通病。所有的在前台或后台创建索引,在secondary端将在前台创建。 在PHP驱动程序的1.5.3版本会有所改进,在创建索引时,允许验证到second节点验证。

减少is_master_interval值

对于对可用性要求高的应用程序来说,建议检查默认的驱动程序运行时的配置设置。 mongo.is_master_interval选项控制着在复制集重新选举时驱动如何快速恢复。 is_master_interval选项默认值为15s,设置驱动发送“isMaster”请求每个mongod实例的时间间隔。这些请求帮助驱动程序判断复制集的拓扑结构,具体的来说,就是请求检测哪个节点是primary并可以接收写操作。 建议将该值设置为1或2秒,以让驱动程序在集群选举或故障转移时,能够迅速的定位到primary节点。当然啦,这也取决于有多少客户端以及ping的频率。 注意,当primary节点发生变化时,如选举或故障转移,总是会有几秒钟驱动程序会收到一个“MongoConnectionException” 信息 “No candidate servers found”。这些异常需要在你的代码中进行处理,否则会终止应用程序。

理解连接处理方式并配置连接TimeoutMS

PHP驱动程序不使用连接池。因此,建议你每个PHP进程创建一个连接。但是,如果web应用程序有许多PHP工作进程,将会创建很多新的数据库连接,PHP驱动程序不能共享进程之间的连接。因此,当网络节点慢,服务器繁忙时,PHP应用程序创建初始数据库连接时特别容易受到阻碍。 在这种情况下,建议你自定义connectionTimeoutMS 选项和注意php.ini中的mongo.ping_interval选项。 connectionTimeoutMS PHP驱动程序不显示的定义一个默认的连接超时。相反,默认值由php.ini文件中的default_socket_timeout选项决定,默认是60秒。连接将等待60秒断开,时间有些长,需要降低些。 强烈建议通过连接字符串的URI选项中显示设置connectionTimeoutMS选项。将其设置为5到30秒之间的值。 mongo.ping_interval mongo.ping_interval默认值为5秒。该选项设置驱动程序发送ping请求到每个mongod实例发现“down”节点的时间间隔,用于跟踪驱动程序的服务器黑名单。告诉驱动程序哪些节点忽略。 转自:http://mongodb.info/2014/05/29/new-blog-mongodb-driver-tips-tricks-php/

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

How to configure MongoDB automatic expansion on Debian How to configure MongoDB automatic expansion on Debian Apr 02, 2025 am 07:36 AM

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Apr 06, 2025 am 12:07 AM

The... (splat) operator in PHP is used to unpack function parameters and arrays, improving code simplicity and efficiency. 1) Function parameter unpacking: Pass the array element as a parameter to the function. 2) Array unpacking: Unpack an array into another array or as a function parameter.

How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) Apr 08, 2025 am 12:03 AM

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

See all articles