Month: December 2009

Bash Scripting ~ apprendimento da esempi

Bash Scripting ~ apprendimento da esempi

Introduction ~

—————————————–
Program (1) ~ array.sh
—————————————–

#!/bin/bash
echo “==============”
declare -a myarr[0]=”Arun”
declare -a myarr1
myarr1=(arun bagul bangalore mumbai raju santhosh)
myarr[1]=”Bagul”

echo “my name is ${myarr[0]} ${myarr[1]}”
echo “————————–”
echo “${myarr1[*]}”
echo ${myarr1[2]}
echo ${myarr1[@]}
echo “————————–”
echo “Total no of elements in array – ${#myarr1[*]}”
echo “Total no of elements in array – ${#myarr1[@]}”
echo “Size of word ‘${myarr1[2]}’ is – ${#myarr1[2]}”
echo ${#myarr1[1]}
echo ${#myarr1[0]}

echo “————————–”

#how to delete element in array
unset myarr[1]
echo “myarr is – ${myarr[*]}”

#how to assign element in array
myarr[1]=”- System Engineer!”
echo “myarr is – ${myarr[*]}”

echo ${myarr}

————————————————————————
Program (2) ~ command_line_arguments.sh
————————————————————————

#!/bin/bash

echo “Script/command name => $0”
echo “arg1 => $1”
echo “arg2 => $2”
echo “arg3 => $3”
echo “Total No of argument = $#”

echo “Script PID is => $$”
echo “Status of previous command – $?”

name=$myname
echo “Name – $name”

read n

————————————————-
Program (3) ~ default_value.sh
————————————————-

#!/bin/bash

#start=’123′
#start=${1:-$start}

start=${1:-‘123’}

echo “Value of ‘start’ variable is ==> $start”

—————————————————
Program (4) ~ echo_example.sh
—————————————————

#!/bin/bash

name=”Arun”

echo -e “My Name is $name_arun and \n”
echo -e “My Name is ${name}_arun and \n”

echo -e ‘My Name is $name and \n’

—————————————–
Program (5) ~ elif.sh
—————————————–

#! /bin/bash

if [ $1 -eq $2 ];then
echo “good”
elif [ $2 -eq $3 ];then
echo “Fine”
elif [ $1 -eq $3 ];then
echo “OK”
else
echo “NO”
fi

————————————————————
Program (6) ~ for_loop_example-1.sh
————————————————————

#!/bin/bash

i=1
while [ $i -le 512 ]
do
temp=$i
echo “What is => $i | $temp”
i=$(expr $i + 32)
for (( j=$temp; $j<=$i; j++ ))
do
echo -n ” $j”
done
done

———————————————————–
Program (7) ~ for_loop_example-2.sh
———————————————————–

#!/bin/bash
#for val in $(ls -1 /tmp)
sum=0
#for val in {1..5}
#for val in {$1..$2}
for((val=$1;$val<=$2;val++))
do
#echo “$val”
sum=$(expr $sum + $val )
#sum=`expr $sum + $val`
done

echo “$0 # Sum of $1 to $2 => $sum”

————————————————————
Program (8) ~ for_loop_example-3.sh
————————————————————

#!/bin/bash

for i in {1..9}
do
echo $i
done

—————————————–
Program (9) ~ function.sh
—————————————–

#!/bin/bash

function my_function()
{
name=”Arun Bagul”
echo “‘my_function’ body ~ $name”
return 1;
}
##########

myfunc()
{
echo “Another way of defining the function”
}

##########################

echo “Starting function program”
echo “——————————”

#calling function here
my_function
##
myfunc

echo -e “\n end of program”

—————————————————————————————
Program (10) ~ how_to_pass_argument_to_function.sh
—————————————————————————————

#!/bin/bash

function my_function()
{
echo “Total number of argument ~ $#”
echo “Arg1 => $1”
echo “Arg2 => $2”
echo “Arg3 => $3”
return 0;
}
##########

echo “Starting function program”
echo “——————————”

#calling function here
my_function arun bagul 1234

————————————————————————-
Program (11) ~ how_to_take_hidden_input.sh
————————————————————————-

#!/bin/bash

echo -n “Enter User Name :”
read username
echo -n “Enter Password :”
read -s mypwd

echo -e “\nI am $username and my password is – $mypwd”

——————————————————————————
Program (12) ~ how_to_take_input_from_user.sh
—————————————————————————–

#!/bin/bash

echo -ne “Enter the Name:- ”
read name
echo -n -e “Enter the Number:- ”
read num

echo “——————————”

add=$(expr $num + 10)

echo “Name is ~ $name”
echo “Number is ~ $add”

—————————————–
Program (13) ~ ifthen.sh
—————————————–

#!/bin/bash

if [ “arun” == “arun” ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ 2 == 2 ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ “arun” = “arun” ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ 2 -eq 2 ];then
echo “true!”
else
echo “false”
fi

——————————————————
Program (14) ~ non-interactive.sh
——————————————————

#!/usr/bin/expect -f
spawn ssh arun@192.168.0.1
expect “password:”
sleep 1
send “pwd\r”
interact

—————————————————————-
Program (15) ~ read_file_line_by_line.sh
—————————————————————-

#!/bin/bash

file_name=”/etc/hosts”

while read myvar
do
echo “Line => $myvar”
done < $file_name

echo “#################################################”

for myvar1 in $(cat $file_name)
do
echo “Line => $myvar1”
done

——————————————————
Program (16) ~ reverse-number.sh
——————————————————

#!/bin/bash

declare -a date_array
num=$1
i=$(expr $(echo $num | wc -c) – 1 )

while [ $num -gt 10 ]
do
temp=$( expr $num % 10 )
num=$( expr $num / 10);
echo “Digit($i) => $temp”
date_array[$i]=”${temp}”
i=$(expr $i – 1)
done
echo “Digit($i) => $num”
date_array[$i]=”${num}”

echo ${date_array[*]}

——————————————————–
Program (17) ~ string-operation.sh
——————————————————–

#! /bin/bash

echo “Arun Bagul:-”
string=”/root/arun/bagul/image.gif”
echo “string=> $string”
echo “String=> ${string##/*/}”
echo “String=> ${string#/*/}”

echo “String=> ${string%.*}”
echo “String=> ${string%%.*}”
#str=”/home/y/conf/arunbagul/daily_market_0.conf”
str=”${str##/*conf/}”
echo “String=> ${str%/*}”

#done

mystr=”keyword_summary_exact_arunsb”
echo $mystr
echo ${mystr%_*}

echo “$*”

—————————————–
Program (18) ~ switch.sh
—————————————–

#!/bin/bash

echo ” Switch program | arg1 => $1″
echo ” ——————————-”
case $1 in

123)
echo “Case is 123”
;;

arun)
echo “Case is ‘arun'”
;;

pri*)

echo “Case is ‘pri*'”
;;

*)
echo ” * Usage: $0 ”
echo ” Default case (nothing is matched)”
exit 0;
;;
esac

—————————————————————–
Program (19) ~ while_loop_example-1.sh
——————————————————————

#!/bin/bash

mywait=wait

while [ “${mywait}” = “wait” ]
do
echo “arun”
done

——————————————————————-
Program (20) ~ while_loop_example-2.sh
——————————————————————–

#! /bin/bash

## on command line -> i=0 && while [ $i -le 10 ] ; do echo $i; i=$(expr $i + 1); done

i=0
while [ $i -le 10 ]
do
echo $i
i=$(expr $i + 1)
done

——————————————————————–

* Please download PDF file http://www.slideshare.net/arunbagul/bash-learning-by-examples/

Regards,
Arun

Bash-Skript ~ Lernen durch Beispiele

Bash-Skript ~ Lernen durch Beispiele

Introduction ~

—————————————–
Program (1) ~ array.sh
—————————————–

#!/bin/bash
echo “==============”
declare -a myarr[0]=”Arun”
declare -a myarr1
myarr1=(arun bagul bangalore mumbai raju santhosh)
myarr[1]=”Bagul”

echo “my name is ${myarr[0]} ${myarr[1]}”
echo “————————–”
echo “${myarr1[*]}”
echo ${myarr1[2]}
echo ${myarr1[@]}
echo “————————–”
echo “Total no of elements in array – ${#myarr1[*]}”
echo “Total no of elements in array – ${#myarr1[@]}”
echo “Size of word ‘${myarr1[2]}’ is – ${#myarr1[2]}”
echo ${#myarr1[1]}
echo ${#myarr1[0]}

echo “————————–”

#how to delete element in array
unset myarr[1]
echo “myarr is – ${myarr[*]}”

#how to assign element in array
myarr[1]=”- System Engineer!”
echo “myarr is – ${myarr[*]}”

echo ${myarr}

————————————————————————
Program (2) ~ command_line_arguments.sh
————————————————————————

#!/bin/bash

echo “Script/command name => $0”
echo “arg1 => $1”
echo “arg2 => $2”
echo “arg3 => $3”
echo “Total No of argument = $#”

echo “Script PID is => $$”
echo “Status of previous command – $?”

name=$myname
echo “Name – $name”

read n

————————————————-
Program (3) ~ default_value.sh
————————————————-

#!/bin/bash

#start=’123′
#start=${1:-$start}

start=${1:-‘123’}

echo “Value of ‘start’ variable is ==> $start”

—————————————————
Program (4) ~ echo_example.sh
—————————————————

#!/bin/bash

name=”Arun”

echo -e “My Name is $name_arun and \n”
echo -e “My Name is ${name}_arun and \n”

echo -e ‘My Name is $name and \n’

—————————————–
Program (5) ~ elif.sh
—————————————–

#! /bin/bash

if [ $1 -eq $2 ];then
echo “good”
elif [ $2 -eq $3 ];then
echo “Fine”
elif [ $1 -eq $3 ];then
echo “OK”
else
echo “NO”
fi

————————————————————
Program (6) ~ for_loop_example-1.sh
————————————————————

#!/bin/bash

i=1
while [ $i -le 512 ]
do
temp=$i
echo “What is => $i | $temp”
i=$(expr $i + 32)
for (( j=$temp; $j<=$i; j++ ))
do
echo -n ” $j”
done
done

———————————————————–
Program (7) ~ for_loop_example-2.sh
———————————————————–

#!/bin/bash
#for val in $(ls -1 /tmp)
sum=0
#for val in {1..5}
#for val in {$1..$2}
for((val=$1;$val<=$2;val++))
do
#echo “$val”
sum=$(expr $sum + $val )
#sum=`expr $sum + $val`
done

echo “$0 # Sum of $1 to $2 => $sum”

————————————————————
Program (8) ~ for_loop_example-3.sh
————————————————————

#!/bin/bash

for i in {1..9}
do
echo $i
done

—————————————–
Program (9) ~ function.sh
—————————————–

#!/bin/bash

function my_function()
{
name=”Arun Bagul”
echo “‘my_function’ body ~ $name”
return 1;
}
##########

myfunc()
{
echo “Another way of defining the function”
}

##########################

echo “Starting function program”
echo “——————————”

#calling function here
my_function
##
myfunc

echo -e “\n end of program”

—————————————————————————————
Program (10) ~ how_to_pass_argument_to_function.sh
—————————————————————————————

#!/bin/bash

function my_function()
{
echo “Total number of argument ~ $#”
echo “Arg1 => $1”
echo “Arg2 => $2”
echo “Arg3 => $3”
return 0;
}
##########

echo “Starting function program”
echo “——————————”

#calling function here
my_function arun bagul 1234

————————————————————————-
Program (11) ~ how_to_take_hidden_input.sh
————————————————————————-

#!/bin/bash

echo -n “Enter User Name :”
read username
echo -n “Enter Password :”
read -s mypwd

echo -e “\nI am $username and my password is – $mypwd”

——————————————————————————
Program (12) ~ how_to_take_input_from_user.sh
—————————————————————————–

#!/bin/bash

echo -ne “Enter the Name:- ”
read name
echo -n -e “Enter the Number:- ”
read num

echo “——————————”

add=$(expr $num + 10)

echo “Name is ~ $name”
echo “Number is ~ $add”

—————————————–
Program (13) ~ ifthen.sh
—————————————–

#!/bin/bash

if [ “arun” == “arun” ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ 2 == 2 ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ “arun” = “arun” ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ 2 -eq 2 ];then
echo “true!”
else
echo “false”
fi

——————————————————
Program (14) ~ non-interactive.sh
——————————————————

#!/usr/bin/expect -f
spawn ssh arun@192.168.0.1
expect “password:”
sleep 1
send “pwd\r”
interact

—————————————————————-
Program (15) ~ read_file_line_by_line.sh
—————————————————————-

#!/bin/bash

file_name=”/etc/hosts”

while read myvar
do
echo “Line => $myvar”
done < $file_name

echo “#################################################”

for myvar1 in $(cat $file_name)
do
echo “Line => $myvar1”
done

——————————————————
Program (16) ~ reverse-number.sh
——————————————————

#!/bin/bash

declare -a date_array
num=$1
i=$(expr $(echo $num | wc -c) – 1 )

while [ $num -gt 10 ]
do
temp=$( expr $num % 10 )
num=$( expr $num / 10);
echo “Digit($i) => $temp”
date_array[$i]=”${temp}”
i=$(expr $i – 1)
done
echo “Digit($i) => $num”
date_array[$i]=”${num}”

echo ${date_array[*]}

——————————————————–
Program (17) ~ string-operation.sh
——————————————————–

#! /bin/bash

echo “Arun Bagul:-”
string=”/root/arun/bagul/image.gif”
echo “string=> $string”
echo “String=> ${string##/*/}”
echo “String=> ${string#/*/}”

echo “String=> ${string%.*}”
echo “String=> ${string%%.*}”
#str=”/home/y/conf/arunbagul/daily_market_0.conf”
str=”${str##/*conf/}”
echo “String=> ${str%/*}”

#done

mystr=”keyword_summary_exact_arunsb”
echo $mystr
echo ${mystr%_*}

echo “$*”

—————————————–
Program (18) ~ switch.sh
—————————————–

#!/bin/bash

echo ” Switch program | arg1 => $1″
echo ” ——————————-”
case $1 in

123)
echo “Case is 123”
;;

arun)
echo “Case is ‘arun'”
;;

pri*)

echo “Case is ‘pri*'”
;;

*)
echo ” * Usage: $0 ”
echo ” Default case (nothing is matched)”
exit 0;
;;
esac

—————————————————————–
Program (19) ~ while_loop_example-1.sh
——————————————————————

#!/bin/bash

mywait=wait

while [ “${mywait}” = “wait” ]
do
echo “arun”
done

——————————————————————-
Program (20) ~ while_loop_example-2.sh
——————————————————————–

#! /bin/bash

## on command line -> i=0 && while [ $i -le 10 ] ; do echo $i; i=$(expr $i + 1); done

i=0
while [ $i -le 10 ]
do
echo $i
i=$(expr $i + 1)
done

——————————————————————–

* Please download PDF file http://www.slideshare.net/arunbagul/bash-learning-by-examples/

Regards,
Arun

Scripts Bash ~ apprentissage par des exemples

Scripts Bash ~ apprentissage par des exemples

Introduction ~

—————————————–
Program (1) ~ array.sh
—————————————–

#!/bin/bash
echo “==============”
declare -a myarr[0]=”Arun”
declare -a myarr1
myarr1=(arun bagul bangalore mumbai raju santhosh)
myarr[1]=”Bagul”

echo “my name is ${myarr[0]} ${myarr[1]}”
echo “————————–”
echo “${myarr1[*]}”
echo ${myarr1[2]}
echo ${myarr1[@]}
echo “————————–”
echo “Total no of elements in array – ${#myarr1[*]}”
echo “Total no of elements in array – ${#myarr1[@]}”
echo “Size of word ‘${myarr1[2]}’ is – ${#myarr1[2]}”
echo ${#myarr1[1]}
echo ${#myarr1[0]}

echo “————————–”

#how to delete element in array
unset myarr[1]
echo “myarr is – ${myarr[*]}”

#how to assign element in array
myarr[1]=”- System Engineer!”
echo “myarr is – ${myarr[*]}”

echo ${myarr}

————————————————————————
Program (2) ~ command_line_arguments.sh
————————————————————————

#!/bin/bash

echo “Script/command name => $0”
echo “arg1 => $1”
echo “arg2 => $2”
echo “arg3 => $3”
echo “Total No of argument = $#”

echo “Script PID is => $$”
echo “Status of previous command – $?”

name=$myname
echo “Name – $name”

read n

————————————————-
Program (3) ~ default_value.sh
————————————————-

#!/bin/bash

#start=’123′
#start=${1:-$start}

start=${1:-‘123’}

echo “Value of ‘start’ variable is ==> $start”

—————————————————
Program (4) ~ echo_example.sh
—————————————————

#!/bin/bash

name=”Arun”

echo -e “My Name is $name_arun and \n”
echo -e “My Name is ${name}_arun and \n”

echo -e ‘My Name is $name and \n’

—————————————–
Program (5) ~ elif.sh
—————————————–

#! /bin/bash

if [ $1 -eq $2 ];then
echo “good”
elif [ $2 -eq $3 ];then
echo “Fine”
elif [ $1 -eq $3 ];then
echo “OK”
else
echo “NO”
fi

————————————————————
Program (6) ~ for_loop_example-1.sh
————————————————————

#!/bin/bash

i=1
while [ $i -le 512 ]
do
temp=$i
echo “What is => $i | $temp”
i=$(expr $i + 32)
for (( j=$temp; $j<=$i; j++ ))
do
echo -n ” $j”
done
done

———————————————————–
Program (7) ~ for_loop_example-2.sh
———————————————————–

#!/bin/bash
#for val in $(ls -1 /tmp)
sum=0
#for val in {1..5}
#for val in {$1..$2}
for((val=$1;$val<=$2;val++))
do
#echo “$val”
sum=$(expr $sum + $val )
#sum=`expr $sum + $val`
done

echo “$0 # Sum of $1 to $2 => $sum”

————————————————————
Program (8) ~ for_loop_example-3.sh
————————————————————

#!/bin/bash

for i in {1..9}
do
echo $i
done

—————————————–
Program (9) ~ function.sh
—————————————–

#!/bin/bash

function my_function()
{
name=”Arun Bagul”
echo “‘my_function’ body ~ $name”
return 1;
}
##########

myfunc()
{
echo “Another way of defining the function”
}

##########################

echo “Starting function program”
echo “——————————”

#calling function here
my_function
##
myfunc

echo -e “\n end of program”

—————————————————————————————
Program (10) ~ how_to_pass_argument_to_function.sh
—————————————————————————————

#!/bin/bash

function my_function()
{
echo “Total number of argument ~ $#”
echo “Arg1 => $1”
echo “Arg2 => $2”
echo “Arg3 => $3”
return 0;
}
##########

echo “Starting function program”
echo “——————————”

#calling function here
my_function arun bagul 1234

————————————————————————-
Program (11) ~ how_to_take_hidden_input.sh
————————————————————————-

#!/bin/bash

echo -n “Enter User Name :”
read username
echo -n “Enter Password :”
read -s mypwd

echo -e “\nI am $username and my password is – $mypwd”

——————————————————————————
Program (12) ~ how_to_take_input_from_user.sh
—————————————————————————–

#!/bin/bash

echo -ne “Enter the Name:- ”
read name
echo -n -e “Enter the Number:- ”
read num

echo “——————————”

add=$(expr $num + 10)

echo “Name is ~ $name”
echo “Number is ~ $add”

—————————————–
Program (13) ~ ifthen.sh
—————————————–

#!/bin/bash

if [ “arun” == “arun” ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ 2 == 2 ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ “arun” = “arun” ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ 2 -eq 2 ];then
echo “true!”
else
echo “false”
fi

——————————————————
Program (14) ~ non-interactive.sh
——————————————————

#!/usr/bin/expect -f
spawn ssh arun@192.168.0.1
expect “password:”
sleep 1
send “pwd\r”
interact

—————————————————————-
Program (15) ~ read_file_line_by_line.sh
—————————————————————-

#!/bin/bash

file_name=”/etc/hosts”

while read myvar
do
echo “Line => $myvar”
done < $file_name

echo “#################################################”

for myvar1 in $(cat $file_name)
do
echo “Line => $myvar1”
done

——————————————————
Program (16) ~ reverse-number.sh
——————————————————

#!/bin/bash

declare -a date_array
num=$1
i=$(expr $(echo $num | wc -c) – 1 )

while [ $num -gt 10 ]
do
temp=$( expr $num % 10 )
num=$( expr $num / 10);
echo “Digit($i) => $temp”
date_array[$i]=”${temp}”
i=$(expr $i – 1)
done
echo “Digit($i) => $num”
date_array[$i]=”${num}”

echo ${date_array[*]}

——————————————————–
Program (17) ~ string-operation.sh
——————————————————–

#! /bin/bash

echo “Arun Bagul:-”
string=”/root/arun/bagul/image.gif”
echo “string=> $string”
echo “String=> ${string##/*/}”
echo “String=> ${string#/*/}”

echo “String=> ${string%.*}”
echo “String=> ${string%%.*}”
#str=”/home/y/conf/arunbagul/daily_market_0.conf”
str=”${str##/*conf/}”
echo “String=> ${str%/*}”

#done

mystr=”keyword_summary_exact_arunsb”
echo $mystr
echo ${mystr%_*}

echo “$*”

—————————————–
Program (18) ~ switch.sh
—————————————–

#!/bin/bash

echo ” Switch program | arg1 => $1″
echo ” ——————————-”
case $1 in

123)
echo “Case is 123”
;;

arun)
echo “Case is ‘arun'”
;;

pri*)

echo “Case is ‘pri*'”
;;

*)
echo ” * Usage: $0 ”
echo ” Default case (nothing is matched)”
exit 0;
;;
esac

—————————————————————–
Program (19) ~ while_loop_example-1.sh
——————————————————————

#!/bin/bash

mywait=wait

while [ “${mywait}” = “wait” ]
do
echo “arun”
done

——————————————————————-
Program (20) ~ while_loop_example-2.sh
——————————————————————–

#! /bin/bash

## on command line -> i=0 && while [ $i -le 10 ] ; do echo $i; i=$(expr $i + 1); done

i=0
while [ $i -le 10 ]
do
echo $i
i=$(expr $i + 1)
done

——————————————————————–

* Please download PDF file http://www.slideshare.net/arunbagul/bash-learning-by-examples/

Regards,
Arun

BASH Scripting ~ leren door voorbeelden

BASH Scripting ~ leren door voorbeelden

Introduction ~

—————————————–
Program (1) ~ array.sh
—————————————–

#!/bin/bash
echo “==============”
declare -a myarr[0]=”Arun”
declare -a myarr1
myarr1=(arun bagul bangalore mumbai raju santhosh)
myarr[1]=”Bagul”

echo “my name is ${myarr[0]} ${myarr[1]}”
echo “————————–”
echo “${myarr1[*]}”
echo ${myarr1[2]}
echo ${myarr1[@]}
echo “————————–”
echo “Total no of elements in array – ${#myarr1[*]}”
echo “Total no of elements in array – ${#myarr1[@]}”
echo “Size of word ‘${myarr1[2]}’ is – ${#myarr1[2]}”
echo ${#myarr1[1]}
echo ${#myarr1[0]}

echo “————————–”

#how to delete element in array
unset myarr[1]
echo “myarr is – ${myarr[*]}”

#how to assign element in array
myarr[1]=”- System Engineer!”
echo “myarr is – ${myarr[*]}”

echo ${myarr}

————————————————————————
Program (2) ~ command_line_arguments.sh
————————————————————————

#!/bin/bash

echo “Script/command name => $0”
echo “arg1 => $1”
echo “arg2 => $2”
echo “arg3 => $3”
echo “Total No of argument = $#”

echo “Script PID is => $$”
echo “Status of previous command – $?”

name=$myname
echo “Name – $name”

read n

————————————————-
Program (3) ~ default_value.sh
————————————————-

#!/bin/bash

#start=’123′
#start=${1:-$start}

start=${1:-‘123’}

echo “Value of ‘start’ variable is ==> $start”

—————————————————
Program (4) ~ echo_example.sh
—————————————————

#!/bin/bash

name=”Arun”

echo -e “My Name is $name_arun and \n”
echo -e “My Name is ${name}_arun and \n”

echo -e ‘My Name is $name and \n’

—————————————–
Program (5) ~ elif.sh
—————————————–

#! /bin/bash

if [ $1 -eq $2 ];then
echo “good”
elif [ $2 -eq $3 ];then
echo “Fine”
elif [ $1 -eq $3 ];then
echo “OK”
else
echo “NO”
fi

————————————————————
Program (6) ~ for_loop_example-1.sh
————————————————————

#!/bin/bash

i=1
while [ $i -le 512 ]
do
temp=$i
echo “What is => $i | $temp”
i=$(expr $i + 32)
for (( j=$temp; $j<=$i; j++ ))
do
echo -n ” $j”
done
done

———————————————————–
Program (7) ~ for_loop_example-2.sh
———————————————————–

#!/bin/bash
#for val in $(ls -1 /tmp)
sum=0
#for val in {1..5}
#for val in {$1..$2}
for((val=$1;$val<=$2;val++))
do
#echo “$val”
sum=$(expr $sum + $val )
#sum=`expr $sum + $val`
done

echo “$0 # Sum of $1 to $2 => $sum”

————————————————————
Program (8) ~ for_loop_example-3.sh
————————————————————

#!/bin/bash

for i in {1..9}
do
echo $i
done

—————————————–
Program (9) ~ function.sh
—————————————–

#!/bin/bash

function my_function()
{
name=”Arun Bagul”
echo “‘my_function’ body ~ $name”
return 1;
}
##########

myfunc()
{
echo “Another way of defining the function”
}

##########################

echo “Starting function program”
echo “——————————”

#calling function here
my_function
##
myfunc

echo -e “\n end of program”

—————————————————————————————
Program (10) ~ how_to_pass_argument_to_function.sh
—————————————————————————————

#!/bin/bash

function my_function()
{
echo “Total number of argument ~ $#”
echo “Arg1 => $1”
echo “Arg2 => $2”
echo “Arg3 => $3”
return 0;
}
##########

echo “Starting function program”
echo “——————————”

#calling function here
my_function arun bagul 1234

————————————————————————-
Program (11) ~ how_to_take_hidden_input.sh
————————————————————————-

#!/bin/bash

echo -n “Enter User Name :”
read username
echo -n “Enter Password :”
read -s mypwd

echo -e “\nI am $username and my password is – $mypwd”

——————————————————————————
Program (12) ~ how_to_take_input_from_user.sh
—————————————————————————–

#!/bin/bash

echo -ne “Enter the Name:- ”
read name
echo -n -e “Enter the Number:- ”
read num

echo “——————————”

add=$(expr $num + 10)

echo “Name is ~ $name”
echo “Number is ~ $add”

—————————————–
Program (13) ~ ifthen.sh
—————————————–

#!/bin/bash

if [ “arun” == “arun” ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ 2 == 2 ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ “arun” = “arun” ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ 2 -eq 2 ];then
echo “true!”
else
echo “false”
fi

——————————————————
Program (14) ~ non-interactive.sh
——————————————————

#!/usr/bin/expect -f
spawn ssh arun@192.168.0.1
expect “password:”
sleep 1
send “pwd\r”
interact

—————————————————————-
Program (15) ~ read_file_line_by_line.sh
—————————————————————-

#!/bin/bash

file_name=”/etc/hosts”

while read myvar
do
echo “Line => $myvar”
done < $file_name

echo “#################################################”

for myvar1 in $(cat $file_name)
do
echo “Line => $myvar1”
done

——————————————————
Program (16) ~ reverse-number.sh
——————————————————

#!/bin/bash

declare -a date_array
num=$1
i=$(expr $(echo $num | wc -c) – 1 )

while [ $num -gt 10 ]
do
temp=$( expr $num % 10 )
num=$( expr $num / 10);
echo “Digit($i) => $temp”
date_array[$i]=”${temp}”
i=$(expr $i – 1)
done
echo “Digit($i) => $num”
date_array[$i]=”${num}”

echo ${date_array[*]}

——————————————————–
Program (17) ~ string-operation.sh
——————————————————–

#! /bin/bash

echo “Arun Bagul:-”
string=”/root/arun/bagul/image.gif”
echo “string=> $string”
echo “String=> ${string##/*/}”
echo “String=> ${string#/*/}”

echo “String=> ${string%.*}”
echo “String=> ${string%%.*}”
#str=”/home/y/conf/arunbagul/daily_market_0.conf”
str=”${str##/*conf/}”
echo “String=> ${str%/*}”

#done

mystr=”keyword_summary_exact_arunsb”
echo $mystr
echo ${mystr%_*}

echo “$*”

—————————————–
Program (18) ~ switch.sh
—————————————–

#!/bin/bash

echo ” Switch program | arg1 => $1″
echo ” ——————————-”
case $1 in

123)
echo “Case is 123”
;;

arun)
echo “Case is ‘arun'”
;;

pri*)

echo “Case is ‘pri*'”
;;

*)
echo ” * Usage: $0 ”
echo ” Default case (nothing is matched)”
exit 0;
;;
esac

—————————————————————–
Program (19) ~ while_loop_example-1.sh
——————————————————————

#!/bin/bash

mywait=wait

while [ “${mywait}” = “wait” ]
do
echo “arun”
done

——————————————————————-
Program (20) ~ while_loop_example-2.sh
——————————————————————–

#! /bin/bash

## on command line -> i=0 && while [ $i -le 10 ] ; do echo $i; i=$(expr $i + 1); done

i=0
while [ $i -le 10 ]
do
echo $i
i=$(expr $i + 1)
done

——————————————————————–

* Please download PDF file http://www.slideshare.net/arunbagul/bash-learning-by-examples/

Regards,
Arun

BASH脚本〜以实例学习

BASH脚本〜以实例学习

Introduction ~

—————————————–
Program (1) ~ array.sh
—————————————–

#!/bin/bash
echo “==============”
declare -a myarr[0]=”Arun”
declare -a myarr1
myarr1=(arun bagul bangalore mumbai raju santhosh)
myarr[1]=”Bagul”

echo “my name is ${myarr[0]} ${myarr[1]}”
echo “————————–”
echo “${myarr1[*]}”
echo ${myarr1[2]}
echo ${myarr1[@]}
echo “————————–”
echo “Total no of elements in array – ${#myarr1[*]}”
echo “Total no of elements in array – ${#myarr1[@]}”
echo “Size of word ‘${myarr1[2]}’ is – ${#myarr1[2]}”
echo ${#myarr1[1]}
echo ${#myarr1[0]}

echo “————————–”

#how to delete element in array
unset myarr[1]
echo “myarr is – ${myarr[*]}”

#how to assign element in array
myarr[1]=”- System Engineer!”
echo “myarr is – ${myarr[*]}”

echo ${myarr}

————————————————————————
Program (2) ~ command_line_arguments.sh
————————————————————————

#!/bin/bash

echo “Script/command name => $0”
echo “arg1 => $1”
echo “arg2 => $2”
echo “arg3 => $3”
echo “Total No of argument = $#”

echo “Script PID is => $$”
echo “Status of previous command – $?”

name=$myname
echo “Name – $name”

read n

————————————————-
Program (3) ~ default_value.sh
————————————————-

#!/bin/bash

#start=’123′
#start=${1:-$start}

start=${1:-‘123’}

echo “Value of ‘start’ variable is ==> $start”

—————————————————
Program (4) ~ echo_example.sh
—————————————————

#!/bin/bash

name=”Arun”

echo -e “My Name is $name_arun and \n”
echo -e “My Name is ${name}_arun and \n”

echo -e ‘My Name is $name and \n’

—————————————–
Program (5) ~ elif.sh
—————————————–

#! /bin/bash

if [ $1 -eq $2 ];then
echo “good”
elif [ $2 -eq $3 ];then
echo “Fine”
elif [ $1 -eq $3 ];then
echo “OK”
else
echo “NO”
fi

————————————————————
Program (6) ~ for_loop_example-1.sh
————————————————————

#!/bin/bash

i=1
while [ $i -le 512 ]
do
temp=$i
echo “What is => $i | $temp”
i=$(expr $i + 32)
for (( j=$temp; $j<=$i; j++ ))
do
echo -n ” $j”
done
done

———————————————————–
Program (7) ~ for_loop_example-2.sh
———————————————————–

#!/bin/bash
#for val in $(ls -1 /tmp)
sum=0
#for val in {1..5}
#for val in {$1..$2}
for((val=$1;$val<=$2;val++))
do
#echo “$val”
sum=$(expr $sum + $val )
#sum=`expr $sum + $val`
done

echo “$0 # Sum of $1 to $2 => $sum”

————————————————————
Program (8) ~ for_loop_example-3.sh
————————————————————

#!/bin/bash

for i in {1..9}
do
echo $i
done

—————————————–
Program (9) ~ function.sh
—————————————–

#!/bin/bash

function my_function()
{
name=”Arun Bagul”
echo “‘my_function’ body ~ $name”
return 1;
}
##########

myfunc()
{
echo “Another way of defining the function”
}

##########################

echo “Starting function program”
echo “——————————”

#calling function here
my_function
##
myfunc

echo -e “\n end of program”

—————————————————————————————
Program (10) ~ how_to_pass_argument_to_function.sh
—————————————————————————————

#!/bin/bash

function my_function()
{
echo “Total number of argument ~ $#”
echo “Arg1 => $1”
echo “Arg2 => $2”
echo “Arg3 => $3”
return 0;
}
##########

echo “Starting function program”
echo “——————————”

#calling function here
my_function arun bagul 1234

————————————————————————-
Program (11) ~ how_to_take_hidden_input.sh
————————————————————————-

#!/bin/bash

echo -n “Enter User Name :”
read username
echo -n “Enter Password :”
read -s mypwd

echo -e “\nI am $username and my password is – $mypwd”

——————————————————————————
Program (12) ~ how_to_take_input_from_user.sh
—————————————————————————–

#!/bin/bash

echo -ne “Enter the Name:- ”
read name
echo -n -e “Enter the Number:- ”
read num

echo “——————————”

add=$(expr $num + 10)

echo “Name is ~ $name”
echo “Number is ~ $add”

—————————————–
Program (13) ~ ifthen.sh
—————————————–

#!/bin/bash

if [ “arun” == “arun” ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ 2 == 2 ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ “arun” = “arun” ];then
echo “true!”
else
echo “false”
fi

echo “———————————-”

if [ 2 -eq 2 ];then
echo “true!”
else
echo “false”
fi

——————————————————
Program (14) ~ non-interactive.sh
——————————————————

#!/usr/bin/expect -f
spawn ssh arun@192.168.0.1
expect “password:”
sleep 1
send “pwd\r”
interact

—————————————————————-
Program (15) ~ read_file_line_by_line.sh
—————————————————————-

#!/bin/bash

file_name=”/etc/hosts”

while read myvar
do
echo “Line => $myvar”
done < $file_name

echo “#################################################”

for myvar1 in $(cat $file_name)
do
echo “Line => $myvar1”
done

——————————————————
Program (16) ~ reverse-number.sh
——————————————————

#!/bin/bash

declare -a date_array
num=$1
i=$(expr $(echo $num | wc -c) – 1 )

while [ $num -gt 10 ]
do
temp=$( expr $num % 10 )
num=$( expr $num / 10);
echo “Digit($i) => $temp”
date_array[$i]=”${temp}”
i=$(expr $i – 1)
done
echo “Digit($i) => $num”
date_array[$i]=”${num}”

echo ${date_array[*]}

——————————————————–
Program (17) ~ string-operation.sh
——————————————————–

#! /bin/bash

echo “Arun Bagul:-”
string=”/root/arun/bagul/image.gif”
echo “string=> $string”
echo “String=> ${string##/*/}”
echo “String=> ${string#/*/}”

echo “String=> ${string%.*}”
echo “String=> ${string%%.*}”
#str=”/home/y/conf/arunbagul/daily_market_0.conf”
str=”${str##/*conf/}”
echo “String=> ${str%/*}”

#done

mystr=”keyword_summary_exact_arunsb”
echo $mystr
echo ${mystr%_*}

echo “$*”

—————————————–
Program (18) ~ switch.sh
—————————————–

#!/bin/bash

echo ” Switch program | arg1 => $1″
echo ” ——————————-”
case $1 in

123)
echo “Case is 123”
;;

arun)
echo “Case is ‘arun'”
;;

pri*)

echo “Case is ‘pri*'”
;;

*)
echo ” * Usage: $0 ”
echo ” Default case (nothing is matched)”
exit 0;
;;
esac

—————————————————————–
Program (19) ~ while_loop_example-1.sh
——————————————————————

#!/bin/bash

mywait=wait

while [ “${mywait}” = “wait” ]
do
echo “arun”
done

——————————————————————-
Program (20) ~ while_loop_example-2.sh
——————————————————————–

#! /bin/bash

## on command line -> i=0 && while [ $i -le 10 ] ; do echo $i; i=$(expr $i + 1); done

i=0
while [ $i -le 10 ]
do
echo $i
i=$(expr $i + 1)
done

——————————————————————–

* Please download PDF file http://www.slideshare.net/arunbagul/bash-learning-by-examples/

Regards,
Arun

How to configure multi master MySQL replication

How to configure multi master MySQL replication

Introduction ~

I was planning to write article on Multi Master MySQL replication since long time; Finally started now!. Please refer the the article on “How to configure MySQL replication with one Master” URL ~ http://www.indiangnu.org/2007/mysql-replication-one-master-multiple-slave/

* Let me inform you all that Multi Master replication in MySQL is purely based on following two variables. It has nothing to do with replication technology used in MySQL replication….

mysql> show variables  like  ‘%increment_%’;

+—————————————+——-+
| Variable_name                                   | Value |
+—————————————+——-+
| auto_increment_increment    |  1    |
| auto_increment_offset               |  1    |
+—————————————+—–+
2 rows in set (0.00 sec)

mysql>

** Requirements ~

a) Master Hosts (2 master in my case) ~

master-1 => 10.66.66.194
master-2 => 10.66.90.135
b) Replication Slave (1 slave) ~
slave => 10.66.75.137

c) MySQL server (with replication support)

** Let us understand how it works ?


* Master-1 Server =>

Set following variables…

mysql> set auto_increment_increment=5;
mysql> set auto_increment_offset=1;

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 2     |
| auto_increment_offset    | 1     |
+————————–+——-+
2 rows in set (0.00 sec)

mysql>

** Create Table ~

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

** Add Record now ~

mysql> INSERT INTO class VALUES (”,’Arun Bagul’);
mysql> INSERT INTO class VALUES (”,’Ravi Bhure’);
mysql> INSERT INTO class VALUES (”,’Karthik Appigita’);
mysql> INSERT INTO class VALUES (”,’Ameya Pandit’);

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      3 | Ravi Bhure       |
|      5 | Karthik Appigita |
|      7 | Ameya Pandit     |
+——–+——————+
4 rows in set (0.00 sec)

mysql>

* Master-2 Server =>

Set following variables…

mysql> set auto_increment_increment=2;
mysql> set auto_increment_offset=2;

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 2     |
| auto_increment_offset    | 2     |
+————————–+——-+
2 rows in set (0.00 sec)

mysql>

** Create Table ~

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

** Add Record now ~

mysql> INSERT INTO class VALUES (”,’Nilkanth Parab’);
mysql> INSERT INTO class VALUES (”,’Nishit Shah’);
mysql> INSERT INTO class VALUES (”,’Ram Krishna’);
mysql> INSERT INTO class VALUES (”,’Suhail Thakur’);

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      2 | Nilkanth Parab   |
|      4 | Nishit Shah      |
|      6 | Ram Krishna      |
|      8 | Suhail Thakur    |
+——–+——————+
4 rows in set (0.00 sec)

mysql>

** What is the importance of “auto_increment_increment” and “auto_increment_offset” ~

mysql> desc class;
+——–+————-+——+—–+———+—————-+
| Field  | Type        | Null | Key | Default | Extra          |
+——–+————-+——+—–+———+—————-+
| rollno | int(5)      | NO   | PRI | NULL    | auto_increment |
| name   | varchar(30) | YES  |     | NULL    |                |
+——–+————-+——+—–+———+—————-+

auto_increment_offset => This is BASE value for column with “auto_increment” attribute (please refer the above example)
auto_increment_increment => This is the increment value for column with “auto_increment” attribute

** If you combine the both tables (master-1 and master-2) the final table will look like this ~

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      2 | Nilkanth Parab   |
|      3 | Ravi Bhure       |
|      4 | Nishit Shah      |
|      5 | Karthik Appigita |
|      6 | Ram Krishna      |
|      7 | Ameya Pandit     |
|      8 | Suhail Thakur    |
+——–+——————+
8 rows in set (0.00 sec)

mysql>

** This is how Multi master replication works….

auto_increment_offset=Nth master server
auto_increment_increment=M

Where –
N => nth number of master server (on master-1 keep it 1 and on master-2 keep it 2 and so on..)
M => Total number of Master Server (2 in our case but better to keep this value high so that we can add new master server easily)

log-slave-updates => Slave server does not log to its own binary log any updates that are received from a Master server. This option tells the slave to log the updates performed by its SQL thread to its own binary log.

** Make sure that MySQL is running and up on all master servers and slave server-

How to setup Multi Master MySQL replication ? –

Step 1] Create Database/Tables on  Master  & Slave Servers –

You can create DB on all master & slave server or  create on one server and export that DB on  rest of all servers…

Master-1 => Create DB and Table

mysql> create database student;

mysql> use student;

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

mysql> show tables;
+——————-+
| Tables_in_student |
+——————-+
| class             |
+——————-+
1 row in set (0.00 sec)

mysql> desc class;
+——–+————-+——+—–+———+—————-+
| Field  | Type        | Null | Key | Default         |      Extra          |
+——–+————-+——+—–+———+—————-+
| rollno | int(5)      | NO   | PRI | NULL    | auto_increment |
| name   | varchar(30) | YES  |     | NULL    |                |
+——–+————-+——+—–+———+—————-+
2 rows in set (0.00 sec)

mysql> SELECT * FROM class;
Empty set (0.00 sec)
mysql>

* Now take dump of “student” DB and export it on all master and Slave server…

[root@master-1~]# mysqldump -u root -p  -d student > /home/arunsb/student.sql

* SCP the dump file on master-2 and slave server ~
[root@master-1~]# scp /home/arunsb/student.sql arunsb@10.66.90.135:/tmp/student.sql
[root@master-1~]# scp /home/arunsb/student.sql arunsb@10.66.75.137:/tmp/student.sql

Login on master-2 and slave ~

mysql> create database student;

[root@master-2~]# mysql -u root -p student < /tmp/student.sql
Enter password:
[root@master-2~]#

[root@master-2~]# mysql -u root -p
Enter password:

mysql> use student

mysql> SELECT * FROM class;
Empty set (0.00 sec)
mysql>

** Please repeat the same steps on Slave server as well…

Step 2] Update “my.cnf” config file on master-1,master-2 and slave server –

[root@master-1~]# cat /etc/my.cnf

###########################
##MySQL replication setting

#Master setting(1)
server-id = 1
log-bin = /var/log/mysql/binary/mysql-bin.log
binlog-do-db = student
binlog-ignore-db = mysql
log = /var/log/mysql/mysql.log
auto_increment_offset=1
auto_increment_increment=5
log-slave-updates

##slave setting
master-port=3306
master-host=10.66.90.135
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
###########################

[root@master-1~]#

[root@master-2~]# cat /etc/mysql/my.cnf

###########################
##MySQL replication setting

#Master setting(2)
server-id = 2
log-bin = /var/log/mysql/binary/mysql-bin.log
binlog-do-db=student
binlog-ignore-db = mysql
log = /var/log/mysql/mysql.log
auto_increment_offset=2
auto_increment_increment=5
log-slave-updates

##slave setting
master-port=3306
master-host=10.66.66.194
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
###########################

[root@master-2~]#

* please create directory for binary log and set permission…

[root@master-1~]# mkdir -p /var/log/mysql/binary/
[root@master-1~]# chown mysql:adm  /var/log/mysql/ /var/log/mysql/binary/

[root@master-2~]# mkdir -p /var/log/mysql/binary/
[root@master-2~]# chown mysql:adm  /var/log/mysql/ /var/log/mysql/binary/

** MySQL Replication Slave ~

[root@slave~]# cat  /etc/my.cnf

[mysqld]

########################################
##slave setting
server-id=4
master-port=3306
master-host=10.66.90.135
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
########################################

[root@slave~]#


Step 3] Give Replication permission on both masters ~


** Master (1 & 2) ~

mysql> GRANT REPLICATION SLAVE ON *.* TO ‘replication’@’10.66.%.%’ IDENTIFIED BY ‘mypwd’;
Query OK, 0 rows affected (0.00 sec)

mysql>

Step 4] Restart MySQL on both master as well as replication slave server ~

** Please verify setting on master-1 and master-2 server…

* Master-1

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 5     |
| auto_increment_offset    | 1     |
+————————–+——-+
2 rows in set (0.00 sec)

* Master-2

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 5     |
| auto_increment_offset    | 2     |
+————————–+——-+
2 rows in set (0.00 sec)

** Please verify ‘master’ & ‘slave’ status on both masters(1 & 2) and slave –

mysql> show master status;
mysql> show slave status;

** Multi Master replication is started…

Step 5] Add few records on Master-1 & Master-2 server at same time ~

Add records on both master server at same time and check master and replication slave status as shown above….

** Add following on master-1

mysql> INSERT INTO class VALUES (”,’Arun Bagul’);
mysql> INSERT INTO class VALUES (”,’Ravi Bhure’);
mysql> INSERT INTO class VALUES (”,’Karthik Appigita’);
mysql> INSERT INTO class VALUES (”,’Ameya Pandit’);

** Add following on master-2

mysql> INSERT INTO class VALUES (”,’Nilkanth Parab’);
mysql> INSERT INTO class VALUES (”,’Nishit Shah’);
mysql> INSERT INTO class VALUES (”,’Ram Krishna’);
mysql> INSERT INTO class VALUES (”,’Suhail Thakur’);

** Please verify the numbers of records on both masters and slave….

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      2 | Nilkanth Parab   |
|      6 | Ravi Bhure       |
|     11 | Karthik Appigita |
|     16 | Ameya Pandit     |
|     17 | Nishit Shah      |
|     22 | Ram Krishna      |
|     27 | Suhail Thakur    |
+——–+——————+
8 rows in set (0.00 sec)

mysql>

* So we all learned to configure multi-master MySQL replication. Enjoy!!

Regards,
Arun Bagul

कैसे कॉन्फ़िगर करना है multi master MySQL replication

कैसे कॉन्फ़िगर करना है multi master MySQL replication

परिचय ~

मैं लंबे समय से multi master MySQL replication पर लेख लिखने की योजना बना रहा था, आखिरकार अब शुरू किया!  कृपया कर के “MySQL replication with one master” कॉन्फ़िगर  करने  के लिए  निम्न यूआरएल का प्रयोग  करे  ~ http://www.indiangnu.org/2007/mysql-replication-one-master-multiple-slave/

* मुझे आप सभी को सूचित  करना  चाहता हू की  “multi master MySQL replication” पूर्णतः दोन वेरियबल पर आधारित है.  इसका MySQL replication तकनीक के साथ कोई संबंध नही है…

mysql> show variables like ‘%increment_%’;
+——————————-+——-+
| Variable_name                 | Value |
+——————————-+——-+
| auto_increment_increment      | 1     |
| auto_increment_offset         | 1     |
+——————————-+——-+
2 rows in set (0.00 sec)

mysql>

** आवश्यकताओं ~

a) Master Hosts (2 master in my case) ~
master-1 => 10.66.66.194
master-2 => 10.66.90.135
b) Replication Slave (1 slave) ~
Slave => 10.66.75.137

c) MySQL server (with replication support)

** हमें समझ ना है की यह कैसे काम करता है?

* Master-1 Server =>

Set following variables…

mysql> set auto_increment_increment=5;
mysql> set auto_increment_offset=1;

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 2     |
| auto_increment_offset    | 1     |
+————————–+——-+
2 rows in set (0.00 sec)

mysql>

** Create Table ~

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

** रिकॉर्ड अभी जोड़ें  ~

mysql> INSERT INTO class VALUES (”,’Arun Bagul’);
mysql> INSERT INTO class VALUES (”,’Ravi Bhure’);
mysql> INSERT INTO class VALUES (”,’Karthik Appigita’);
mysql> INSERT INTO class VALUES (”,’Ameya Pandit’);

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      3 | Ravi Bhure       |
|      5 | Karthik Appigita |
|      7 | Ameya Pandit     |
+——–+——————+
4 rows in set (0.00 sec)

mysql>

* Master-2 Server =>

Set following variables…

mysql> set auto_increment_increment=2;
mysql> set auto_increment_offset=2;

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 2     |
| auto_increment_offset    | 2     |
+————————–+——-+
2 rows in set (0.00 sec)

mysql>

** Create Table ~

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

** Add Record now ~

mysql> INSERT INTO class VALUES (”,’Nilkanth Parab’);
mysql> INSERT INTO class VALUES (”,’Nishit Shah’);
mysql> INSERT INTO class VALUES (”,’Ram Krishna’);
mysql> INSERT INTO class VALUES (”,’Suhail Thakur’);

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      2 | Nilkanth Parab   |
|      4 | Nishit Shah      |
|      6 | Ram Krishna      |
|      8 | Suhail Thakur    |
+——–+——————+
4 rows in set (0.00 sec)

mysql>

** “auto_increment_increment” और  “auto_increment_offset” का क्या महत्व है? ~

mysql> desc class;
+——–+————-+——+—–+———+—————-+
| Field  | Type        | Null | Key | Default | Extra          |
+——–+————-+——+—–+———+—————-+
| rollno | int(5)      | NO   | PRI | NULL    | auto_increment |
| name   | varchar(30) | YES  |     | NULL    |                |
+——–+————-+——+—–+———+—————-+

auto_increment_offset    => इस ‘auto_increment “विशेषता के आधार स्तम्भ के लिए मूल्य है (कृपया ऊपर के उदाहरण देखें)
auto_increment_increment => इस स्तंभ के लिए वेतन वृद्धि “auto_increment” विशेषता के साथ मूल्य है

** यदि आप मेज दोनों गठबंधन (master-1 और master-2) अंतिम टेबल इस तरह से दिखेगा ~

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      2 | Nilkanth Parab   |
|      3 | Ravi Bhure       |
|      4 | Nishit Shah      |
|      5 | Karthik Appigita |
|      6 | Ram Krishna      |
|      7 | Ameya Pandit     |
|      8 | Suhail Thakur    |
+——–+——————+
8 rows in set (0.00 sec)

mysql>

** इस  तरह से  multi master replication काम करता है….

auto_increment_offset=Nth master server
auto_increment_increment=M

कहाँ —
N = nth number of master server (on master-1 keep it 1 and on master-2 keep it 2 and so on..)
M = Total number of Master Server (2 in our case but better to keep this value high so that we can add new master server easily)

log-slave-updates => Slave server does not log to its own binary log any updates that are received from a Master server. This option tells the slave to log the updates performed by its SQL thread to its own binary log.

** यकीन है कि MySQL चल रहा है और सभी master सर्वर और slave सर्वर पर…

कसे सेटप करना है multi master MySQL replication? –

Step 1] Create Database/Tables on  Master  & Slave Servers –

You can create DB on all master & slave server or  create on one server and export that DB on  rest of all servers…

Master-1 => Create DB and Table

mysql> create database student;

mysql> use student;

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

mysql> show tables;
+——————-+
| Tables_in_student |
+——————-+
| class             |
+——————-+
1 row in set (0.00 sec)

mysql> desc class;
+——–+————-+——+—–+———+—————-+
| Field  | Type        | Null | Key | Default | Extra          |
+——–+————-+——+—–+———+—————-+
| rollno | int(5)      | NO   | PRI | NULL    | auto_increment |
| name   | varchar(30) | YES  |     | NULL    |                |
+——–+————-+——+—–+———+—————-+
2 rows in set (0.00 sec)

mysql> SELECT * FROM class;
Empty set (0.00 sec)
mysql>

* Now take dump of “student” DB and export it on all master and Slave server…

[root@master-1~]# mysqldump -u root -p  -d student > /home/arunsb/student.sql

* SCP the dump file on master-2 and slave server ~
[root@master-1~]# scp /home/arunsb/student.sql arunsb@10.66.90.135:/tmp/student.sql
[root@master-1~]# scp /home/arunsb/student.sql arunsb@10.66.75.137:/tmp/student.sql

Login on master-2 and slave ~

mysql> create database student;

[root@master-2~]# mysql -u root -p student < /tmp/student.sql
Enter password:
[root@master-2~]#

[root@master-2~]# mysql -u root -p
Enter password:

mysql> use student

mysql> SELECT * FROM class;
Empty set (0.00 sec)
mysql>

** Please repeat the same steps on Slave server as well…

Step 2] Update “my.cnf” config file on master-1,master-2 and slave server –

[root@master-1~]# cat /etc/my.cnf

###########################
##MySQL replication setting

#Master setting(1)
server-id = 1
log-bin = /var/log/mysql/binary/mysql-bin.log
binlog-do-db = student
binlog-ignore-db = mysql
#log = /var/log/mysql/mysql.log
auto_increment_offset=1
auto_increment_increment=5
log-slave-updates

##slave setting
master-port=3306
master-host=10.66.90.135
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
###########################

[root@master-1~]#

[root@master-2~]# cat /etc/mysql/my.cnf

###########################
##MySQL replication setting

#Master setting(2)
server-id = 2
log-bin = /var/log/mysql/binary/mysql-bin.log
binlog-do-db=student
binlog-ignore-db = mysql
log = /var/log/mysql/mysql.log
auto_increment_offset=2
auto_increment_increment=5
log-slave-updates

##slave setting
master-port=3306
master-host=10.66.66.194
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
###########################

[root@master-2~]#

* please create directory for binary log and set permission…

[root@master-1~]# mkdir -p /var/log/mysql/binary/
[root@master-1~]# chown mysql:adm  /var/log/mysql/ /var/log/mysql/binary/

[root@master-2~]# mkdir -p /var/log/mysql/binary/
[root@master-2~]# chown mysql:adm  /var/log/mysql/ /var/log/mysql/binary/

** MySQL Replication Slave ~

[root@slave~]# cat  /etc/my.cnf

[mysqld]

########################################
##slave setting
server-id=4
master-port=3306
master-host=10.66.90.135
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
########################################

[root@slave~]#

Step 3] Give Replication permission on both masters ~

** Master (1 & 2) ~

mysql> GRANT REPLICATION SLAVE ON *.* TO ‘replication’@’10.66.%.%’ IDENTIFIED BY ‘mypwd’;
Query OK, 0 rows affected (0.00 sec)

mysql>

Step 4] Restart MySQL on both master as well as replication slave server ~

** Please verify setting on master-1 and master-2 server…

* Master-1

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 5     |
| auto_increment_offset    | 1     |
+————————–+——-+
2 rows in set (0.00 sec)

* Master-2

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 5     |
| auto_increment_offset    | 2     |
+————————–+——-+
2 rows in set (0.00 sec)

** Please verify ‘master’ & ‘slave’ status on both masters(1 & 2) and slave –

mysql> show master status;
mysql> show slave status;

** Multi Master replication is started…

Step 5] Add few records on Master-1 & Master-2 server at same time ~

Add records on both master server at same time and check master and replication slave status as shown above….

** Add following on master-1

mysql> INSERT INTO class VALUES (”,’Arun Bagul’);
mysql> INSERT INTO class VALUES (”,’Ravi Bhure’);
mysql> INSERT INTO class VALUES (”,’Karthik Appigita’);
mysql> INSERT INTO class VALUES (”,’Ameya Pandit’);

** Add following on master-2

mysql> INSERT INTO class VALUES (”,’Nilkanth Parab’);
mysql> INSERT INTO class VALUES (”,’Nishit Shah’);
mysql> INSERT INTO class VALUES (”,’Ram Krishna’);
mysql> INSERT INTO class VALUES (”,’Suhail Thakur’);

** Please verify the numbers of records on both masters and slave….

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      2 | Nilkanth Parab   |
|      6 | Ravi Bhure       |
|     11 | Karthik Appigita |
|     16 | Ameya Pandit     |
|     17 | Nishit Shah      |
|     22 | Ram Krishna      |
|     27 | Suhail Thakur    |
+——–+——————+
8 rows in set (0.00 sec)

mysql>

* तो हम सब विन्यस्त बहु मास्टर MySQL प्रतिकृति को सीखा. आनंद लीजिए!!

सादर,
Arun Bagul

วิธี การ กำหนด ค่า หลาย master ซ้ำ MySQL

วิธี การ กำหนด ค่า หลาย master ซ้ำ MySQL

Introduction ~

ฉัน วางแผน ที่ จะ เขียน บทความ เกี่ยว กับ การ ทำ ซ้ำ Multi Master MySQL ตั้งแต่ เวลา ยาว ที่สุด เริ่ม ต้น วัน นี้. โปรด ดู ที่ บทความ เรื่อง “วิธี การ กำหนด ค่า ซ้ำ MySQL กับ Master” URL ~ http://www.indiangnu.org/2007/mysql-replication-one-master-multiple-slave/

* ให้ ฉัน ทราบ สิ่ง ที่ จำลอง แบบ Multi Master ใน MySQL จะ ขึ้น ต่อ ไป อย่าง บริสุทธิ์ ใน สอง ตัวแปร. มี อะไร จะ ทำ อย่างไร กับ เทคโนโลยี การ ทำ ซ้ำ ใช้ ใน การ ทำ ซ้ำ MySQL ….

mysql> show variables like ‘%increment_%’;
+——————————-+——-+
| Variable_name                 | Value |
+——————————-+——-+
| auto_increment_increment      | 1     |
| auto_increment_offset         | 1     |
+——————————-+——-+
2 rows in set (0.00 sec)

mysql>

** Requirements ~

a) Master Hosts (2 master in my case) ~
master-1 => 10.66.66.194
master-2 => 10.66.90.135
b) Replication Slave (1 slave) ~
Slave => 10.66.75.137

c) MySQL server (with replication support)

** Let us understand how it works ?

* Master-1 Server =>

Set following variables…

mysql> set auto_increment_increment=5;
mysql> set auto_increment_offset=1;

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 2     |
| auto_increment_offset    | 1     |
+————————–+——-+
2 rows in set (0.00 sec)

mysql>

** Create Table ~

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

** Add Record now ~

mysql> INSERT INTO class VALUES (”,’Arun Bagul’);
mysql> INSERT INTO class VALUES (”,’Ravi Bhure’);
mysql> INSERT INTO class VALUES (”,’Karthik Appigita’);
mysql> INSERT INTO class VALUES (”,’Ameya Pandit’);

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      3 | Ravi Bhure       |
|      5 | Karthik Appigita |
|      7 | Ameya Pandit     |
+——–+——————+
4 rows in set (0.00 sec)

mysql>

* Master-2 Server =>

Set following variables…

mysql> set auto_increment_increment=2;
mysql> set auto_increment_offset=2;

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 2     |
| auto_increment_offset    | 2     |
+————————–+——-+
2 rows in set (0.00 sec)

mysql>

** Create Table ~

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

** Add Record now ~

mysql> INSERT INTO class VALUES (”,’Nilkanth Parab’);
mysql> INSERT INTO class VALUES (”,’Nishit Shah’);
mysql> INSERT INTO class VALUES (”,’Ram Krishna’);
mysql> INSERT INTO class VALUES (”,’Suhail Thakur’);

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      2 | Nilkanth Parab   |
|      4 | Nishit Shah      |
|      6 | Ram Krishna      |
|      8 | Suhail Thakur    |
+——–+——————+
4 rows in set (0.00 sec)

mysql>

** What is the importance of “auto_increment_increment” and “auto_increment_offset” ~

mysql> desc class;
+——–+————-+——+—–+———+—————-+
| Field  | Type        | Null | Key | Default | Extra          |
+——–+————-+——+—–+———+—————-+
| rollno | int(5)      | NO   | PRI | NULL    | auto_increment |
| name   | varchar(30) | YES  |     | NULL    |                |
+——–+————-+——+—–+———+—————-+

auto_increment_offset    => This is BASE value for column with “auto_increment” attribute (please refer the above example)
auto_increment_increment => This is the increment value for column with “auto_increment” attribute

** If you combine the both tables (master-1 and master-2) the final table will look like this ~

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      2 | Nilkanth Parab   |
|      3 | Ravi Bhure       |
|      4 | Nishit Shah      |
|      5 | Karthik Appigita |
|      6 | Ram Krishna      |
|      7 | Ameya Pandit     |
|      8 | Suhail Thakur    |
+——–+——————+
8 rows in set (0.00 sec)

mysql>

** This is how Multi master replication works….

auto_increment_offset=Nth master server
auto_increment_increment=M

ที่ —
N => หมายเลข nth ของ เซิร์ฟเวอร์ หลัก (ใน master-1 เก็บ ไว้ 1 และ master-2 ให้ เป็น 2 เป็นต้น ..)
M => จำนวน Master Server (2 ใน กรณี ของ เรา แต่ ดี กว่า เพื่อ ให้ ค่า นี้ สูง เพื่อ ให้ เรา สามารถ เพิ่ม เซิร์ฟเวอร์ หลัก ใหม่ ได้ อย่าง ง่ายดาย)

ปรับปรุง log-ทาส-=> server ทาส ไม่ เข้า กับ ตัว เอง binary log ของ การ ปรับปรุง ใด ๆ ที่ จะ ได้ รับ จาก เซิร์ฟเวอร์ Master. ตัว เลือก นี้ จะ บอก ทาส เพื่อ เข้า สู่ การ ปรับปรุง โดย ด้าย SQL เพื่อ เข้า สู่ ระบบ ไบนารี ของ ตนเอง.

** โปรด ตรวจ สอบ ว่า MySQL ทำงาน และ มี ขึ้น ใน ทุก เซิร์ฟเวอร์ หลัก และ เซิร์ฟเวอร์ ทาส –

วิธี การ ติด ตั้ง ซ้ำ Multi Master MySQL? —

Step 1] Create Database/Tables on  Master  & Slave Servers –

You can create DB on all master & slave server or  create on one server and export that DB on  rest of all servers…

Master-1 => Create DB and Table

mysql> create database student;

mysql> use student;

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

mysql> show tables;
+——————-+
| Tables_in_student |
+——————-+
| class             |
+——————-+
1 row in set (0.00 sec)

mysql> desc class;
+——–+————-+——+—–+———+—————-+
| Field  | Type        | Null | Key | Default | Extra          |
+——–+————-+——+—–+———+—————-+
| rollno | int(5)      | NO   | PRI | NULL    | auto_increment |
| name   | varchar(30) | YES  |     | NULL    |                |
+——–+————-+——+—–+———+—————-+
2 rows in set (0.00 sec)

mysql> SELECT * FROM class;
Empty set (0.00 sec)
mysql>

* Now take dump of “student” DB and export it on all master and Slave server…

[root@master-1~]# mysqldump -u root -p  -d student > /home/arunsb/student.sql

* SCP the dump file on master-2 and slave server ~
[root@master-1~]# scp /home/arunsb/student.sql arunsb@10.66.90.135:/tmp/student.sql
[root@master-1~]# scp /home/arunsb/student.sql arunsb@10.66.75.137:/tmp/student.sql

Login on master-2 and slave ~

mysql> create database student;

[root@master-2~]# mysql -u root -p student < /tmp/student.sql
Enter password:
[root@master-2~]#

[root@master-2~]# mysql -u root -p
Enter password:

mysql> use student

mysql> SELECT * FROM class;
Empty set (0.00 sec)
mysql>

** Please repeat the same steps on Slave server as well…

Step 2] Update “my.cnf” config file on master-1,master-2 and slave server –

[root@master-1~]# cat /etc/my.cnf

###########################
##MySQL replication setting

#Master setting(1)
server-id = 1
log-bin = /var/log/mysql/binary/mysql-bin.log
binlog-do-db = student
binlog-ignore-db = mysql
#log = /var/log/mysql/mysql.log
auto_increment_offset=1
auto_increment_increment=5
log-slave-updates

##slave setting
master-port=3306
master-host=10.66.90.135
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
###########################

[root@master-1~]#

[root@master-2~]# cat /etc/mysql/my.cnf

###########################
##MySQL replication setting

#Master setting(2)
server-id = 2
log-bin = /var/log/mysql/binary/mysql-bin.log
binlog-do-db=student
binlog-ignore-db = mysql
log = /var/log/mysql/mysql.log
auto_increment_offset=2
auto_increment_increment=5
log-slave-updates

##slave setting
master-port=3306
master-host=10.66.66.194
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
###########################

[root@master-2~]#

* please create directory for binary log and set permission…

[root@master-1~]# mkdir -p /var/log/mysql/binary/
[root@master-1~]# chown mysql:adm  /var/log/mysql/ /var/log/mysql/binary/

[root@master-2~]# mkdir -p /var/log/mysql/binary/
[root@master-2~]# chown mysql:adm  /var/log/mysql/ /var/log/mysql/binary/

** MySQL Replication Slave ~

[root@slave~]# cat  /etc/my.cnf

[mysqld]

########################################
##slave setting
server-id=4
master-port=3306
master-host=10.66.90.135
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
########################################

[root@slave~]#

Step 3] Give Replication permission on both masters ~

** Master (1 & 2) ~

mysql> GRANT REPLICATION SLAVE ON *.* TO ‘replication’@’10.66.%.%’ IDENTIFIED BY ‘mypwd’;
Query OK, 0 rows affected (0.00 sec)

mysql>

Step 4] Restart MySQL on both master as well as replication slave server ~

** Please verify setting on master-1 and master-2 server…

* Master-1

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 5     |
| auto_increment_offset    | 1     |
+————————–+——-+
2 rows in set (0.00 sec)

* Master-2

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 5     |
| auto_increment_offset    | 2     |
+————————–+——-+
2 rows in set (0.00 sec)

** Please verify ‘master’ & ‘slave’ status on both masters(1 & 2) and slave –

mysql> show master status;
mysql> show slave status;

** Multi Master replication is started…

Step 5] Add few records on Master-1 & Master-2 server at same time ~

Add records on both master server at same time and check master and replication slave status as shown above….

** Add following on master-1

mysql> INSERT INTO class VALUES (”,’Arun Bagul’);
mysql> INSERT INTO class VALUES (”,’Ravi Bhure’);
mysql> INSERT INTO class VALUES (”,’Karthik Appigita’);
mysql> INSERT INTO class VALUES (”,’Ameya Pandit’);

** Add following on master-2

mysql> INSERT INTO class VALUES (”,’Nilkanth Parab’);
mysql> INSERT INTO class VALUES (”,’Nishit Shah’);
mysql> INSERT INTO class VALUES (”,’Ram Krishna’);
mysql> INSERT INTO class VALUES (”,’Suhail Thakur’);

** Please verify the numbers of records on both masters and slave….

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      2 | Nilkanth Parab   |
|      6 | Ravi Bhure       |
|     11 | Karthik Appigita |
|     16 | Ameya Pandit     |
|     17 | Nishit Shah      |
|     22 | Ram Krishna      |
|     27 | Suhail Thakur    |
+——–+——————+
8 rows in set (0.00 sec)

mysql>

* ดังนั้น เรา ทุก คน เรียน รู้ เพื่อ multi-กำหนด ค่า ซ้ำ MySQL master. สนุก!!

ความ นับถือ,
Arun Bagul

을 구성하는 방법 MySQL은 다중 마스터 복제

을 구성하는 방법 MySQL은 다중 마스터 복제

소개 ~

나도 오랫만에 MySQL은 다중 마스터 복제에 대한 글을 쓰기로 계획했다; 마지막으로 지금 시작하십시오!. 제발 “을 구성하는 방법 하나의 마스터”URL과 MySQL이 복제 문서를 참조하십시오 ~ http://www.indiangnu.org/2007/mysql-replication-one-master-multiple-slave/

* 내 모든 MySQL에 다중 마스터 복제 순수에 대한 다음과 같은 두 개의 변수를 기반으로 알려 보자. 그것은 복제 기술을 MySQL의 복제에 사용되는 상관 없어요 ….

mysql> show variables like ‘%increment_%’;
+——————————-+——-+
| Variable_name                 | Value |
+——————————-+——-+
| auto_increment_increment      | 1     |
| auto_increment_offset         | 1     |
+——————————-+——-+
2 rows in set (0.00 sec)

mysql>

** Requirements ~

a) Master Hosts (2 master in my case) ~
master-1 => 10.66.66.194
master-2 => 10.66.90.135
b) Replication Slave (1 slave) ~
Slave => 10.66.75.137

c) MySQL server (with replication support)

** Let us understand how it works ?

* Master-1 Server =>

Set following variables…

mysql> set auto_increment_increment=5;
mysql> set auto_increment_offset=1;

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 2     |
| auto_increment_offset    | 1     |
+————————–+——-+
2 rows in set (0.00 sec)

mysql>

** Create Table ~

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

** Add Record now ~

mysql> INSERT INTO class VALUES (”,’Arun Bagul’);
mysql> INSERT INTO class VALUES (”,’Ravi Bhure’);
mysql> INSERT INTO class VALUES (”,’Karthik Appigita’);
mysql> INSERT INTO class VALUES (”,’Ameya Pandit’);

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      3 | Ravi Bhure       |
|      5 | Karthik Appigita |
|      7 | Ameya Pandit     |
+——–+——————+
4 rows in set (0.00 sec)

mysql>

* Master-2 Server =>

Set following variables…

mysql> set auto_increment_increment=2;
mysql> set auto_increment_offset=2;

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 2     |
| auto_increment_offset    | 2     |
+————————–+——-+
2 rows in set (0.00 sec)

mysql>

** Create Table ~

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

** Add Record now ~

mysql> INSERT INTO class VALUES (”,’Nilkanth Parab’);
mysql> INSERT INTO class VALUES (”,’Nishit Shah’);
mysql> INSERT INTO class VALUES (”,’Ram Krishna’);
mysql> INSERT INTO class VALUES (”,’Suhail Thakur’);

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      2 | Nilkanth Parab   |
|      4 | Nishit Shah      |
|      6 | Ram Krishna      |
|      8 | Suhail Thakur    |
+——–+——————+
4 rows in set (0.00 sec)

mysql>

** What is the importance of “auto_increment_increment” and “auto_increment_offset” ~

mysql> desc class;
+——–+————-+——+—–+———+—————-+
| Field  | Type        | Null | Key | Default | Extra          |
+——–+————-+——+—–+———+—————-+
| rollno | int(5)      | NO   | PRI | NULL    | auto_increment |
| name   | varchar(30) | YES  |     | NULL    |                |
+——–+————-+——+—–+———+—————-+

auto_increment_offset    => This is BASE value for column with “auto_increment” attribute (please refer the above example)
auto_increment_increment => This is the increment value for column with “auto_increment” attribute

** If you combine the both tables (master-1 and master-2) the final table will look like this ~

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      2 | Nilkanth Parab   |
|      3 | Ravi Bhure       |
|      4 | Nishit Shah      |
|      5 | Karthik Appigita |
|      6 | Ram Krishna      |
|      7 | Ameya Pandit     |
|      8 | Suhail Thakur    |
+——–+——————+
8 rows in set (0.00 sec)

mysql>

** This is how Multi master replication works….

auto_increment_offset=Nth master server
auto_increment_increment=M

어디로 –
마스터 서버의 N = “니은 번째 번호를 마스터 (- 1 .. 그것을 2 이렇게 계속) 그것을 1 마스터 – 2 계속
우리의 경우 마스터 서버 (2 남 = “총 숫자지만, 높은 그렇게 우리는 새 마스터 서버를 쉽게 추가할 수 있도록이 값을 계속 더 나은)

로그인 노예 업데이 트 = “슬레이브 서버는 바이너리 자체는 마스터 서버에서 수신되는 모든 업데이 트를 로그에 기록하지 않습니다. 이 옵션은 슬레이브 업데이 트를 자사는 SQL 스레드에 의해 자신의 이진 로그에 로그인할 때 수행을 알려줍니다.

**이 MySQL과 최대의 모든 마스터 서버와 슬레이브 서버에서 실행되고 있는지 확인 –

어떻게 설정 MySQL은 다중 마스터 복제? –

Step 1] Create Database/Tables on  Master  & Slave Servers –

You can create DB on all master & slave server or  create on one server and export that DB on  rest of all servers…

Master-1 => Create DB and Table

mysql> create database student;

mysql> use student;

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

mysql> show tables;
+——————-+
| Tables_in_student |
+——————-+
| class             |
+——————-+
1 row in set (0.00 sec)

mysql> desc class;
+——–+————-+——+—–+———+—————-+
| Field  | Type        | Null | Key | Default | Extra          |
+——–+————-+——+—–+———+—————-+
| rollno | int(5)      | NO   | PRI | NULL    | auto_increment |
| name   | varchar(30) | YES  |     | NULL    |                |
+——–+————-+——+—–+———+—————-+
2 rows in set (0.00 sec)

mysql> SELECT * FROM class;
Empty set (0.00 sec)
mysql>

* Now take dump of “student” DB and export it on all master and Slave server…

[root@master-1~]# mysqldump -u root -p  -d student > /home/arunsb/student.sql

* SCP the dump file on master-2 and slave server ~
[root@master-1~]# scp /home/arunsb/student.sql arunsb@10.66.90.135:/tmp/student.sql
[root@master-1~]# scp /home/arunsb/student.sql arunsb@10.66.75.137:/tmp/student.sql

Login on master-2 and slave ~

mysql> create database student;

[root@master-2~]# mysql -u root -p student < /tmp/student.sql
Enter password:
[root@master-2~]#

[root@master-2~]# mysql -u root -p
Enter password:

mysql> use student

mysql> SELECT * FROM class;
Empty set (0.00 sec)
mysql>

** Please repeat the same steps on Slave server as well…

Step 2] Update “my.cnf” config file on master-1,master-2 and slave server –

[root@master-1~]# cat /etc/my.cnf

###########################
##MySQL replication setting

#Master setting(1)
server-id = 1
log-bin = /var/log/mysql/binary/mysql-bin.log
binlog-do-db = student
binlog-ignore-db = mysql
#log = /var/log/mysql/mysql.log
auto_increment_offset=1
auto_increment_increment=5
log-slave-updates

##slave setting
master-port=3306
master-host=10.66.90.135
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
###########################

[root@master-1~]#

[root@master-2~]# cat /etc/mysql/my.cnf

###########################
##MySQL replication setting

#Master setting(2)
server-id = 2
log-bin = /var/log/mysql/binary/mysql-bin.log
binlog-do-db=student
binlog-ignore-db = mysql
log = /var/log/mysql/mysql.log
auto_increment_offset=2
auto_increment_increment=5
log-slave-updates

##slave setting
master-port=3306
master-host=10.66.66.194
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
###########################

[root@master-2~]#

* please create directory for binary log and set permission…

[root@master-1~]# mkdir -p /var/log/mysql/binary/
[root@master-1~]# chown mysql:adm  /var/log/mysql/ /var/log/mysql/binary/

[root@master-2~]# mkdir -p /var/log/mysql/binary/
[root@master-2~]# chown mysql:adm  /var/log/mysql/ /var/log/mysql/binary/

** MySQL Replication Slave ~

[root@slave~]# cat  /etc/my.cnf

[mysqld]

########################################
##slave setting
server-id=4
master-port=3306
master-host=10.66.90.135
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
########################################

[root@slave~]#

Step 3] Give Replication permission on both masters ~

** Master (1 & 2) ~

mysql> GRANT REPLICATION SLAVE ON *.* TO ‘replication’@’10.66.%.%’ IDENTIFIED BY ‘mypwd’;
Query OK, 0 rows affected (0.00 sec)

mysql>

Step 4] Restart MySQL on both master as well as replication slave server ~

** Please verify setting on master-1 and master-2 server…

* Master-1

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 5     |
| auto_increment_offset    | 1     |
+————————–+——-+
2 rows in set (0.00 sec)

* Master-2

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 5     |
| auto_increment_offset    | 2     |
+————————–+——-+
2 rows in set (0.00 sec)

** Please verify ‘master’ & ‘slave’ status on both masters(1 & 2) and slave –

mysql> show master status;
mysql> show slave status;

** Multi Master replication is started…

Step 5] Add few records on Master-1 & Master-2 server at same time ~

Add records on both master server at same time and check master and replication slave status as shown above….

** Add following on master-1

mysql> INSERT INTO class VALUES (”,’Arun Bagul’);
mysql> INSERT INTO class VALUES (”,’Ravi Bhure’);
mysql> INSERT INTO class VALUES (”,’Karthik Appigita’);
mysql> INSERT INTO class VALUES (”,’Ameya Pandit’);

** Add following on master-2

mysql> INSERT INTO class VALUES (”,’Nilkanth Parab’);
mysql> INSERT INTO class VALUES (”,’Nishit Shah’);
mysql> INSERT INTO class VALUES (”,’Ram Krishna’);
mysql> INSERT INTO class VALUES (”,’Suhail Thakur’);

** Please verify the numbers of records on both masters and slave….

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      2 | Nilkanth Parab   |
|      6 | Ravi Bhure       |
|     11 | Karthik Appigita |
|     16 | Ameya Pandit     |
|     17 | Nishit Shah      |
|     22 | Ram Krishna      |
|     27 | Suhail Thakur    |
+——–+——————+
8 rows in set (0.00 sec)

mysql>

* 그래서 우리 모두를 구성하는 다중 마스터 복제 MySQL을 배웠습니다. 즐기십시오!

안부,
Arun Bagul

Como configurar vários master replicação do MySQL

Como configurar vários master replicação do MySQL

Introdução ~

Eu estava planejando escrever artigo sobre a replicação do MySQL Multi Master desde muito tempo, finalmente começou agora!. Por favor, consulte o artigo sobre “Como configurar a replicação do MySQL com um mestre” URL ~ http://www.indiangnu.org/2007/mysql-replication-one-master-multiple-slave/

* Deixe-me informá-lo de tudo o que Multi master replicação no MySQL é puramente baseado em duas variáveis. Não tem nada a ver com a tecnologia de replicação usado em replicação do MySQL ….

mysql> show variables like ‘%increment_%’;
+——————————-+——-+
| Variable_name                 | Value |
+——————————-+——-+
| auto_increment_increment      | 1     |
| auto_increment_offset         | 1     |
+——————————-+——-+
2 rows in set (0.00 sec)

mysql>

** Requirements ~

a) Master Hosts (2 master in my case) ~
master-1 => 10.66.66.194
master-2 => 10.66.90.135
b) Replication Slave (1 slave) ~
Slave => 10.66.75.137

c) MySQL server (with replication support)

** Let us understand how it works ?

* Master-1 Server =>

Set following variables…

mysql> set auto_increment_increment=5;
mysql> set auto_increment_offset=1;

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 2     |
| auto_increment_offset    | 1     |
+————————–+——-+
2 rows in set (0.00 sec)

mysql>

** Create Table ~

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

** Add Record now ~

mysql> INSERT INTO class VALUES (”,’Arun Bagul’);
mysql> INSERT INTO class VALUES (”,’Ravi Bhure’);
mysql> INSERT INTO class VALUES (”,’Karthik Appigita’);
mysql> INSERT INTO class VALUES (”,’Ameya Pandit’);

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      3 | Ravi Bhure       |
|      5 | Karthik Appigita |
|      7 | Ameya Pandit     |
+——–+——————+
4 rows in set (0.00 sec)

mysql>

* Master-2 Server =>

Set following variables…

mysql> set auto_increment_increment=2;
mysql> set auto_increment_offset=2;

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 2     |
| auto_increment_offset    | 2     |
+————————–+——-+
2 rows in set (0.00 sec)

mysql>

** Create Table ~

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

** Add Record now ~

mysql> INSERT INTO class VALUES (”,’Nilkanth Parab’);
mysql> INSERT INTO class VALUES (”,’Nishit Shah’);
mysql> INSERT INTO class VALUES (”,’Ram Krishna’);
mysql> INSERT INTO class VALUES (”,’Suhail Thakur’);

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      2 | Nilkanth Parab   |
|      4 | Nishit Shah      |
|      6 | Ram Krishna      |
|      8 | Suhail Thakur    |
+——–+——————+
4 rows in set (0.00 sec)

mysql>

** What is the importance of “auto_increment_increment” and “auto_increment_offset” ~

mysql> desc class;
+——–+————-+——+—–+———+—————-+
| Field  | Type        | Null | Key | Default | Extra          |
+——–+————-+——+—–+———+—————-+
| rollno | int(5)      | NO   | PRI | NULL    | auto_increment |
| name   | varchar(30) | YES  |     | NULL    |                |
+——–+————-+——+—–+———+—————-+

auto_increment_offset    => This is BASE value for column with “auto_increment” attribute (please refer the above example)
auto_increment_increment => This is the increment value for column with “auto_increment” attribute

** If you combine the both tables (master-1 and master-2) the final table will look like this ~

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      2 | Nilkanth Parab   |
|      3 | Ravi Bhure       |
|      4 | Nishit Shah      |
|      5 | Karthik Appigita |
|      6 | Ram Krishna      |
|      7 | Ameya Pandit     |
|      8 | Suhail Thakur    |
+——–+——————+
8 rows in set (0.00 sec)

mysql>

** This is how Multi master replication works….

auto_increment_offset=Nth master server
auto_increment_increment=M

Onde —
N => número enésima de servidor principal (master-1 em mantê-1 e no master-2 mantê-la 2 e assim por diante ..)
M => número total de Servidor Mestre (2 no nosso caso, mas melhor para manter este alto valor, para que possamos adicionar novo servidor mestre facilmente)

log-slave-updates => Servidor Slave não registra o seu próprio log binário quaisquer actualizações que são recebidas de um servidor mestre. Esta opção diz ao slave para registrar as atualizações feitas pela thread de SQL para seu próprio log binário.

** Certifique-se que o MySQL está rodando e até em todos os servidores master e slave-servidor

Como configurar Multi Master replicação do MySQL? —

Step 1] Create Database/Tables on  Master  & Slave Servers –

You can create DB on all master & slave server or  create on one server and export that DB on  rest of all servers…

Master-1 => Create DB and Table

mysql> create database student;

mysql> use student;

mysql> create table class ( rollno INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT , name VARCHAR(30) );

mysql> show tables;
+——————-+
| Tables_in_student |
+——————-+
| class             |
+——————-+
1 row in set (0.00 sec)

mysql> desc class;
+——–+————-+——+—–+———+—————-+
| Field  | Type        | Null | Key | Default | Extra          |
+——–+————-+——+—–+———+—————-+
| rollno | int(5)      | NO   | PRI | NULL    | auto_increment |
| name   | varchar(30) | YES  |     | NULL    |                |
+——–+————-+——+—–+———+—————-+
2 rows in set (0.00 sec)

mysql> SELECT * FROM class;
Empty set (0.00 sec)
mysql>

* Now take dump of “student” DB and export it on all master and Slave server…

[root@master-1~]# mysqldump -u root -p  -d student > /home/arunsb/student.sql

* SCP the dump file on master-2 and slave server ~
[root@master-1~]# scp /home/arunsb/student.sql arunsb@10.66.90.135:/tmp/student.sql
[root@master-1~]# scp /home/arunsb/student.sql arunsb@10.66.75.137:/tmp/student.sql

Login on master-2 and slave ~

mysql> create database student;

[root@master-2~]# mysql -u root -p student < /tmp/student.sql
Enter password:
[root@master-2~]#

[root@master-2~]# mysql -u root -p
Enter password:

mysql> use student

mysql> SELECT * FROM class;
Empty set (0.00 sec)
mysql>

** Please repeat the same steps on Slave server as well…

Step 2] Update “my.cnf” config file on master-1,master-2 and slave server –

[root@master-1~]# cat /etc/my.cnf

###########################
##MySQL replication setting

#Master setting(1)
server-id = 1
log-bin = /var/log/mysql/binary/mysql-bin.log
binlog-do-db = student
binlog-ignore-db = mysql
#log = /var/log/mysql/mysql.log
auto_increment_offset=1
auto_increment_increment=5
log-slave-updates

##slave setting
master-port=3306
master-host=10.66.90.135
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
###########################

[root@master-1~]#

[root@master-2~]# cat /etc/mysql/my.cnf

###########################
##MySQL replication setting

#Master setting(2)
server-id = 2
log-bin = /var/log/mysql/binary/mysql-bin.log
binlog-do-db=student
binlog-ignore-db = mysql
log = /var/log/mysql/mysql.log
auto_increment_offset=2
auto_increment_increment=5
log-slave-updates

##slave setting
master-port=3306
master-host=10.66.66.194
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
###########################

[root@master-2~]#

* please create directory for binary log and set permission…

[root@master-1~]# mkdir -p /var/log/mysql/binary/
[root@master-1~]# chown mysql:adm  /var/log/mysql/ /var/log/mysql/binary/

[root@master-2~]# mkdir -p /var/log/mysql/binary/
[root@master-2~]# chown mysql:adm  /var/log/mysql/ /var/log/mysql/binary/

** MySQL Replication Slave ~

[root@slave~]# cat  /etc/my.cnf

[mysqld]

########################################
##slave setting
server-id=4
master-port=3306
master-host=10.66.90.135
master-user=replication
master-password=mypwd
master-connect-retry=60
replicate-do-db=student
########################################

[root@slave~]#

Step 3] Give Replication permission on both masters ~

** Master (1 & 2) ~

mysql> GRANT REPLICATION SLAVE ON *.* TO ‘replication’@’10.66.%.%’ IDENTIFIED BY ‘mypwd’;
Query OK, 0 rows affected (0.00 sec)

mysql>

Step 4] Restart MySQL on both master as well as replication slave server ~

** Please verify setting on master-1 and master-2 server…

* Master-1

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 5     |
| auto_increment_offset    | 1     |
+————————–+——-+
2 rows in set (0.00 sec)

* Master-2

mysql> show variables like ‘%increment_%’;
+————————–+——-+
| Variable_name            | Value |
+————————–+——-+
| auto_increment_increment | 5     |
| auto_increment_offset    | 2     |
+————————–+——-+
2 rows in set (0.00 sec)

** Please verify ‘master’ & ‘slave’ status on both masters(1 & 2) and slave –

mysql> show master status;
mysql> show slave status;

** Multi Master replication is started…

Step 5] Add few records on Master-1 & Master-2 server at same time ~

Add records on both master server at same time and check master and replication slave status as shown above….

** Add following on master-1

mysql> INSERT INTO class VALUES (”,’Arun Bagul’);
mysql> INSERT INTO class VALUES (”,’Ravi Bhure’);
mysql> INSERT INTO class VALUES (”,’Karthik Appigita’);
mysql> INSERT INTO class VALUES (”,’Ameya Pandit’);

** Add following on master-2

mysql> INSERT INTO class VALUES (”,’Nilkanth Parab’);
mysql> INSERT INTO class VALUES (”,’Nishit Shah’);
mysql> INSERT INTO class VALUES (”,’Ram Krishna’);
mysql> INSERT INTO class VALUES (”,’Suhail Thakur’);

** Please verify the numbers of records on both masters and slave….

mysql> SELECT * FROM class;
+——–+——————+
| rollno | name             |
+——–+——————+
|      1 | Arun Bagul       |
|      2 | Nilkanth Parab   |
|      6 | Ravi Bhure       |
|     11 | Karthik Appigita |
|     16 | Ameya Pandit     |
|     17 | Nishit Shah      |
|     22 | Ram Krishna      |
|     27 | Suhail Thakur    |
+——–+——————+
8 rows in set (0.00 sec)

mysql>

* Portanto, todos nós aprendemos a configurar multi-replicação do MySQL mestre. Aproveite!

Atenciosamente,
Arun Bagul