Dedunu

  • Technology
  • Travel
  • Retrieving data from MongoDB database using PHP

    Previous post I wrote about how to install MongoDB driver on PHP when we are using windows. In Linux (I mean Ubuntu) there were no issues on installing that. So after that, I want to connect to MongoDB to take data.

    <!DOCTYPE html>
    <html>
        <head>
            <title>MongoDB Connection</title>
        </head>
        <body>
            <?php
            $m = new MongoClient();
           
            $db = $m->phpdb;
           
            $col = $db->pageind;
           
            $rs = $col->find();
           
            foreach ($rs as $rec)
            {
                echo “<a href=’” . $rec[‘url’] . “‘>” . $rec[‘name’] . “</a><br />”;
            }
                ?>
        </body>
    </html>

    Below Code part, I inserted between PHP document. This MongoDB server is not running in auth mode. If your MongoDB is running in auth mode and in different ports, you can specify them with the connection string. And my database structure was like this.
    Database : phpdb
    Collection : pageind
    Sample document in a database was like below.

    {
    _id : 1,
    name : “Dedunu Blog”,
    url : “http://www.dedunu.info/”
    }

    nn

    Tags

    nn

      n

    • MongoDB
    • n

    • php
    • n

    n

  • PHP MongoDB Driver not working!

    In Semester 3 I have PHP unfortunately not MongoDB. So I was doing PHP practicals while I was doing my office things. So suddenly MongoDB came to my mind. Then I tried to connect to MongoDB with PHP. Although I have already tried with .NET. I never tried MongoDB with PHP. So I installed IIS 8.0 and PHP 5.3.1. 
    Then I downloaded the driver from github. Extracted it to C:\Program Files (x86)\PHP\v5.3\ext\. 
    I added the entry to php.ini properly. Then I restarted IIS using iisreset. But MongoDB Driver for PHP was not loaded properly. Then I was checking and doing so many things but nothing worked.
    Try this way!
    1. Download driver from github.
    2. Extract the zip file.
    3. Copy php_mongo-1.3.2RC1-5.3-vc9-nts.dll to C:\Program Files (x86)\PHP\v5.3\ext\. And rename it to php_mongo.dll.
    4. Then take properties of php_mongo.dll and unblock executable.

    5. Then go to C:\Program Files (x86)\PHP\v5.3\ and open php.ini as Administrator and add below line to end of the file.

    extension=php_mongo.dll

    6. Then take a command prompt as administrator and run below command.

    iisreset

    Enjoy MongoDB with PHP!

    nn

    Tags

    nn

      n

    • MongoDB
    • n

    • mongodb driver
    • n

    • iis
    • n

    • mongodb php driver
    • n

    • php
    • n

    n

  • Start Here! Learn HTML5

    Another good book for starter from Oreilly. If you don’t know how to save an HTML file with your notepad. You can learn it from this book too. And gradually the author moves to advanced topics. Also, this book contains CSS.
    The author doesn’t use confusable terminology. So it’s really cool for novice people. I got addicted when reading the first chapter. At the last chapter, he introduces Microsoft Expression Web. It is also again a nice tool to develop web pages. And you can learn best practices of HTML from this book too. For example, read below extract.

    The nonbreaking space entity &nbsp; is very popular for creating spaces, and in fact,
    many WYSIWYG web site creation programs like Microsoft Expression Web and Adobe
    Dreamweaver insert them for you when you press the spacebar. Don’t use nonbreaking
    spaces instead of good layout techniques, though. For example, if something needs to be
    indented a certain amount, use the correct HTML tags and styles for indenting it, don’t just
    “space over” with a half-dozen &nbsp; codes.

    Those points are emphasized. So this book is a nice book to start your HTML life. There are three appendixes which help to learn about ‘Designing for Usability’, ‘Designing for Accessibility’ and cool Quick reference. A quick reference was something remarkable to me. It has only two different sections which contain what is new in HTML5 and what are the thing getting deprecated in HTML5. It’s a smart appendix.
    I recommend this book for beginners who wish to begin learning HTML or HTML5 both. Name of this book deserves it 100%!!!
    You can buy it from here.

    nn

    Tags

    nn

      n

    • Microsoft Press
    • n

    • HTML5
    • n

    n

  • How to setup a replication set in MongoDB?

    I just wanted to set up a replication set in MongoDB to check the behaviour of MongoDB replica set.
    For this demonstration, I use MongoDB 2.2.0 and PowerShell 2.0.
    First I want to create directories for MongoDB replication instances. I’m going to for a replication set with three nodes. So because of them, I need to create there MongoDB data directories.

    New-Item -ItemType Directory -Path \data\
    New-Item -ItemType Directory -Path \data\repDb1
    New-Item -ItemType Directory -Path \data\repDb2
    New-Item -ItemType Directory -Path \data\repDb3
    New-Item -ItemType Directory -Path \log

    Now copy MongoDB Binaries to the drive that you ran above PowerShell commands. Change directory to MongoDB bin. And create three separate configuration files in MongoDB bin folder with notepad or any other plain text editor.

    • config1.cnf
    • config2.cnf
    • config3.cnf

    Create config1.cnf file with below content.

    logpath=d:\log\log1.txt
    dbpath=d:\data\repDb1
    port=9991
    replSet=samplecluster
    rest=true

    Create config2.cnf file with below content.

    logpath=d:\log\log2.txt
    dbpath=d:\data\repDb2
    port=9992
    replSet=samplecluster
    rest=true

    Create config3.cnf file with below content.

    logpath=d:\log\log3.txt
    dbpath=d:\data\repDb3
    port=9993
    replSet=samplecluster
    rest=true

    Now we are going to run MongoDB. For that you must go to MongoDB bin folder. And Press Shift and while you pressing shift right click on the free area. And select ‘Open command window here’

    Now you will get a Command Prompt. Type below three commands on your command prompt.

    start mongod –-config config1.cnf
    start mongod –-config config2.cnf
    start mongod –-config config3.cnf

    Now you should log in to one of those MongoDB instances to initiate the MongoDB replication set. For that type below command.

    
    
    mongo localhost:9991

    Now you have a MongoDB shell and you can initiate replication set. Type below command to initiate the MongoDB replication set.

    
    
    rs.initiate()

    This command will take some time. Don’t worry most of the times it’s not stuck. Then you have to add other instances to this MongoDB replication set.

    
    
    rs.add(“localhost:9992”);
    rs.add(“localhost:9993”);

    This will return { “ok” : 1 }. If it is not please replace your machine name to “localhost”. Now it is done. Your MongoDB replication is up and running!!!

    nn

    Tags

    nn

      n

    • MongoDB
    • n

    • MongoShell
    • n

    • Mongo Shell
    • n

    • MongoDB Replication
    • n

    • MongoDB Install
    • n

    n

  • DISTINCT and ORDER BY together

    When we are using DISTINCT we can’t use ORDER BY as we want. If you are going to use DISTINCT when you have ORDER BY, all ORDER BY attributes should be in the SELECT phrase.
    USE AdventureWorks2012;
    — Works fineSELECT BusinessEntityIDFROM HumanResources.EmployeeORDER BY BirthDate;
    — FailsSELECT DISTINCTBusinessEntityIDFROM HumanResources.EmployeeORDER BY BirthDate;
    — If you are going to sort with distinct
     result should consist attribute that you are sortingSELECT DISTINCT BusinessEntityIDFROM HumanResources.EmployeeORDER BY BusinessEntityID;

    Otherwise, it fails with the following error.
    ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

    nn

    Tags

    nn

      n

    • SQL
    • n

    • SQL Server 2012
    • n

    • TSQL
    • n

    • SQL Server
    • n

    • SQL Sever
    • n

    n

  • Training Kit (Exam 70-461) Querying Microsoft SQL Server 2012

    I wrote about Programming SQL Server 2012 book before few months. But that book was not for beginners, it was a little bit advanced and not easy to catch the things from scratch. But Querying Microsoft SQL Server 2012 book is a good book for a beginner.
    I was waiting for this book’s release. Because I wanted to know the things that cover the 70-461 exam. Authors cover important points and highlight them. This kit also consists of training resources such as questions. It helps you to face the exam without any fear.
    Every lesson comes with a new thing to me. And the organization of this book is perfect. This book covers all the fundamentals in T-SQL. For novice DevDBA’s this would be the best book to know the capabilities of T-SQL. This book also covers new features best practices as well as bad practices.
    Exam 70-461 is essential exam if you are looking for Microsoft SQL Server New Certifications. This book will definitely help you to pass this exam. And this covers standard methods of coding in SQL and TSQL both. In the first chapter, Authors discuss Relational Theories very well. And they point out the misconceptions about relational database management system.
    I call it a complete Kit of learning TSQL. But this book is not for experienced DBAs or Developers. You can buy this book from here.

    nn

    Tags

    nn

      n

    • SQL
    • n

    • SQL Server 2012
    • n

    • DBA
    • n

    • 70-461
    • n

    • T-SQL
    • n

    • TSQL
    • n

    • SQL Server
    • n

    • microsoft
    • n

    n

  • Ethics of Big Data

    nn

    Everybody talks about how to handle big data? and What are the technologies we are going to go with? What are the technologies that we are going to drop when we move towards the big data? What are the concepts that we put away when we move to big data? I even had such problems in my mind. And I had asked them from some guys even. But I never thought what is the ethical background we need with big data. 

    nn

    What are the disciplines that we need when we are moving forward to big data? This book reveals an aspect that technical guys usually don’t think a lot much. Nobody knows who use our data on social sites like Facebook. This book is written with so many references. Basically with proofs. It is more like an encyclopedia on the Ethics of Big Data. But to read and understand this book you need some experience with Information Technology.

    nn

    In the perspective of business, this book is very important. Because this book says how to make data more trustworthy to deliver to the business. You will have just only a few pages. But ethics are compressed within those pages. If you like big data you should read this book!

    nn

    Tags

    nn

      n

    • Big Data
    • n

    • Ethics of Big Data
    • n

    n

  • Titan with Cassandra

    In a previous blog post about Titan, I mentioned about Titan supports three different storage back ends. For this, you need Titan and Cassandra both.

    • Download Cassandra Here
    • Download Titan Here

    Then extract Cassandra and Titan both into a folder and take a terminal or a command prompt. Go to Cassandra bin and run cassandra.bat or Cassandra. Before this, you have to set JAVA_HOME.

    Linux:

    bash cassandra

    Windows:

    cassandra.bat

    Then take gremlin from titan bin.

    Linux:

    bash gremlin.sh

    Windows:

    gremlin.bat

    Then you have to configure storage backend. for that use following commands on gremlin.

    cnf = new BaseConfiguration();
    cnf.setProperty(‘storage.backend’,’cassandra’);
    cnf.setProperty(‘storage.hostname’,’127.0.0.1′);
    g = TitanFactory.open(cnf);

    Now rest is your graph database experience!!!

    nn

    Tags

    nn

      n

    • Titan DB
    • n

    • Configure Storage Backend
    • n

    • Titan and Cassandra
    • n

    • Graph DB
    • n

    • Cassandra
    • n

    • Graph database
    • n

    • Titan
    • n

    n

  • Getting Started with Titan Graph Database

    I have used Neo4J as my first graph database. And it had web console which makes easier to handle. But this time I’m not going to tell you how to get started with Neo4J. This time we are going to work with Titan which is an open source project which is held by Aurelius.

    nn

    This database is more scalable graph database. Let’s get to know how to get started with Titan Graph Database.

    nn

    First download Titan from Titan Site
    nClick Here to download Titan Graph Database

    nn

    Then extract titan to a disk. In this example, I take D drive. If you are Linux user extract this Titan file to somewhere you can execute.

    nn

    Then take a shell in Linux or command prompt in Windows. Then move to the titan folder. Then move to the bin.

    nn

    Before taking Gremlin Console you need to install Java.

    nn

    After that, if you are Linux guy run this command.

    nn

    $ bash gremlin.shn

    nn

    Or if you are windows user, your command is,

    nn

    $ gremlin.batn

    nn

    Now you have the gremlin shell. Now we are going to connect to the graph database. Basically, we can use it with its three main storage backends. They are

    nn

      n

    • Cassandra
    • n

    • HBase
    • n

    • Oracle Berkley 
    • n

    nn

    But I’m not going to use any of them now. Now I’m just going to use Titans Native Storage.

    nn

    g = TitanFactory.open('local/tmp');n

    nn

     Below command will initiate a graph and assign it to g variable. Now in your bin, you can see local and temp folders are created.

    nn

    If you hope to search through your graph in Titan you have to index it first. Otherwise, you can’t search for it. Let say we are going to search with “name” property. Then we have to add “name” index first.

    nn

    g.createKeyIndex('name', Vertex.class);n

    nn

    Now you are ready to add nodes and edges. And you have an index on “name” property.

    nn

    v = g.addVertex(null);n

    nn

    This will create a new node on the graph. we are going to set properties to this node.

    nn

    v.setProperty('name','dedunu');  nv.setProperty('type','person');  nv.setProperty('age',20);nnv1 = g.addVertex(null);nv1.setProperty('name','malinda');nv1.setProperty('type','person');nv1.setProperty('age',22);nnv2 = g.addVertex(null);nv2.setProperty('name','UCSC');nv2.setProperty('type','institute');  n

    nn

    Now we have three nodes. Two of them are Persons and the other one is an institute.  Now we are going to create relationships between those three nodes.

    nn

    e1 = g.addEdge(null, v, v2, 'study in');  ne2 = g.addEdge(null, v1, v2, 'study in');  ne3 = g.addEdge(null, v, v1, 'knows');  ne4 = g.addEdge(null, v1, v, 'knows');n

    nn

    Now you have a graph like this.

    nn

    nn

     Now we are going to have a journey around our beautiful data!!! And now I want to know where Malinda “study in”. For that, I have to load Malinda to a vertex variable. I’ll use an existing one.

    nn

    v1.out('study in').map();n

    nn

    Now I want who studies at UCSC.  For that, I have to load UCSC to a variable first. I’ll use an existing one.

    nn

    v2.in('study in').map();n

    nn

    If I only need names of them then below one is the command.

    nn

    v2.in('study in').name;n

    nn

    Finally to commit all those things to disk you have to shut down the graph for that run below command.

    nn

    g.shutdown();n

    nn

    Be careful those commands are case sensitive!!! Enjoy Graphs!!!

    nn

    Tags

    nn

      n

    • Titan DB
    • n

    • Titan with Gremlin
    • n

    • Tinker Pop
    • n

    • Graph DB
    • n

    • How to begin titan db
    • n

    • Graph database
    • n

    • Gremlin
    • n

    • Titan
    • n

    n

  • How to shutdown MongoDB instance from MongoDB Shell?

    For some cases, we may have access to database instance of MongoDB as DBA. But we may not have access to Linux or Windows box to shutdown the MongoDB service or MongoDB instance. To Shutdown the MongoDB instance you should be able to log in to “admin” database.  If your MongoDB instance is running with “auth” mode you really need a password and username. In this demo, I assume MongoDB instance is not running with “auth” mode.

    I log in to MongoDB shell first.

    mongo

    Then I change the database to the admin database.

    use admin

    Now I’m going to run that command. This command will shutdown the instance hence think twice before you use it.

    db.runCommand( { shutdown : 1 } );

    nn

    Tags

    nn

      n

    • MongoDB
    • n

    • mongo
    • n

    • MongoDB Shell
    • n

    • MongoShell
    • n

    • Shutdown MongoDB
    • n

    • database instance
    • n

    • Stop MongoDB from Shell
    • n

    n

←Previous Page
1 … 12 13 14 15 16 17
Next Page→
 

Loading Comments...