How to Setup MongoDB Replication Using Replica Set and Arbiters

How to Setup MongoDB Replication Using Replica Set and Arbiters

If you are running MongoDB on production environment it is essential that you setup a real time replication of your primary instance.

Using replica set, you can also scale horizontally and distribute the read load across multiple mongodb nodes.

This tutorial explains in detail on how to setup MongoDB replication.

You can setup mongodb replication in several configurations. But we’ll discuss the minimum configuration setup.

MongoDB recommends that you have minimum of three nodes in a replica set. But, out of those three nodes, two nodes stores data and one node can be just an arbiter node.

An arbiter node doesn’t hold any data, but it participates in the voting process when the primary goes down.

The following is the minimal setup that is explained in this tutorial.

MongoDB Replica Set

1. Install MongoDB

First, install mongodb on all three nodes.

yum install mongo-10gen mongo-10gen-server

2. Modify /etc/hosts file

On mongodb1 (and mongodb2) server, modify the /etc/hosts file and add the following.

192.168.100.1 mongodb1
192.168.100.2 mongodb2
192.168.100.3 arbiter1

Make sure all these nodes are able to talk to each other using the hostname. You can also use FQDN here. For example, instead of mongodb1, you can use mongodb1.thegeekstuff.com

3. Enable Auth on all MongoDB Nodes

While this is not mandatory for the replica set functionality, don’t run your prod mongodb instance without authentication enabled.

By default auth is not enabled on mongodb. Add the following line to your mongod.conf file.

# vi /etc/mongod.conf
auth = true

Restart the mongodb instance after the above change.

service mongod restart

Also make sure you create an admin username and password for the admin database to use replica set commands.

> use admin
switched to db admin
> db.addUser("admin", "SecretPwd");

Note: Do the above on all the mongodb nodes.

4. On mongodb1: Restore Existing DB

If you already have a single instance mongodb server running, and like to migrate it to this new replica set configuration using the three nodes, take a backup of the mongodb using mongodump, and restore it on the mongodb1 instance usingmongorestore command.

mongorestore --dbpath /var/lib/mongo --db ${db_destination} --drop dump/${db_source}

After the restore, if the file permissions under /var/lib/mongo directory are different, change it accordingly as shown below.

cd /var/lib/gmongo
chown mongod:mongod *
service mongod start

5. On mongodb1: Add replSet to mongod.conf

Add the following line to mongod.conf file. You can give any value here. I’ve given “prodRepl”, but it can be anything, as long as they are same on all the nodes.

# vi /etc/mongod.conf
replSet = prodRepl

At this stage, you’ll notice that there is no replica set configuration. It will display “null” as shown below.

> use admin;
> db.auth("admin","SecretPwd");
> rs.conf();
null

6. Setup KeyFile for Replication Auth on all MongoDB Nodes

On all the mongodb nodes, create a keyfile with some random password. The main thing is that this password should be same across all mongodb nodes.

# mkdir /root/data/mongodb/
# vi /root/data/keyfile
SecretPwdReplicaSetMongoDB

Add the following line to mongod.conf file on all the nodes

# vi /etc/mongod.conf
keyFile = /root/data/keyfile

Setup appropriate permissions to the keyfile and restart the mongodb instance as shown below.

chown mongod:mongod /root/data/keyfile
chmod 700 /srv/mongodb/keyfile
service mongod restart

7. On mongodb1: Initiate the Replica Set

Now, it is time to initiate the replica set as shown below using the rs.initiate() command.

> use admin
> db.auth("admin","SecretPwd");
> rs.initiate();
{
  "info2" : "no configuration explicitly specified -- making one",
  "me" : "mongodb1:27017",
  "info" : "Config now saved locally.  Should come online in about a minute.",
  "ok" : 1
}

Right after the initiate, you’ll notice that the configuration is not null anymore. Also, you’ll notice that the mongodb prompt changed from “>” to “replicasetName:PRIMARY>” as shown below.

> rs.config();
{
  "_id" : "prodRepl",
  "version" : 1,
  "members" : [
     {
       "_id" : 0,
       "host" : "mongodb1:27017"
     }
  ]
}

