OperationFailure: not authorized on tracking to execute command












3














I did the following



-- `sudo apt-get install mongodb-org`
-- go to `etc/mongod.conf` change bindIp to: `0.0.0.0`
-- sudo mkdir /data/db
-- start without auth to create user
`sudo mongod --port 27017 --dbpath /data/db`
-- open shell with : mongo --port 27017

```
> use admin
> db.createUser( { user: "useradmin", pwd: "mypassword", roles: [ { role: "root", db: "admin" } ] } )

```

-- Restart with auth required(ctrl+c the above mongod process):
`sudo mongod --auth --port 27017 --dbpath /data/db'

-- To open shell(ctrl+c above mongo shell):
`mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin`


my mongoengine_settings.py



```PYTHON
from mongoengine import connect

DATABASE = 'tracking'
USERNAME = 'useradmin'
PASSWORD = 'mypassword'
HOST = 'mongodb://localhost/tracking'
PORT = 27017

connect(DATABASE,
username=USERNAME,
password=PASSWORD,
host=HOST,
port=PORT
)

```


now when i try to bulk insert some data using mongoengine that works fine if i don't have --auth enabled, otherwise it throws the following error:



OperationFailure(u'command SON([('createIndexes', u'order'), ('indexes', [{'unique': True, 'background': False, 'sparse': False, 'key': SON([('order_id', 1)]), 'name': u'order_id_1'}])]) on namespace tracking.$cmd failed: not authorized on tracking to execute command { createIndexes: "order", indexes: [ { unique: true, background: false, sparse: false, key: { order_id: 1 }, name: "order_id_1" } ] }',)


what am i doing wrong?










share|improve this question



























    3














    I did the following



    -- `sudo apt-get install mongodb-org`
    -- go to `etc/mongod.conf` change bindIp to: `0.0.0.0`
    -- sudo mkdir /data/db
    -- start without auth to create user
    `sudo mongod --port 27017 --dbpath /data/db`
    -- open shell with : mongo --port 27017

    ```
    > use admin
    > db.createUser( { user: "useradmin", pwd: "mypassword", roles: [ { role: "root", db: "admin" } ] } )

    ```

    -- Restart with auth required(ctrl+c the above mongod process):
    `sudo mongod --auth --port 27017 --dbpath /data/db'

    -- To open shell(ctrl+c above mongo shell):
    `mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin`


    my mongoengine_settings.py



    ```PYTHON
    from mongoengine import connect

    DATABASE = 'tracking'
    USERNAME = 'useradmin'
    PASSWORD = 'mypassword'
    HOST = 'mongodb://localhost/tracking'
    PORT = 27017

    connect(DATABASE,
    username=USERNAME,
    password=PASSWORD,
    host=HOST,
    port=PORT
    )

    ```


    now when i try to bulk insert some data using mongoengine that works fine if i don't have --auth enabled, otherwise it throws the following error:



    OperationFailure(u'command SON([('createIndexes', u'order'), ('indexes', [{'unique': True, 'background': False, 'sparse': False, 'key': SON([('order_id', 1)]), 'name': u'order_id_1'}])]) on namespace tracking.$cmd failed: not authorized on tracking to execute command { createIndexes: "order", indexes: [ { unique: true, background: false, sparse: false, key: { order_id: 1 }, name: "order_id_1" } ] }',)


    what am i doing wrong?










    share|improve this question

























      3












      3








      3







      I did the following



      -- `sudo apt-get install mongodb-org`
      -- go to `etc/mongod.conf` change bindIp to: `0.0.0.0`
      -- sudo mkdir /data/db
      -- start without auth to create user
      `sudo mongod --port 27017 --dbpath /data/db`
      -- open shell with : mongo --port 27017

      ```
      > use admin
      > db.createUser( { user: "useradmin", pwd: "mypassword", roles: [ { role: "root", db: "admin" } ] } )

      ```

      -- Restart with auth required(ctrl+c the above mongod process):
      `sudo mongod --auth --port 27017 --dbpath /data/db'

      -- To open shell(ctrl+c above mongo shell):
      `mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin`


      my mongoengine_settings.py



      ```PYTHON
      from mongoengine import connect

      DATABASE = 'tracking'
      USERNAME = 'useradmin'
      PASSWORD = 'mypassword'
      HOST = 'mongodb://localhost/tracking'
      PORT = 27017

      connect(DATABASE,
      username=USERNAME,
      password=PASSWORD,
      host=HOST,
      port=PORT
      )

      ```


      now when i try to bulk insert some data using mongoengine that works fine if i don't have --auth enabled, otherwise it throws the following error:



      OperationFailure(u'command SON([('createIndexes', u'order'), ('indexes', [{'unique': True, 'background': False, 'sparse': False, 'key': SON([('order_id', 1)]), 'name': u'order_id_1'}])]) on namespace tracking.$cmd failed: not authorized on tracking to execute command { createIndexes: "order", indexes: [ { unique: true, background: false, sparse: false, key: { order_id: 1 }, name: "order_id_1" } ] }',)


      what am i doing wrong?










      share|improve this question













      I did the following



      -- `sudo apt-get install mongodb-org`
      -- go to `etc/mongod.conf` change bindIp to: `0.0.0.0`
      -- sudo mkdir /data/db
      -- start without auth to create user
      `sudo mongod --port 27017 --dbpath /data/db`
      -- open shell with : mongo --port 27017

      ```
      > use admin
      > db.createUser( { user: "useradmin", pwd: "mypassword", roles: [ { role: "root", db: "admin" } ] } )

      ```

      -- Restart with auth required(ctrl+c the above mongod process):
      `sudo mongod --auth --port 27017 --dbpath /data/db'

      -- To open shell(ctrl+c above mongo shell):
      `mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin`


      my mongoengine_settings.py



      ```PYTHON
      from mongoengine import connect

      DATABASE = 'tracking'
      USERNAME = 'useradmin'
      PASSWORD = 'mypassword'
      HOST = 'mongodb://localhost/tracking'
      PORT = 27017

      connect(DATABASE,
      username=USERNAME,
      password=PASSWORD,
      host=HOST,
      port=PORT
      )

      ```


      now when i try to bulk insert some data using mongoengine that works fine if i don't have --auth enabled, otherwise it throws the following error:



      OperationFailure(u'command SON([('createIndexes', u'order'), ('indexes', [{'unique': True, 'background': False, 'sparse': False, 'key': SON([('order_id', 1)]), 'name': u'order_id_1'}])]) on namespace tracking.$cmd failed: not authorized on tracking to execute command { createIndexes: "order", indexes: [ { unique: true, background: false, sparse: false, key: { order_id: 1 }, name: "order_id_1" } ] }',)


      what am i doing wrong?







      mongodb mongoengine






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 14 '16 at 11:03









      Crazyshezy

      96321937




      96321937
























          2 Answers
          2






          active

          oldest

          votes


















          6














          MongoDB users are created in a specific database rather than at the instance level. Once created users can be granted different roles for different databases. The database a user is created in is called their authentication database



          Because usernames are not unique (only the combination of username and authentication database is) you can create two users with the same name in different databases with different roles and passwords. This also means that when connecting you need to specify the authentication database as well as the username and password.



          This is why after creating the useradmin user in the admin database you needed to run this command:



          mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin



          to connect the MongoDB shell to the default database test.



          If you don't specify the authentication database explicitly then MongoDB assumes the database you are connecting to is also the authentication database. So connecting to the admin database like this would have worked:



          mongo --port 27017 -u useradmin -p mypassword admin



          and these three commands are effectively the same and all will return an "Authentication failed" error :



          mongo --port 27017 -u useradmin -p mypassword
          mongo --port 27017 -u useradmin -p my password test
          mongo --port 27017 -u useradmin -p my password test --authenticationDatabase test





          To connect from Python, if you use MongoClient and pass it a complete MongoDB URI, the connection string can include optional parameters. One of the options is authSource (the the database name associated with the user’s credentials) which is obviously what you need: connection options.



          Your URI will look something like this:




          MdbURI = "mongodb://useradmin:mypassword@localhost:27017/tracking?authSource=admin"
          client = MongoClient(MdbURI)






          share|improve this answer





















          • Wow this worked for me!
            – Souvik Ray
            Jan 25 at 12:37



















          0














          Here's a means of connecting and authenticating with pymongo:



          from pymongo import MongoClient

          # MongoDB connection info
          hostname = '10.20.30.40'
          port = 27017
          username = 'adminUserName'
          password = 'secret'
          databaseName = 'someDB'

          # connect with authentication
          client = MongoClient(hostname, port)
          db = client[databaseName]
          db.authenticate(username, password)





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35985762%2foperationfailure-not-authorized-on-tracking-to-execute-command%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            6














            MongoDB users are created in a specific database rather than at the instance level. Once created users can be granted different roles for different databases. The database a user is created in is called their authentication database



            Because usernames are not unique (only the combination of username and authentication database is) you can create two users with the same name in different databases with different roles and passwords. This also means that when connecting you need to specify the authentication database as well as the username and password.



            This is why after creating the useradmin user in the admin database you needed to run this command:



            mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin



            to connect the MongoDB shell to the default database test.



            If you don't specify the authentication database explicitly then MongoDB assumes the database you are connecting to is also the authentication database. So connecting to the admin database like this would have worked:



            mongo --port 27017 -u useradmin -p mypassword admin



            and these three commands are effectively the same and all will return an "Authentication failed" error :



            mongo --port 27017 -u useradmin -p mypassword
            mongo --port 27017 -u useradmin -p my password test
            mongo --port 27017 -u useradmin -p my password test --authenticationDatabase test





            To connect from Python, if you use MongoClient and pass it a complete MongoDB URI, the connection string can include optional parameters. One of the options is authSource (the the database name associated with the user’s credentials) which is obviously what you need: connection options.



            Your URI will look something like this:




            MdbURI = "mongodb://useradmin:mypassword@localhost:27017/tracking?authSource=admin"
            client = MongoClient(MdbURI)






            share|improve this answer





















            • Wow this worked for me!
              – Souvik Ray
              Jan 25 at 12:37
















            6














            MongoDB users are created in a specific database rather than at the instance level. Once created users can be granted different roles for different databases. The database a user is created in is called their authentication database



            Because usernames are not unique (only the combination of username and authentication database is) you can create two users with the same name in different databases with different roles and passwords. This also means that when connecting you need to specify the authentication database as well as the username and password.



            This is why after creating the useradmin user in the admin database you needed to run this command:



            mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin



            to connect the MongoDB shell to the default database test.



            If you don't specify the authentication database explicitly then MongoDB assumes the database you are connecting to is also the authentication database. So connecting to the admin database like this would have worked:



            mongo --port 27017 -u useradmin -p mypassword admin



            and these three commands are effectively the same and all will return an "Authentication failed" error :



            mongo --port 27017 -u useradmin -p mypassword
            mongo --port 27017 -u useradmin -p my password test
            mongo --port 27017 -u useradmin -p my password test --authenticationDatabase test





            To connect from Python, if you use MongoClient and pass it a complete MongoDB URI, the connection string can include optional parameters. One of the options is authSource (the the database name associated with the user’s credentials) which is obviously what you need: connection options.



            Your URI will look something like this:




            MdbURI = "mongodb://useradmin:mypassword@localhost:27017/tracking?authSource=admin"
            client = MongoClient(MdbURI)






            share|improve this answer





















            • Wow this worked for me!
              – Souvik Ray
              Jan 25 at 12:37














            6












            6








            6






            MongoDB users are created in a specific database rather than at the instance level. Once created users can be granted different roles for different databases. The database a user is created in is called their authentication database



            Because usernames are not unique (only the combination of username and authentication database is) you can create two users with the same name in different databases with different roles and passwords. This also means that when connecting you need to specify the authentication database as well as the username and password.



            This is why after creating the useradmin user in the admin database you needed to run this command:



            mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin



            to connect the MongoDB shell to the default database test.



            If you don't specify the authentication database explicitly then MongoDB assumes the database you are connecting to is also the authentication database. So connecting to the admin database like this would have worked:



            mongo --port 27017 -u useradmin -p mypassword admin



            and these three commands are effectively the same and all will return an "Authentication failed" error :



            mongo --port 27017 -u useradmin -p mypassword
            mongo --port 27017 -u useradmin -p my password test
            mongo --port 27017 -u useradmin -p my password test --authenticationDatabase test





            To connect from Python, if you use MongoClient and pass it a complete MongoDB URI, the connection string can include optional parameters. One of the options is authSource (the the database name associated with the user’s credentials) which is obviously what you need: connection options.



            Your URI will look something like this:




            MdbURI = "mongodb://useradmin:mypassword@localhost:27017/tracking?authSource=admin"
            client = MongoClient(MdbURI)






            share|improve this answer












            MongoDB users are created in a specific database rather than at the instance level. Once created users can be granted different roles for different databases. The database a user is created in is called their authentication database



            Because usernames are not unique (only the combination of username and authentication database is) you can create two users with the same name in different databases with different roles and passwords. This also means that when connecting you need to specify the authentication database as well as the username and password.



            This is why after creating the useradmin user in the admin database you needed to run this command:



            mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin



            to connect the MongoDB shell to the default database test.



            If you don't specify the authentication database explicitly then MongoDB assumes the database you are connecting to is also the authentication database. So connecting to the admin database like this would have worked:



            mongo --port 27017 -u useradmin -p mypassword admin



            and these three commands are effectively the same and all will return an "Authentication failed" error :



            mongo --port 27017 -u useradmin -p mypassword
            mongo --port 27017 -u useradmin -p my password test
            mongo --port 27017 -u useradmin -p my password test --authenticationDatabase test





            To connect from Python, if you use MongoClient and pass it a complete MongoDB URI, the connection string can include optional parameters. One of the options is authSource (the the database name associated with the user’s credentials) which is obviously what you need: connection options.



            Your URI will look something like this:




            MdbURI = "mongodb://useradmin:mypassword@localhost:27017/tracking?authSource=admin"
            client = MongoClient(MdbURI)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 22 '16 at 6:04









            William Byrne III

            1214




            1214












            • Wow this worked for me!
              – Souvik Ray
              Jan 25 at 12:37


















            • Wow this worked for me!
              – Souvik Ray
              Jan 25 at 12:37
















            Wow this worked for me!
            – Souvik Ray
            Jan 25 at 12:37




            Wow this worked for me!
            – Souvik Ray
            Jan 25 at 12:37













            0














            Here's a means of connecting and authenticating with pymongo:



            from pymongo import MongoClient

            # MongoDB connection info
            hostname = '10.20.30.40'
            port = 27017
            username = 'adminUserName'
            password = 'secret'
            databaseName = 'someDB'

            # connect with authentication
            client = MongoClient(hostname, port)
            db = client[databaseName]
            db.authenticate(username, password)





            share|improve this answer


























              0














              Here's a means of connecting and authenticating with pymongo:



              from pymongo import MongoClient

              # MongoDB connection info
              hostname = '10.20.30.40'
              port = 27017
              username = 'adminUserName'
              password = 'secret'
              databaseName = 'someDB'

              # connect with authentication
              client = MongoClient(hostname, port)
              db = client[databaseName]
              db.authenticate(username, password)





              share|improve this answer
























                0












                0








                0






                Here's a means of connecting and authenticating with pymongo:



                from pymongo import MongoClient

                # MongoDB connection info
                hostname = '10.20.30.40'
                port = 27017
                username = 'adminUserName'
                password = 'secret'
                databaseName = 'someDB'

                # connect with authentication
                client = MongoClient(hostname, port)
                db = client[databaseName]
                db.authenticate(username, password)





                share|improve this answer












                Here's a means of connecting and authenticating with pymongo:



                from pymongo import MongoClient

                # MongoDB connection info
                hostname = '10.20.30.40'
                port = 27017
                username = 'adminUserName'
                password = 'secret'
                databaseName = 'someDB'

                # connect with authentication
                client = MongoClient(hostname, port)
                db = client[databaseName]
                db.authenticate(username, password)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 at 16:37









                Ryan Loggerythm

                491512




                491512






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35985762%2foperationfailure-not-authorized-on-tracking-to-execute-command%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin