Tuesday 30 July 2013

Set Up Virtual Host on Ubuntu 12.04

1.     About Virtual Hosts

Virtual Hosts are used to run more than one domain off of a single IP address. This is especially useful to people who need to run several sites off on one virtual private server. The sites display different information to the visitors, depending on with which the users accessed the site.There is no limit to the number of virtual hosts that can be added to a VPS.


2.     Set Up

>  sudo su
 apt-get install apache2

3.     Create a new directory

The first step in creating a virtual host is to a create a directory where we will keep the new website’s information. 

>  mkdir –p /var/www/example.com/public_html

4.     Grant Permission

We need to grant ownership of the directory to the user, instead of just keeping it on the root system.

>  chown –R $USER:$USER /var/www/example.com/public_html
>  chmod –R 755 /var/www

5.     Create the Page

>  nano /var/www/example.com/public_html/index.html

We can add some text to the file so we will have something to look at when the IP redirects to the virtual host.

<html>
      <head>
                  <title>www.example.com</title>
      </head>
      <body>
                  <h1>Success: You have Set Up Virtual Host </h1>
      </body>
</html>

Save and Exit.

6.     Create the New Virtual Host File

>  cp /etc/apache2/sites-available/default /etc/apache2/sites-available/example.com

7.     Turn On Virtual Hosts

 nano /etc/apache2/sites-available/example.com

<VirtualHost *:80>
      ServerAdmin   webmaster@example.com
      ServerName    example.com
      ServerAlias      www.example.com
      DocumentRoot /var/www/example.com/public_html

Activate the host, with the built in apache shortcut

>  a2ensite example.com


8.     Restart Apache

> service apache2 restart

9.     Setting Up the Local Hosts

 nano /etc/hosts
ADD
#Virtual  Hosts
172.16.1.28           example.com
172.16.1.28           www.example.com

All Done.

Happy to help you !! 

Master-Master Replication In MySQL


About Master-Master Replication

MySQL replication is the process by which a single data set, stored in a MySQL database, will be live-copied to a second server.

Master-Master replication allows data to be copied from either server to the other one. This subtle but important difference between Master-Slave and Master-Master allows us to perform mysql read or writes from either server. This configuration adds redundancy and increases efficiency when dealing with accessing the data. 

The examples in this article will be based on two cloud servers, named Server C and Server D. .
 
Server C:  172.16.1.17
Server D:  172.16.1.28

1.      Setup

>  sudo su
>  apt-get install mysql-client
>  apt-get install mysql-server

2.      Configure MySQL On Server C

>  nano etc/mysql/my.cnf

There are four lines that we need to change, which are currently set to the following:

#server-id       = 1
#log_bin         = /var/log/mysql/mysql-bin.log
#binlog_do_db           = include_database_name
Bind-address  = 127.0.0.1

Change it to :

server-id         = 1
log_bin           = /var/log/mysql/mysql-bin.log
binlog_do_db = example
#bind-address = 127.0.0.1


Now, Refresh MySQL

 service mysql restart

Open up the MySQL Shell

>  MySQL -u root –p

We need to create a pseudo-user that will be used for replicating data between our two cloud servers. The examples in this article will assume that you name this user "replicator". Replace "password" with the password you wish to use for replication.

>  create user ‘replicator’@’%’ identified by ‘password’;
>  GRANT REPLICATION SLAVE ON *.* TO ‘slave_user’@’%’ ;

Switch to database

>  USE example;
>  SHOW MASTER STATUS;

Note : Remember File name and position. Here, I’m assuming File- mysql-bin.000001 and Position- 107

>  exit;


3.      Configure MySQL On Server D

>  sudo su
>  apt-get install mysql-client
>  apt-get install mysql-server

Now we need to configure the mysql server:

>  nano /etc/mysql/my.cnf

Once inside that file, we need to make a few changes same as server C. The changes are :
server_id                     = 2
relay-log                     = /var/log/mysql/mysql-relay-bin.log
log_bin                       = /var/log/mysql/mysql-bin.log
binlog_do_db             = example
#bind-address             = 127.0.0.1

Restart MySQL

>  service mysql restart
>  mysql –u  root –p
>  create user ‘replicator’@’%’ identified by ‘password’;
>  create database example;
>  grant replicator slave on *.* to ‘replicator’@’%’;
>  slave stop;
>  CHANGE MASTER TO MASTER_HOST=’172.16.1.17’,MASTER_USER=’repliactor’,MASTER_PASSWORD=’password’,MASTER_LOG_FILE=’mysql-bin.000001’,MASTER_LOG_POS=107;
>  slave start;