8. On mongodb1: View Log and Replica Set Status

At this stage, on mongodb1, mongod.log will display something similar to the following:

# tail /var/log/mongo/mongod.log
Sat Feb 22 18:11:30.995 [conn2] ******
Sat Feb 22 18:11:30.995 [conn2] replSet info saving a newer config version to local.system.replset
Sat Feb 22 18:11:30.996 [conn2] replSet saveConfigLocally done
Sat Feb 22 18:11:30.996 [conn2] replSet replSetInitiate config now saved locally.  Should come online in about a minute.
Sat Feb 22 18:11:34.568 [rsStart] replSet I am mongodb1:27017
Sat Feb 22 18:11:34.569 [rsStart] replSet STARTUP2
Sat Feb 22 18:11:35.570 [rsSync] replSet SECONDARY
Sat Feb 22 18:11:35.570 [rsMgr] replSet info electSelf 0
Sat Feb 22 18:11:36.570 [rsMgr] replSet PRIMARY
...

Also, the status will display the following, indicating that there is only one node added to the replica set so far.

prodRepl:PRIMARY> rs.status();
{
"set" : "prodRepl",
"date" : ISODate("2014-02-22T06:28:49Z"),
"myState" : 1,
"members" : [
  {
    "_id" : 0,
    "name" : "mongodb1:27017",
    "health" : 1,
    "state" : 1,
    "stateStr" : "PRIMARY",
    "uptime" : 645,
    "optime" : Timestamp(1438853170, 1),
    "optimeDate" : ISODate("2014-02-22T06:19:30Z"),
    "self" : true
  }
],
"ok" : 1
}

9. On mongodb1: Add the 2nd node

On mongodb1 server, add the 2nd node using rs.add command as shown below.

prodRepl:PRIMARY> rs.add("mongodb2");
{ "ok" : 1 }

Afer you’ve added the node, you’ll notice that the rs.config command will show both the nodes as shown below.

prodRepl:PRIMARY> rs.config();
{
  "_id" : "prodRepl",
  "version" : 2,
  "members" : [
     {
       "_id" : 0,
       "host" : "mongodb1:27017"
     },
     {
       "_id" : 1,
       "host" : "mongodb2:27017"
     }
  ]
}

10. Sync started Between Nodes

As shown from the rs.status() command, you’ll notice that the mongodb2 node is in startup state, and it is performing the initial cloning of the database from mongodb1 to mongodb2.

Depending on the size of the database on mongodb1, this might take some time to complete.

prodRepl:PRIMARY> rs.status();
{
  "set" : "prodRepl",
  "date" : ISODate("2014-02-22T21:27:53Z"),
  "myState" : 1,
  "members" : [
   {
     "_id" : 0,
     "name" : "mongodb1:27017",
     "health" : 1,
     "state" : 1,
     "stateStr" : "PRIMARY",
     "uptime" : 225,
     "optime" : Timestamp(1343239634, 1),
     "optimeDate" : ISODate("2014-02-22T21:27:14Z"),
     "self" : true
   },
   {
     "_id" : 1,
     "name" : "mongodb2:27017",
     "health" : 1,
     "state" : 5,
     "stateStr" : "STARTUP2",
     "uptime" : 39,
     "optime" : Timestamp(0, 0),
     "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
     "lastHeartbeat" : ISODate("2014-02-22T21:27:52Z"),
     "lastHeartbeatRecv" : ISODate("2014-02-22T21:27:52Z"),
     "pingMs" : 2,
     "lastHeartbeatMessage" : "initial sync cloning db: mongoprod"
   }
  ],
  "ok" : 1
}

Once the sync is completed, the state on the mongodb1 will change to “SECONDARY” and you’ll see the last heartbeat message indicating that the data is all synced and ready to go.

