Web development

How to add parameters or variables to a BackboneJS view

It’s very easy to add parameters or variables to any BackboneJS view but being aware of what BackboneJS version are you using because it changed. Let’s see:

Before 1.1.0 version

Any parameter in the constructors went to the options variable of the view::

Controller

var userView = new UserView({
  mode: "add"
})

View

render: function() {
    // Use example in any function
    var mode = this.options.mode; // mode="add"
}

Now (version >= 1.1.0)

Just like above but adding this to the Controller:

initialize : function (options) {
  this.options = options || {};
}

You can check the BackboneJS changelog

Standard
Dev Ops

How to install or upgrade NodeJS to any version

expressnodejs

When you work in a few NodeJS projects, you may need an easy and fast way to install, switch or upgrade the NodeJS version. With NVM is possible.

Install

curl https://raw.github.com/creationix/nvm/master/install.sh | sh

Usage

Install a NodeJS version

nvm install 0.10.26

Switch or upgrade

nvm use 0.10.26

Use by default

nvm alias default 0.10.26

List the versions installed

nvm ls

By the way, if you get nvm: command not found check this solution

More info about NVM

Standard
Databases

Lost connection to Mysql server at ‘reading initial communication packet’, system error:0

mariadb-logo

Lost connection to Mysql server at ‘reading initial communication packet’, system error:0

This error appears when you try to connect to a MySQL or MariaDB database and it is unavailable. The fix depends on your situation:

If you have MySQL/MariaDB installed on the server where you are trying to connect

  • Edit the configuration file located on /etc/mysql/my.cnf
  • Set:
    bind_adress = 0.0.0.0
  • Restart:
    service mysql restart

If the server is just a proxy, like a HAProxy server:

  • Remember to grant access permissions on all database servers, check my guide about it on how to deploy a MariaDb Cluster:
    INSERT INTO USER (Host,USER) VALUES ('11.11.11.11','haproxy');
    FLUSH PRIVILEGES;

    Repeat it for each db server, changing IP and proxy user.

Standard
Databases

Host ‘host_name’ is blocked because of many connection errors. Unblock with ‘mysqladmin flush-hosts

mariadb-logo

Host ‘host_name’ is blocked because of many connection errors. Unblock with ‘mysqladmin flush-hosts

I got this error message when I was setting up a MariaDb Cluster and I don’t finish it properly. But don’t worry, for fix this error just run the next command on your database server:

mysqladmin flush-hosts -u root -p

Source http://cmanios.wordpress.com/2013/04/19/mysql-connect-error-1129/

Standard
Databases

How to setup the load balancer HAProxy in front of MariaDb Cluster

mariadb-logo

 

In my previous post I showed you How to deploy a MariaDb Cluster and now let’s see how to setup HAProxy in front of the cluster. But before, we should prepare the nodes.

Preparing the cluster

On each node, login to the database:

mysql -u root -p

Enter your password and press enter.

Now create the HAProxy user. The load balancer will use this user in order to check the availability of the nodes.

  • NODE 1 – Example IP 11.11.11.11
INSERT INTO USER (Host,USER) VALUES ('11.11.11.11','haproxy');
FLUSH PRIVILEGES;
  • NODE 2 – Example IP 22.22.22.22
INSERT INTO USER (Host,USER) VALUES ('22.22.22.22','haproxy');
FLUSH PRIVILEGES;
  • NODE 3 – Example IP 33.33.33.33
INSERT INTO USER (Host,USER) VALUES ('33.33.33.33','haproxy');
FLUSH PRIVILEGES;

 Setting up HAProxy

Installing HAProxy

Now install the packages on the load balancer server

sudo apt-get install mysql-client haproxy

Configuring HAProxy

Create and edit the configuration file:

sudo nano /etc/haproxy/haproxy.cfg

Paste a full example:

global
    log 127.0.0.1 local0
    log 127.0.0.1 local1 notice
    maxconn 1024
    user haproxy
    group haproxy
    daemon
defaults
    log global
    mode http
    option tcplog
    option dontlognull
    retries 3
    option redispatch
    timeout connect 5000
    timeout client 10000
    timeout server 10000
listen workerhaproxy 0.0.0.0:6000
    mode tcp
    balance source
    option tcpka
    option mysql-check user haproxy
    server db1 11.11.11.11:3306 check weight 1
    server db2 22.22.22.22:3306 check weight 1
    server db3 33.33.33.33:3306 check weight 1

Some notes on this example:

  • Remember to change your databases IP.
  • HAProxy will listeting on 6000 port.
  • Uses source balance option, what means each user will try to connect to the same database server. There are more options, like round-robin.
  • Please check the documentation in order to understand the configuration.

Enable and start HAProxy

Edit the file /etc/default/haproxy

sudo nano /etc/default/haproxy

Change ENABLED to 1, save and exit.

Now start HAProxy

service haproxy start

 Testing

Now in your app or database client, test it connecting to the HAPRoxy server, on port 6000.

You can use root user or whatever user you had created on database.

Sources

http://cbonte.github.io/haproxy-dconv/configuration-1.4.html

http://www.sebastien-han.fr/blog/2012/04/08/mysql-galera-cluster-with-haproxy/

https://www.digitalocean.com/community/articles/how-to-use-haproxy-to-set-up-mysql-load-balancing–3

Standard