Mongodb query works in shell, and does not work in PHP

Asked

Viewed 58 times

0

I need help with the query in the mongoDB database, in the shell I can perform the search normally, however on the site I get the error 'not authorized for query on'. I use the mLab service for database.

I am using the same user in the shell and in PHP, the version of my database is 3.4.10 and the version of the codeigniter library is 1.0.

Shell connection:

mongo ds249757.mlab.com:49757/tx5 -u user -p pass

Codeigniter connection

<?php

//mongodb host
$config['default']['mongo_hostbase'] = 'ds249757.mlab.com:49757';
//mongodb name

$config['default']['mongo_database'] = 'tx5';
//mongodb username - by default, it is empty
$config['default']['mongo_username'] = 'user';
//mongodb password - by default, it is empty
$config['default']['mongo_password'] = 'pass';      
$config['default']['mongo_persist']  = FALSE;
$config['default']['mongo_persist_key']  = 'ci_persist';
$config['default']['mongo_replica_set']  = FALSE;
$config['default']['mongo_query_safety'] = 'safe';
$config['default']['mongo_suppress_connect_error'] = FALSE;
$config['default']['mongo_host_db_flag']   = FALSE; 

?>

Query in codeigniter

<?php 
        $this->load->library('mongo_db');
        $category = $this->mongo_db->db->admin->find();
       foreach ($category  as $cat)
       {
        $name=$cat['username'];
       $passwd=$cat['password'];
       }
         ?>

Canvases

<p>Permissões Usuário
<a href="https://i.stack.imgur.com/xU7pY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xU7pY.png" alt="enter image description here"></a></p>

<p>Query no Shell
<a href="https://i.stack.imgur.com/FgdyN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FgdyN.png" alt="enter image description here"></a></p>

<p>Query no codeigniter
<a href="https://i.stack.imgur.com/43E4U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/43E4U.png" alt="enter image description here"></a></p>

<p>Banco de dados MLAB
<a href="https://i.stack.imgur.com/OEp3O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OEp3O.png" alt="enter image description here"></a></p>

  • mLab usually requires you to create a user with read or read and write permission in the database. Have you created one? Mongo Shell is running on mLab or localhost?

  • Lucas shell is running on mLab as well, the same user and server configured in the shell was configured in php.

  • The message is generic Matheus, it just says it’s not authorized. I am not an expert in codeigniter, but I suggest that there in mlab you create a Sario (if not already done) in the user guide. Give read and write permission and test with the created user, user and password. I asked in vdd if you are running the shell inside mLab (on the site). Because this on different domain could result in unauthorized

  • Lucas, when I try to create a user db.createUser( { user: "teste",&#xA; pwd: "teste",&#xA; roles: [ "readWriteAnyDatabase"&#xA;&#xA;] } ) I get the error 'couldn’t add user: No role named readWriteAnyDatabase'. Have any idea how to fix this?

  • Creating by the interface of mLab? Creates right by the site..

  • I created the same error (Not authorized for query).

  • Is that the case? https://imgur.com/a/Q1vRF

  • Yes, I tested it this way and it wasn’t, I upgraded the user to readWrite and it still didn’t work.

  • So sorry Matheus, it’s probably something specific to codeigniter, maybe version conflicts or configuration parameter

  • And I’ll keep checking the code here and see if I can find something.

  • If no one here is more assertive, try changing the parameters, changing version of lib, testing for another language (java, Node, etc), etc. good luck

  • 1

    I discovered in fact the problem was in the codeigniter library, it was not sending the user and password to connect to the server.

  • Boa @Matheusarruda!

Show 8 more comments

1 answer

2


The problem was in the codeigniter library, it was not sending the user and password to connect to the server.

  • would like to understand how to solve this problem. is possible?

  • @luismare which library are you using? Are you trying to make the connection by code Igniter? I think it is feasible to open a new question with the details of your question, in which case leave the link here that I try to help you there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.