Sunday, 22 February 2015

PERCONA STANDALONE SERVER

As a DevOps activist I am exploring Percona XtraDB. In a series of blogs I will share my learnings. This blog intends to capture step by step details of installation of Percona XtraDB in Standalone mode

Introduction:


Percona Server is an enhanced drop-in replacement for Mysql. It offers breakthrough performance, scalability, features, and instrumentation.
Percona focus on providing a solution for the most demanding applications, empowering users to get the best performance and lowest downtime possible.

The Percona XtraDB Storage Engine:

  • Percona XtraDB is an enhanced version of the InnoDB storage engine, designed to better scale on modern hardware, and including a variety of other features useful in high performance environments. It is fully backwards compatible, and so can be used as a drop-in replacement for standard InnoDB. 
  • Percona XtraDB includes all of InnoDB's robust, reliable ACID-compliant design and advanced MVCC architecture, and builds on that solid foundation with more features, more tunability. more metrics, and more scalability.
  • It is designed to scale better on many cores, to use memory more efficiently, and to be more convenient and useful.

      Installation on ubuntu:

      STEP 1: Add Percona Software Repositories
      $ apt-key adv --keyserver keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A
      STEP 2: Add this to /etc/apt/sources.list:
      deb http://repo.percona.com/apt precise main
      deb-src http://repo.percona.com/apt precise main
      STEP 3: Update the local cache
      $ apt-get update
      STEP 4: Install the server and client packages
      $ apt-get install percona-server-server-5.6 percona-server-client-5.6
      STEP 5: Start Percona Server
      $ service mysql start
      Let me know if you have any suggestions. You can also contact me at belwal.mohit@gmail.com.

      Understanding Percona XtraDB cluster

      As a DevOps activist I am exploring Percona XtraDB. In a series of blogs I will share my learnings. This blog intends to capture theoretical knowledge of Percona XtraDB Cluster.

      Prerequisites

      1. You should have basic knowledge of mysql. 
      2. OS - Ubuntu

      What is Percona?

      Percona XtraDB cluster is an open source, free MySql high availability and scalability software.
      It provides:
      1. Synchronous Replication: Transaction either committed on all nodes or none.
      2. Multi-Master Replication: You can write to any node
      3. Parallel applying events on slave. Real “parallel replication”.
      4. Automatic node provisioning.
      5. Data consistency. No more unsynchronized slaves.

      Introduction

      1. The cluster consists of nodes. The cluster’s recommended configuration is to have 3 nodes, however 2 nodes can be used as well.
      2. Every node is a regular Mysql / Percona server setup. You can convert your existing MySQL / Percona Server into Node and roll Cluster using it as a base or you can detach Node from Cluster and use it as a regular server.
      3. Each node will contain full copy of data.


      percona.jpeg.jpg

      Benefits of this approach:

      • Whenever you execute a query, it is executed locally. All data is available locally, so no remote access is required.
      • No central management. You can loose any node at any time, and cluster will continue functioning.
      • It is a good solution for scaling read workload. You can put read queries to any of the nodes.

      Drawbacks:

      • Overhead of joining new node. New node will copy all data from an existing node. If it is 100 GB, it will copy 100 GB.
      • Not an effective write scaling solution. All writes have to go on all nodes.
      • Duplication of data. If you have 3 nodes, there will be 3 duplicates.

      Difference between Percona XtraDB Cluster and MySQL Replication

      For this we will have to look into the well known CAP theorem for distributed systems. According to this theorem, characteristics of Distributed systems are:
      C - Consistency (all your data is consistent on all nodes),
      A - Availability (your system is AVAILABLE to handle requests in case of failure of one or several nodes),
      P - Partitioning tolerance (in case of inter-node connection failure, each node is still available to handle requests).
      CAP theorem says that any Distributed system can have any two out of these three.
      • MySQL replication has: Availability and Partitioning tolerance.
      • Percona XtraDB Cluster has: Consistency and Availability.
      So, MySql replication does not guarantee Consistency of data, while Percona XtraDB cluster provides consistency while it looses partitioning tolerance.

      Components 

      Percona XtraDb Cluster is based on:
      • Percona Server with XtraDB and includes Write Set Replication patches.
      It uses:
      • Galera Library: A generic synchronous Multi-Master replication plugin for transactional applications.
      • Galera supports:
        • Incremental State Transfer (IST), useful in WAN deployments.
        • RSU, Rolling Schema Update. Schema change does not block operations against table.

      Percona XtraDB cluster limitations

      • Currently replication work only with InnoDB storage engine.
      That means writes to table of other types, including (mysql.*) tables, are not replicated.
      DDL statements are replicated in statement level and changes to mysql.* tables will get replicated that way.
      So you can issue: CREATE USER …. , this will be replicated,
      but issuing: INSERT INTO mysql.user …. , will not be replicated.
      You can also enable experimental MyISAM replication support with wsrep_replicate_myisam.
      • Unsupported queries:
        • LOCK/UNLOCK tables
        • lock function (GET_LOCK(), RELEASE_LOCK()....)
      • Due to cluster level concurrency control, transaction issuing COMMIT may be aborted at that stage.
      There can be two transactions writing to same rows and committing in separate Percona XtraDB Cluster nodes, and only one of the them can successfully commit. The failing one will be aborted. For cluster level aborts, Percona will give back deadlock error code.
      • The write throughput of whole cluster is limited by weakest node. If one node becomes slow, whole cluster will become slow.

      FEATURES


      High Availability

      In a basic setup with 3 nodes, the Percona XtraDB cluster will continue to function if you take any of the nodes down. Even in a situation of node crash, or if node becomes unavailable over network, the cluster will continue to work, and queries can be issued on working nodes.
      In case, when there are changes in data while node was down, there are two options that Node may use when it joins the cluster:
      1. State Snapshot Transfer (SST): SST method performs full copy of data from one node to other. It’s used when a new node joins the cluster. One of the existing node will transfer data to it.
         There are three available methods of SST:
        • mysqldump
        • rsync
        • xtrabackup
      Downside of “mysqldump” and “rsync” is that your cluster becomes READ-ONLY while data is copied from one node to other.
      while
      xtrabackup SST does not require this for entire syncing process.
      1. Incremental State Transfer (IST): If a node is down for a short period of time, and then starts up, the node is able to fetch only those changes made during the period it was down.
      This is done using caching mechanism on nodes. Each node contains a cache, ring-buffer of last N changes, and the node is able to transfer part of this cache. IST can be done only if the amount of changes needed to transfer is less than N. If it exceeds N, then the joining node has to perform SST.


      Multi-Master Replication

      • Multi-Master replication stands for the ability to write to any node in the cluster, and not to worry that it will get out-of-sync situation, as it regularly happens with regular MySQL replication if you imprudently write to the wrong server.
      • With Percona XtraDB Cluster you can write to any node, and the Cluster guarantees consistency of writes. That is, the write is either committed on all the nodes or not committed at all.
      All queries are executed locally on the node, and there is a special handling only on COMMIT. When the COMMIT is issued, the transaction has to pass certification on all the nodes. If it does not pass, you will receive ERROR as a response on that query. After that, transaction is applied on the local node.

      Let me know if you have any suggestions. You can also contact me at belwal.mohit@gmail.com.

      Monday, 16 February 2015

      Getting Started with Percona XtraDB Cluster

      Percona XtraDB Cluster

      As a DevOps activist I am exploring Percona XtraDB. In a series of blogs I will share my learnings. This blog intends to capture step by step details of installation of Percona XtraDB in Cluster mode. 

      Why Cluster Mode Introduction:

      Percona XtraDB cluster is High Availability and Scalability solution for MySQL users which provides
                Synchronous replication : Transaction either committed on all nodes or none.
                Multi-master replication : You can write to any node.
                Parallel applying events on slave : parallel event application on all slave nodes
                Automatic node provisioning
                Data consistency

        Straight into the Act:Installing Percona XtraDB Cluster

        Pre-requisites/Assumptions
        1. OS - Ububtu
        2. 3 Ubuntu nodes are available
        For the sake of this discussion lets name the nodes as
        node 1
        hostname:
        percona_xtradb_cluster1
        IP: 192.168.1.2

        node 2
        hostname: percona_xtradb_cluster2
        IP: 192.168.1.3

        node 3
        hostname: percona_xtradb_cluster3
        IP: 192.168.1.4
        Repeat the below steps on all nodes

        STEP 1 : Add the Percona repository
        $ echo "deb http://repo.percona.com/apt precise main" >> /etc/apt/sources.list.d/percona.list
        $ echo "deb-src http://repo.percona.com/apt precise main" >> /etc/apt/sources.list.d/percona.list
        $ apt-key adv --keyserver keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A
        STEP 2 : After adding percona repository, Update apt cache so that new packages can be included in our apt-cache.
        $ apt-get update
        STEP 3 : Install Percona XtraDB Cluster :
        $ apt-get install -y percona-xtradb-cluster-56 qpress xtrabackup
        STEP 4 : Install additional package for editing files, downloading etc :
        $ apt-get install -y python-software-properties vim wget curl netcat

        With the above steps we have, installed Percona XtraDB Cluster on every node. Now we'll configure each node, so that a cluster of three nodes can be formed.

        Node Configuration:

        Add/Modify file /etc/mysql/my.cnf on first node :
        [MYSQLD] #This section is for mysql configuration
        user = mysql
        default_storage_engine = InnoDB
        basedir = /usr
        datadir = /var/lib/mysql
        socket = /var/run/mysqld/mysqld.sock
        port = 3306
        innodb_autoinc_lock_mode = 2
        log_queries_not_using_indexes = 1
        max_allowed_packet = 128M
        binlog_format = ROW
        wsrep_provider = /usr/lib/libgalera_smm.so
        wsrep_node_address = 192.168.1.2
        wsrep_cluster_name="newcluster"
        wsrep_cluster_address = gcomm://192.168.1.2,192.168.1.3,192.168.1.4
        wsrep_node_name = cluster1
        wsrep_slave_threads = 4
        wsrep_sst_method = xtrabackup-v2
        wsrep_sst_auth = sst:secret

        [sst] #This section is for sst(state snapshot transfer) configuration
        streamfmt = xbstream

        [xtrabackup] #This section is defines tuning configuration for xtrabackup
        compress
        compact
        parallel = 2
        compress_threads = 2
        rebuild_threads = 2
        Note :
                 wsrep_node_address = {IP of current node}
                 wsrep_cluster_name= {Name of cluster}
                 wsrep_cluster_address = gcomm://{Comma separated IP address’s which are in cluster}
                 wsrep_node_name = {This is name of current node which is used to identify it in cluster}

        Now as we have done node configuration. Now start first node services:
        Start the node :
        $ service mysql bootstrap-pxc
        Make sst user for authentication of cluster nodes :
        $ mysql -e "GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sst'@'localhost' IDENTIFIED BY 'secret';"
        Check cluster status :
        $ mysql -e "show global status like 'wsrep%';"

        Configuration file for second node:
        [MYSQLD]
        user = mysql
        default_storage_engine = InnoDB
        basedir = /usr
        datadir = /var/lib/mysql
        socket = /var/run/mysqld/mysqld.sock
        port = 3306
        innodb_autoinc_lock_mode = 2
        log_queries_not_using_indexes = 1
        max_allowed_packet = 128M
        binlog_format = ROW
        wsrep_provider = /usr/lib/libgalera_smm.so
        wsrep_node_address = 192.168.1.3
        wsrep_cluster_name="newcluster"
        wsrep_cluster_address = gcomm://192.168.1.2,192.168.1.3,192.168.1.4
        wsrep_node_name = cluster2
        wsrep_slave_threads = 4
        wsrep_sst_method = xtrabackup-v2
        wsrep_sst_auth = sst:secret

        [sst]
        streamfmt = xbstream

        [xtrabackup]
        compress
        compact
        parallel = 2
        After doing configuration, start services of node 2.
        Start node 2 :
        $ service mysql start
        Check cluster status :
        $ mysql -e "show global status like 'wsrep%';"
        Now similarly you have to configure node 3. Changes are listed below.
        Changes in configuration for node 3 :
        wsrep_node_address = 192.168.1.4

        wsrep_node_name = cluster3
        Start node 3 :
        $ service mysql start

        Test percona XtraDb cluster:

        Log-in by mysql client in any node:
        mysql>create database opstree;
        mysql>use opstree;
        mysql>create table nu113r(name varchar(50));
        mysql>insert into nu113r values("zukin");
        mysql>select * from nu113r;
        Check the database on other node by mysql client:
        mysql>show databases;
        Note : There should be a database named “opstree”.
        mysql>use opstree;
        mysql>select * from nu113r; 
        Note : Data will be same as in the previous node.

        Let me know if you have any suggestions. You can also contact me at belwal.mohit@gmail.com.