prodRepl:PRIMARY> rs.status();
{
  "set" : "prodRepl",
  "date" : ISODate("2014-02-22T22:03:21Z"),
  "myState" : 1,
  "members" : [
    {
      "_id" : 0,
      "name" : "mongodb1:27017",
      "health" : 1,
      "state" : 1,
      "stateStr" : "PRIMARY",
      "uptime" : 2353,
      "optime" : Timestamp(1394309634, 1),
      "optimeDate" : ISODate("2014-02-22T21:27:14Z"),
      "self" : true
    },
    {
      "_id" : 1,
      "name" : "mongodb2:27017",
      "health" : 1,
      "state" : 2,
      "stateStr" : "SECONDARY",
      "uptime" : 2167,
      "optime" : Timestamp(1394309634, 1),
      "optimeDate" : ISODate("2014-02-22T21:27:14Z"),
      "lastHeartbeat" : ISODate("2014-02-22T22:03:21Z"),
      "lastHeartbeatRecv" : ISODate("2014-02-22T22:03:20Z"),
      "pingMs" : 0,
      "syncingTo" : "mongodb1:27017"
    }
],
"ok" : 1
}

Note: If you login to mongodb2 node, and execute the above command, you’ll see the exact same message, as both the nodes are now part of the same replica set.

11. On arbiter1: Start the mongod instance

The arbiter node doesn’t need to be as powerful as the mongodb1 and mongodb2 nodes. You can pick some existing server that is running some other production application, and run arbiter on it, as it doesn’t consume lot of resources.

Create an empty directory for the mongod instance dbpath. As we explained earlier, this directory will not contain any data from mongodb1 or mongodb2 server. This is needed just to start the mongodb instance on the arbiter node.

The key thing is that specify the replica set name using –replSet as shown below. If you like to change the default mongodb port, you can use –port option as shown below. Run this in the background.

mkdir /var/lib/mongo/data

nohup mongod --port 30000 --dbpath /var/lib/mongo/data --replSet prodRepl &

12. On mongodb1: Add arbiter1 node

Now on the mongodb1 primary node, add this arbiter node using rs.addArb command as shown below.

prodRepl:PRIMARY> rs.addArb("arbiter1:30000");
{ "ok" : 1 }

Now if you view the configuration using rs.config command, you’ll see all three nodes.

prodRepl:PRIMARY> rs.config();
{
  "_id" : "prodRepl",
  "version" : 3,
  "members" : [
    {
      "_id" : 0,
      "host" : "mongodb1:27017"
    },
    {
      "_id" : 1,
      "host" : "mongodb2:27017"
    },
    {
      "_id" : 2,
      "host" : "arbiter1:30000",
      "arbiterOnly" : true
    }
  ]
}

13. Verify Final Replication Status

Execute the rs.status() command which will show the current status of all the three nodes. In this case, everything looks good and functional.

As indicated by the “stateStr”, we now have a Primary, Secondary and an Arbiter. This is the minimum configuration required to get an working mongodb replica set.

Please note that you can execute rs.config() and rs.status() command on any one of the three nodes, which will display the same results.

prodRepl:PRIMARY> rs.status();
{
  "set" : "prodRepl",
  "date" : ISODate("2014-02-22T22:59:15Z"),
  "myState" : 1,
  "members" : [
     {
       "_id" : 0,
       "name" : "mongodb1:27017",
       "health" : 1,
       "state" : 1,
       "stateStr" : "PRIMARY",
       "uptime" : 5707,
       "optime" : Timestamp(1343042482, 1),
       "optimeDate" : ISODate("2014-02-22T22:14:42Z"),
       "self" : true
     },
     {
       "_id" : 1,
       "name" : "mongodb2:27017",
       "health" : 1,
       "state" : 2,
       "stateStr" : "SECONDARY",
       "uptime" : 5521,
       "optime" : Timestamp(1343042482, 1),
       "optimeDate" : ISODate("2014-02-22T22:14:42Z"),
       "lastHeartbeat" : ISODate("2014-02-22T22:59:14Z"),
       "lastHeartbeatRecv" : ISODate("2014-02-22T22:59:13Z"),
       "pingMs" : 0,
       "syncingTo" : "mongodb1:27017"
     },
     {
       "_id" : 2,
       "name" : "arbiter1:30000",
       "health" : 1,
       "state" : 7,
       "stateStr" : "ARBITER",
       "uptime" : 39,
       "lastHeartbeat" : ISODate("2014-02-22T22:59:14Z"),
       "lastHeartbeatRecv" : ISODate("2014-02-22T22:59:15Z"),
       "pingMs" : 0
     }
  ],
  "ok" : 1
}