>  show master status;

 Note : Remember File name and position. Here, I’m assuming File- mysql-bin.000004 and Position- 107


4.      Complete Replication On Server C

>  slave stop;
>  CHANGE MASTER TO MASTER_HOST=’172.16.1.28’,MASTER_USER=’repliactor’,MASTER_PASSWORD=’password’,MASTER_LOG_FILE=’mysql-bin.000004’,MASTER_LOG_POS=107;
>  slave start;



All Done.
Happy to help you !!


Master-Slave Replication In MySQL


About MySQL Replication

Replication enables data from one MySQL database server (the master) to be replicated to one or more MySQL database servers (the slaves).

MySQL replication is a process that allows you to easily maintain multiple copies of a MySQL data by having them copied automatically from a master to a slave database. This can helpful for many reasons including facilating a backup for the data,a way to analyze it without using the main database, or simply as a means to scale out.

Replication is asynchronous - slaves need not be connected permanently to receive updates from the master. This means that updates can occur over long-distance connections and even over temporary or intermittent connections such as a dial-up service. Depending on the configuration, you can replicate all databases, selected databases, or even selected tables within a database.

This tutorial will cover a very simple example of mysql replication—one master will send information to a single slave. For the process to work you will need two IP addresses: one of the master server and and one of the slave. 

This tutorial will use the following IP addresses:
172.16.1.28 - Master Database
172.16.1.17 - Slave Database

1.      Setup

>  sudo su
>  apt-get install mysql-client
>  apt-get install mysql-server

2.      Configure the Master Database

Open up the mysql configuration file on the master server.

>  nano /etc/mysql/my.cnf

Once inside that file, we need to make a few changes. The changes are :

bind-address              = 172.16.1.28
server-id                     = 1
log_bin                       = /var/log/mysql/mysql-bin.log
binlog_do_db             = newdatabase

Now, Refresh MySQL 

>  service mysql restart

Open up the MySQL Shell

>  MySQL -u root –p

We need to grant privileges to the slave. You can use this line to name your slave and set up their password. The command should be in this format:

>  GRANT REPLICATION SLAVE ON *.* TO ‘slave_user’@’%’ IDENTIFIED BY ‘password’;
>  FLUSH PRIVILEGES;

Switch to new database

>  USE newdatabase;
>  FLUSH TABLES WITH READ LOCK;
>  SHOW MASTER STATUS

Note : Remember File name and position. Here, I’m assuming File- mysql-bin.000001 and Position- 107

>  Exit;

Proceeding the with the database still locked, export your database using mysqldump (make sure you are typing this command in the bash shell, not in MySQL)

>  mysqldump –u root –p –opt newdatabase > newdatabase.sql

>  mysql –u root –p

>  UNLOCK TABLES;

>  QUIT;

3.      Configure Slave Database

>  sudo su
>  apt-get install mysql-client
>  apt-get install mysql-server

Log into your slave server, open up the MySQL shell and create the new database that you will be replicating from the master (then exit):

>  mysql –u root –p
>  CREATE DATABASE newdatabase;
>  EXIT;

Now we need to configure the slave configuration in the same way as we did the master:

>  nano /etc/mysql/my.cnf

Once inside that file, we need to make a few changes. The changes are :

server_id                    = 2
relay-log                     = /var/log/mysql/mysql-relay-bin.log
log_bin                       = /var/log/mysql/mysql-bin.log
binlog_do_db = newdatabase

Restart MySQL

>  service mysql restart
>  mysql –u root –p
>  CHANGE MASTER TO MASTER_HOST=’172.16.1.28’,MASTER_USER=’slave_user’,MASTER_PASSWORD=’password’,MASTER_LOG_FILE=’mysql-bin.000001’,MASTER_LOG_POS=107;

This command accomplishes several things at the same time:

1.      It designates the current server as the slave of our master server.

2.      It provides the server the correct login credentials

3.      Last of all, it lets the slave server know where to start replicating from; the master log file and log position come from the numbers we wrote down previously.

>  START SLAVE;
>  SHOW SLAVE STATUS\G

If there is an issue in connecting, you can try starting slave with a command to skip over it:
>  STOP SLAVE;
>  SET GLOBAL SQL_SLAVE_SKIP_COUNTER =1;
>  START SLAVE;

All Done.
Happy to help you !!