Chris IJ Hwang

I am a Quantitative Analyst/Developer and Data Scientist with backgroud of Finance, Education, and IT industry. This site contains some exercises, projects, and studies that I have worked on. If you have any questions, feel free to contact me at ih138 at columbia dot edu.

View My GitHub Profile



Contents

Data Model

Normalized vs Embeded Data Models

MongoGridFS

To MongoDB

Example

$m = new MongoClient("mongodb://ipaddress");
$db = $m->mydb;

$grid = $db->getGridFS();

$name = $_FILES['file_upload']['name']; // the name of the uploades file
$id = $grid->storeUpload('Filedata', $name);

Example


<?php
if(isset($_POST['submit'])) {
    $m = new MongoClient("mongodb://ipaddress");
    $gridfs = $m->selectDB('test')->getGridFS();

    $input =  array('sql_id' => 143294 , 'question_id' => 900, 'filetype'=> 'image');
    $gridfs->storeUpload('pic', $input);
}
?>

<form method="POST" enctype="multipart/form-data" action="test.php">
    <label for="username">Username:</label>
    <input type="text" name="username" id="username" />

    <label for="pic">Please upload a profile picture:</label>
    <input type="file" name="pic" id="pic" />

    <input type="submit" name="submit" />
</form>

Result at MongoDB

This makes normalized data which can be accessed by the additional information put when uploading.

From MongoDB

Authentication

http://docs.mongodb.org/master/MongoDB-security-guide.pdf

  1. Start without authentication.
    $ sudo service mongod start
    Check if it started correctly.
    2015-07-27T21:12:20.253+0000 D INDEX    [initandlisten] checking complete
    2015-07-27T21:12:20.253+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
    2015-07-27T21:12:20.253+0000 D COMMAND  [PeriodicTaskRunner] BackgroundJob starting: PeriodicTaskRunner
    2015-07-27T21:12:20.253+0000 D COMMAND  [ClientCursorMonitor] BackgroundJob starting: ClientCursorMonitor
    2015-07-27T21:12:20.253+0000 D COMMAND  [TTLMonitor] BackgroundJob starting: TTLMonitor
    2015-07-27T21:13:20.227+0000 I STORAGE  [DataFileSync] flushing mmaps took 0ms  for 8 files
  2. Create Adminisgtrative user first. (initially under localexception) The following operations will create two users: a user administrator that will be able to create and modify users (siteUserAdmin), and a root user (siteRootAdmin) that you will use to complete the remainder of the tutorial
    mongo
    > use admin
    > db.createUser( {
        user: "siteUserAdmin",
        pwd: "password",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
      });
      Successfully added user: {
        "user" : "siteUserAdmin",
        "roles" : [
            {
                "role" : "userAdminAnyDatabase",
                "db" : "admin"
            }
        ]
    }
    
    db.createUser( {
        user: "siteRootAdmin",
        pwd: "password",
        roles: [ { role: "root", db: "admin" } ]
    });
    Successfully added user: {
        "user" : "siteRootAdmin",
        "roles" : [
            {
                "role" : "root",
                "db" : "admin"
            }
        ]
    }
    
  3. stop the mongod instance
  4. Create the key file to be used by each member of the replica set. To generate pseudo-random data to use for a keyfile, issue the following openssl command:
    openssl rand -base64 741 > mongodb-keyfile
    chmod 600 mongodb-keyfile
    You may generate a key file using any method you choose. Always ensure that the password stored in the key file is both long and contains a high amount of entropy. Using openssl in this manner helps generate such a key.
  5. Start the mongoDB with security key: First, edit /etc/mongod.conf.
    # path to a key file storing authentication info for connections
    # between replica set members
    keyFile=/home/ubuntu/.ssh/mongodb-keyfile
    
    $ sudo mongod --config /etc/mongod.conf &
  6. Check if it started correctly.
     I CONTROL  [initandlisten] allocator: tcmalloc
     I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", security: { keyFile: "/home/ubuntu/.ssh/mongodb-keyfile" }, storage: { dbPath: "/var/lib/mongodb" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
     I NETWORK  [initandlisten] waiting for connections on port 27017
  7. Create user for specific database.
        
  8. connect to mongo shell
    $ mongo --port 27017 -u siteUserAdmin -p password --authenticationDatabase admin
  9. use nota db.createUser( { user: "jinna", pwd: "password", roles: [ {role: "readWrite", db: "nota"} ]} )