Source :http://www.thegeekstuff.com/2014/02/mongodb-replication/

MongoDB PHP Driver

How to Install the MongoDB PHP Driver (Extension) on CentOS 6

Pre-Flight Check
  • These instructions are intended specifically for installing the MongoDB PHP Driver (Extension) on CentOS 6.
  • I’ll be working from a Liquid Web Core Managed CentOS 6.5 server, and I’ll be logged in as root.
Step 1: Setup Environment, Install PHP Extension & Application Repository (PEAR)

As a matter of best practice we’ll update our packages:

yum -y update

Then let’s install a compiler, the PHP Extension and Application Repository (PEAR), and PHP development tools:

yum -y install gcc php-pear php-devel

Step 2: Installation with the PHP Extension Community Library (PECL)

Now let’s install the MongoDB PHP driver (extension) with PECL:

pecl install mongo

Next you’ll be prompted regarding ‘MongoDB Enterprise Authentication’. If you’re not specifically using SASL (and already have it installed), then hit enter to continue (thus answering ‘no’):

Build with Cyrus SASL (MongoDB Enterprise Authentication) support? [no] :

Step 3:Configure PHP

For a refresher on editing files with vim see: New User Tutorial: Overview of the Vim Text Editor

vim /etc/php.ini

It is possible to place the following bit of code anywhere in the php.ini file. However, the most common location for extensions is in the Dynamic Extensions section.

Find Dynamic Extensions and insert the following in that section:

extension=mongo.so

Then exit and save the file with the command :wq .

And now we’ll restart Apache:

service httpd restart

Step 4: Verify the Extension is Available

Now verify that the extension is available by using the following command:

php -m | grep -i mongo

Source:http://www.liquidweb.com/kb/how-to-install-the-mongodb-php-driver-extension-on-centos-6/

PHP version 5.6 in Amazon Linux

Below are the steps I have used to update PHP version 5.6 in Amazon Linux

1) Remove OLD Apache

sudo service httpd stop
sudo yum erase httpd httpd-tools apr apr-util

2) Remove OLD PHP

sudo yum remove php-*

3) Install PHP 5.6 (Apache 2.4 will be automatically installed with this)

sudo yum install php56

4) Make sure all the required PHP extensions are installed

yum list installed | grep php

5) If not then install them using

sudo yum install php56-xml php56-xmlrpc php56-soap php56-gd

6) To list the other available php extensions

yum search php56

7) PHP 5.6 MySQL extension (Assume you have already installed MySQL)

sudo yum install php56-mysqlnd 

(NOTE: it is not php56-mysql)

8) Start / Restart Apache

sudo service httpd start
sudo service httpd restart

9) Check the version

php -v
httpd -v

Source from :stack overflow

Mongo DB on centos

