My Sites


Friday, July 15, 2016

Mongo backup and authentication

mongodump -d myDatabase -o ~/backups/first_backup
$mongod --auth
db.createUser({user:"admin_name", pwd:"1234",roles:["readWrite","dbAdmin"]})

If you want to add without roles (optional):
db.createUser({user:"admin_name", pwd:"1234", roles:[]}) to check if authenticated or not:
db.auth("admin_name", "1234") it should give you:
1 else :
Error: Authentication failed. 0



mongo restore

mongorestore dump-2013-10-25/
https://docs.mongodb.com/manual/tutorial/backup-and-restore-tools/

install mongo
https://docs.mongodb.com/v3.0/tutorial/install-mongodb-on-amazon/


Everything you need to know about nvm

Install/ Update NVM
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh | bash
or
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh | bash

source ~/.bashrc
nvm ls-remote command results in “N/A”
export NVM_NODEJS_ORG_MIRROR=http://nodejs.org/dist

nvm ls-remote
nvm install 5.0
nvm use 5.0
nvm run 5.0 --version
nvm which 5.0

Saturday, July 2, 2016

Node.js Clustering - 4 Core = 4 Processes

var cluster = require('cluster');

if (cluster.isMaster) {
  // Count the machine's CPUs
  var cpuCount = require('os').cpus().length;

  // Create a worker for each CPU
  for (var i = 0; i < cpuCount; i += 1) {
    cluster.fork();
  }

  // Listen for dying workers
  cluster.on('exit', function () {
    cluster.fork();
  });
} else {
  require('./server');
}

Nginx - Proxy to Express Application, Load Balancer, Static Cache Files

sudo apt-get update
sudo apt-get install nginx
sudo service nginx start 
sudo service nginx stop
nano /etc/nginx/site_enabled/default

upstream project {
server 33.22.22.5:3000;
server 33.22.22.6:3000;
server 33.22.22.7:3000;
}
server {
listen 80;
location / {
proxy_pass http://project;
}
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 168h;
}
location /api {
expires 10m;
}
}

How add an automatic load to the server (Performance wise check)
Bench test for 1000 requests
ab -c 40 -n 1000 http://33.22.22.5/