MongoDb with Laravel (Mac & Homestead Installation)

i just started to learn MongoDb so this post is simply a reference on how to get things starting without too much trouble especially if you are coming from the world of Mysql.

Mac

# Install Mongodb & Php-Ext

# install brew if you don't have it

$ brew update
$ brew install mongodb

####################################################
# dbpath is found in mongo.conf
# so you can ignore the alert & escape this part
$ mkdir -p /data/db
$ whoami
> username
$ sudo chown username /data/db
####################################################

$ brew install php70-mongodb

# Create Username & Password for the Db

- install https://moloquent.github.io/master/
- by default mongo starts with a default connection/db call “test” which you can connect to it without any creds, so assume you have the below in ur “.env” file

DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

and in “config/database.php >> connections array”

'mongodb' => [
    'driver'       => 'mongodb',
    'host'         => env('DB_HOST', 'localhost'),
    'port'         => env('DB_PORT', 27017),
    'database'     => env('DB_DATABASE'),
    'username'     => env('DB_USERNAME'),
    'password'     => env('DB_PASSWORD'),
    'use_mongo_id' => false,
    'options'      => [
        'database' => 'admin',
        'username' => 'mongo',
        'password' => 'mongo',
    ],
],

running the migration now will give an error

[MongoDB\Driver\Exception\AuthenticationException]

Authentication failed.

so now we need to create 2 users, Also Check Built-In Roles & User Management

$ mongo

# first the superUser, change the user/pass to anything u want
$ use admin
$ db.createUser({ user: "mongo", pwd: "mongo", roles: [{ role: "root", db: "admin" }] })

# then the app db (according to the app db name)
$ use homestead
$ db.createUser({ user: "homestead", pwd: "secret", roles: [{ role: "dbOwner", db: "homestead" }] })

# exit the bash

- now try to migrate again and all will go as expected.
.
.
.

# More About Admin

1- edit mongod.conf which is found at /usr/local/etc/mongod.conf

  • you can also find it by brew info mongodb
security:
   authorization: enabled

2- then run

$ brew services restart mongodb

3- test if all good

$ mongo
$ db.serverCmdLineOpts()
{
  "ok" : 0,
  "errmsg" : "not authorized on admin to execute command { getCmdLineOpts: 1.0 }",
  "code" : 13
}

# exit the shell

which means the anonymous editing it not possible, so now lets try with the admin

$ mongo admin -u user -p pass
$ db.serverCmdLineOpts()
{
    "argv" : [
        ...
    ],
    "parsed" : {
        ...
    },
    "ok" : 1
}

SUCCESS, now when u try to connect to the db it wont connect unless you specified the admin user/pass, exactly how it should be 🏆.
Read More


HomeStead

1- ssh into ur vm
2- use the script from https://github.com/zakhttp/Mongostead7
3- Create Username & Password for the Db as above.
4- edit mongod.conf which is found at /etc/mongod.conf

  • you can also find it by sudo systemctl status mongodb
# the same ip in Homestead.yaml or whatever you assigned for your app in the 'hosts' file
net:
    bindIp: 127.0.0.1,192.168.10.10

security:
    authorization: enabled

5- now run

sudo systemctl restart mongodb

6- now you can connect to the db by using 192.168.10.10:27017

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s