Skip to content

Instantly share code, notes, and snippets.

@davideicardi
Last active July 16, 2023 18:18
Show Gist options
  • Save davideicardi/f2094c4c3f3e00fbd490 to your computer and use it in GitHub Desktop.
Save davideicardi/f2094c4c3f3e00fbd490 to your computer and use it in GitHub Desktop.

Revisions

  1. davideicardi renamed this gist Mar 24, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. davideicardi revised this gist Dec 19, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions script.bash
    Original file line number Diff line number Diff line change
    @@ -27,3 +27,9 @@ exit
    # now you can connect with the admin user (from any mongo client >=3 )
    # remember to use --authenticationDatabase "admin"
    mongo -u "foouser" -p "foopwd" YOURHOSTIP --authenticationDatabase "admin"

    # If hosted on Azure set the docker host keep alive timeout to less than 240 seconds
    # that is the default keep alive time on Azure network infrastructure (Public Ip only)
    # See also
    # https://docs.mongodb.org/ecosystem/platforms/windows-azure/
    # http://stackoverflow.com/questions/34373003/connection-timeout-to-mongodb-on-azure-vm
  3. davideicardi revised this gist Dec 12, 2015. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion script.bash
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    # Create a container from the mongo image, run is as a daemon (-d), expose the port 27017 (-p)
    # Create a container from the mongo image,
    # run is as a daemon (-d), expose the port 27017 (-p),
    # set it to auto start (--restart)
    # and with mongo authentication (--auth)
    # Image used is https://hub.docker.com/_/mongo/
    docker pull mongo
    docker run --name YOURCONTAINERNAME --restart=always -d -p 27017:27017 mongo mongod --auth

  4. davideicardi revised this gist Dec 12, 2015. 1 changed file with 18 additions and 16 deletions.
    34 changes: 18 additions & 16 deletions script.bash
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,25 @@
    # Create a container from the mongo image, run is as a daemon (-d), expose the port 27017 (-p)
    docker pull mongo
    docker run --name YOURCONTAINERNAME --restart=always -d -p 27017:27017 mongo mongod --auth

    docker pull mongo
    docker run --name YOURCONTAINERNAME --restart=always -d -p 27017:27017 mongo
    # Using the mongo "localhost exception" (https://docs.mongodb.org/v3.0/core/security-users/#localhost-exception)
    # add a root user

    # bash into the container
    sudo docker exec -i -t YOURCONTAINERNAME bash

    # Now connect to mongo using mongodb shell and create an administrator user:
    # connect to local mongo
    mongo

    mongo YOURIP
    use admin
    db.createUser(
    {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
    }
    )
    exit

    Attach to the container with bash:
    # create the first admin user
    use admin
    db.createUser({user:"foouser",pwd:"foopwd",roles:[{role:"root",db:"admin"}]})

    sudo docker exec -i -t YOURCONTAINERNAME bash
    # exit the mongo shell
    exit
    # exit the container
    exit

    # now you can connect with the admin user (from any mongo client >=3 )
    # remember to use --authenticationDatabase "admin"
    mongo -u "foouser" -p "foopwd" YOURHOSTIP --authenticationDatabase "admin"
  5. davideicardi revised this gist Dec 12, 2015. 2 changed files with 23 additions and 18 deletions.
    23 changes: 23 additions & 0 deletions script.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    # Create a container from the mongo image, run is as a daemon (-d), expose the port 27017 (-p)

    docker pull mongo
    docker run --name YOURCONTAINERNAME --restart=always -d -p 27017:27017 mongo


    # Now connect to mongo using mongodb shell and create an administrator user:

    mongo YOURIP
    use admin
    db.createUser(
    {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
    }
    )
    exit

    Attach to the container with bash:

    sudo docker exec -i -t YOURCONTAINERNAME bash

    18 changes: 0 additions & 18 deletions script.md
    Original file line number Diff line number Diff line change
    @@ -1,18 +0,0 @@
    Create a container from the mongo image, run is as a daemon (-d), expose the port 27017 (-p) and use the host network (--net host)

    docker run --name=YOURCONTAINERNAME -d -p 27017 --net host mongo

    Now connect to mongo using mongodb shell and create an administrator user:

    use admin
    db.createUser(
    {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
    }
    )

    Exit the mongoshell, and attach to the container with bash:

    sudo docker exec -i -t YOURCONTAINERNAME bash
  6. davideicardi revised this gist Dec 12, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions script.md
    Original file line number Diff line number Diff line change
    @@ -13,3 +13,6 @@ Now connect to mongo using mongodb shell and create an administrator user:
    }
    )

    Exit the mongoshell, and attach to the container with bash:

    sudo docker exec -i -t YOURCONTAINERNAME bash
  7. davideicardi revised this gist Dec 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion script.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@

    Create a container from the mongo image, run is as a daemon (-d), expose the port 27017 (-p) and use the host network (--net host)

    docker run --name=YOURCONTAINERNAME -d -p 27017 --net host mongo

    Now connect to mongo using mongodb shell and create an administrator user:

    use admin
    db.createUser(
    {
  8. davideicardi created this gist Dec 12, 2015.
    15 changes: 15 additions & 0 deletions script.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@

    Create a container from the mongo image, run is as a daemon (-d), expose the port 27017 (-p) and use the host network (--net host)

    docker run --name=YOURCONTAINERNAME -d -p 27017 --net host mongo

    Now connect to mongo using mongodb shell and create an administrator user:
    use admin
    db.createUser(
    {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
    }
    )