cat > /etc/yum.repos.d/mongodb-org-3.2.repo <<EOF
[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7Server/mongodb-org/stable/x86_64/
gpgcheck=0
enabled=1
EOF

Now that our repo is defined, we can just install MongoDB with YUM:

yum install -y mongodb-org policycoreutils-python-2.2.5-20.el7.x86_64

By default, SELinux is not going to allow MongoDB to run on the default interface. So we need to adjust the policy for that. Thesemanage command is not installed on CentOS 7 Minimum by default, this is why we added the policycoreutils above.

semanage port -a -t mongod_port_t -p tcp 27017

You could turn SELinux off, of course, or set to permissive, but that is not advised. Correctly configuring SELinux is more secure.

Now we can start our database:

systemctl start mongod
systemctl enable mongod

If all has gone well, MongoDB will be running on port 27017. If this is going to be used for a local installation of an application, leave this port closed on the firewall. If you need to open the firewall for remote clients to attach we will open it below. Keep in mind that exposing MongoDB publicly will be a security nightmare, so consider proper precautions such as locking the port to being exposed to a single IP address or range.

firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --reload


Source :http://mangolassi.it/topic/8075/installing-mongodb-3-2-on-centos-7

2checkout payment method.

1.Initially let see by hard coded HTML form with laravel  (please don’t  use this method in production until changing them  to MVC )

 

-please create a sandbox account and make the relevant configuration such as return URL  based on the 2co documentation.

 

route.php

Route::get(‘co’, ‘HomeController@Twoco’);

Route::get(‘returnco’, ‘HomeController@return_co’);

 

 

view file -cart_test.blade.php

<form method=”post” action=”https://sandbox.2checkout.com/checkout/purchase&#8221; name=”process”>
<input type=”hidden” name=”sid” value=”901310973″ />
<!– 901269012 –>
<input type=”hidden” name=”mode” value=”2CO” />

<input type=”hidden” name=”total” value=”679.71″ />

<input type=”hidden” name=”merchant_order_id” value=”622″ />

<input type=”hidden” name=”cart_order_id” value=”622″ />

<input type=”hidden” name=”card_holder_name” value=”azeem jai” />

<input type=”hidden” name=”street_address” value=”No-90″ />

<input type=”hidden” name=”city” value=”New york” />

<input type=”hidden” name=”state” value=”NY” />

<input type=”hidden” name=”zip” value=”10031″ />

<input type=”hidden” name=”country” value=”US” />

<input type=”hidden” name=”email” value=”jazeem.lk@gmail.com” /><!– 901310973–>

<input type=”hidden” name=”phone” value=”” />

<input type=”hidden” name=”ship_name” value=”azeem jai” />

<input type=”hidden” name=”ship_street_address” value=”No-90″ />

<input type=”hidden” name=”ship_city” value=”New york” />

<input type=”hidden” name=”ship_state” value=”NY” />

<input type=”hidden” name=”ship_zip” value=”10031″ />

<input type=”hidden” name=”ship_country” value=”US” />

<input type=”hidden” name=”fixed” value=”Y” />

<input type=”hidden” name=”id_type” value=”1″ />

<input type=”hidden” name=”payment_method_id” value=”16″ />

<input type=”hidden” name=”demo” value=”N” />

<input type=”hidden” name=”dispatch” value=”payment_notification” />

<input type=”hidden” name=”payment” value=”2checkout” />

<input type=”hidden” name=”order_id” value=”6221″ />
<ul>
<li><label>Product -01 –> $220.00</label></li>
<li><label>Product -02 –> $158.00 </label></li>
<li><label>Shipping and hadling –> $71.71 </label></li>
</ul>
<input type=”hidden” name=”li_1_type” value=”product” ><input type=”hidden” name=”li_1_name” value=”Product-01″ />
<input type=”hidden” name=” li_1_product_id” value=”89″ />
<input type=”hidden” name=”li_1__description” value=”Product-01″ />
<input type=”hidden” name=”li_1_price” value=”220.00″ />
<input type=”hidden” name=”li_1_tangible” value=”Y” />
<input type=”hidden” name=”li_2_type” value=”product” ><input type=”hidden” name=”li_2_name” value=”Product-04″ />
<input type=”hidden” name=” li_2_product_id” value=”93″ />
<input type=”hidden” name=”li_2__description” value=”Product-02″ />
<input type=”hidden” name=”li_2_price” value=”158.00″ />
<input type=”hidden” name=”li_2_tangible” value=”Y” />
<input type=”hidden” name=”li_3_type” value=”shipping” >
<input type=”hidden” name=”li_3_name” value=”Shipping and handling” >
<input type=”hidden” name=”li_3_price” value=”71.71″ >
<input type=”hidden” name=”li_4_type” value=”tax” >
<input type=”hidden” name=”li_4_name” value=”Tax” >
<input type=”hidden” name=”li_4_price” value=”10″ >

<input type=”button” value=”2checkout” onclick=”submit();” >
</form>

function submit(){
document.process.submit();
};

 

add the following function into the Homecontroller 

function Twoco() {

return View::make(‘cart_test’);
}

 

please write a function for return URL and capture the values of returning string

 

 

 

 

2.MVC formated