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?
– BrTkCa
Lucas shell is running on mLab as well, the same user and server configured in the shell was configured in php.
– Matheus Arruda
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
– BrTkCa
Lucas, when I try to create a user
db.createUser( { user: "teste",
 pwd: "teste",
 roles: [ "readWriteAnyDatabase"

] } )
I get the error 'couldn’t add user: No role named readWriteAnyDatabase'. Have any idea how to fix this?– Matheus Arruda
Creating by the interface of mLab? Creates right by the site..
– BrTkCa
I created the same error (Not authorized for query).
– Matheus Arruda
Is that the case? https://imgur.com/a/Q1vRF
– BrTkCa
Yes, I tested it this way and it wasn’t, I upgraded the user to readWrite and it still didn’t work.
– Matheus Arruda
So sorry Matheus, it’s probably something specific to codeigniter, maybe version conflicts or configuration parameter
– BrTkCa
And I’ll keep checking the code here and see if I can find something.
– Matheus Arruda
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
– BrTkCa
I discovered in fact the problem was in the codeigniter library, it was not sending the user and password to connect to the server.
– Matheus Arruda
Boa @Matheusarruda!
– BrTkCa