Shell Road--mysql Master-Slave Automatic deployment

Source: Internet
Author: User

#!/bin/bash

#auto make install Mysql AB relication

#by author Max

#date 2017-05-04

mysql_soft= "MySQL mysql-server mysql-devel php-mysql"

num= ' rpm-qa |grep-i mysql |wc-l '

Code=$?


#Mysql to Install 2017

If [$NUM-ne 0];then

ECHO-E "\033[32mthis server MySQL already install.\033[0m"

Read-p "Please ensure yum remove MySQL server, Yes or no[y/n]": INPUT

if [$INPUT = = "Y"];then

Yum Remove $MYSQL _soft-y

Rm-rf/var/lib/mysql/etc/my.cnf

Yum-y Install $MYSQL _soft

Else

Exit 0

Fi

Else

Rm-rf/var/lib/mysql

Yum-y Install $MYSQL _soft

If [$CODE-eq 0];then

Echo-e "\033[32mthe MySQL install successfully.\033[0m"

Exit 1

Fi

Fi


#mysql to start and config

Cat >/etc/my.cnf<<eof

[Mysqld]

Datadir=/car/lib/mysql

Socket=/var/lib/mysql/mysql.sock

User=mysql

Symbpolic-links=0

Log-bin=mysql-bin

Server-id=1

Auto_increment_offset=1

auto_increment_increment=2

[Mysql_safe]

Log-error=/var/log/mysqld.log

Pid-file=/var/run/mysql/mysqld.pid

Replicate-do-db=all

Eof

/etc/init.d/mysqld restart

Ps-ef |grep MySQL


function Mysql_config () {


#master config MySQL

MYSQL-E "Grant replication Slave on * * to ' max ' @ '% ' identified by ' Zhaomiao ';"

Master_file= ' mysql-e ' show master status; | Tail-1|awk ' {print '} '

Master_pos= ' mysql-e ' show master status; | Tail-1|awk ' {print $} '

Master_ipaddr= ' ifconfig eth0 |grep "bcast" |awk ' {print $} ' |cut-d:-f2 '

Read-p "Please enter input slave IP address": slave_ipaddr


#slave config MySQL

Ssh-l root $SLAVE _ipaddr "sed-i ' S#server-id = 1#server-id = 2#g '/etc/my.cnf"

Ssh-l root $SLAVE _ipaddr "/etc/init.d/mysqld Restart"

Ssh-l root $SLAVE _ipaddr "mysql-e \" Change Master to master_host= ' $MASTER _ipaddr ', master_user= ' Max ', master_password= ' Zhaomiao ', master_log_file= ' $MASTER _file ', master_log_pos= $MASTER _pos;\ ""

Ssh-l root $SLAVE _ipaddr "mysql-e \" SLAVE start;\ ""

Ssh-l root $SLAVE _ipaddr "mysql-e \" show SLAVE status\g;\ ""

}


Read-p "Ensure your server is master?" Yes or no[y/n] ": INPUT

if [$INPUT = = "Y"];then

Mysql_config

Else

Exit 0

Fi


Shell Road--mysql Master-Slave Automatic deployment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.