Home  >  Article  >  Backend Development  >  What to do if php fails to connect to mongodb

What to do if php fails to connect to mongodb

藏色散人
藏色散人Original
2021-06-18 09:58:481910browse

The solution to the failure of php to connect to mongodb: first create a root role account; then create a userAdmin; then create a database connection account; and finally use MongoClient to connect.

What to do if php fails to connect to mongodb

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

What should I do if the php link to mongodb fails?

php cannot connect to mongodb 3.0 problem solving

  • Several commonly used roles

  • root mongodb highest authority
  • userAdmin Database account management permissions created by yourself
  • read read-only permission
  • readWrite readable and writable
  • Encountered jokes

I have created an account for the database, but PHP cannot connect. I can connect using mongo, but PHP cannot connect.

  • Correct posture

For security, we often give web applications the lowest permissions on the database to ensure database security. It can only be used for reading and absolutely not writing.

First create a root role account

After configuring mongodb no auth to start

use admin

db.createUser({
   user:'root',
   pwd:'root',
   roles:[
      {role:'root',db:'admin'}
   ]})

Create a userAdmin for our new database (test)

use test
db.createUser({
   user:'001say',
   pwd:'001say',
   roles:[
      {role:'userAdmin',db:'test'}
   ]})

Establish a database connection account

use test
db.auth('001say','001say')
db.createUser({
   user:'say001',
   pwd:'say001',
   roles:[
      {role:'read',db:'test'}
   ]})

In this way, you will get a The say001 account with read permission

When using MongoClient to connect, the following format may be required

$mongo = new MongoClient("mongodb://name:password@192.168.199.140:27017/test");

You need to specify the database directly in the connection, otherwise MongoClient will connect to the admin database by default. Your permissions are insufficient, which will of course cause the connection to fail.

  • Follow-up

The second step is indispensable. If you directly use Accounts created with root privileges cannot connect to the test database.
Mongodb provides very detailed permission management, with minimum permissions for operations on collections.
If you still can't connect, you may need the latest driver. pecl

If you encounter similar problems, I hope I can help you~~
The article is purely hand-written. If there are any errors, please contact me to modify it~~Thank you

Recommended study: " PHP video tutorial

The above is the detailed content of What to do if php fails to connect to mongodb. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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