Essay 15_tww

1. Write scripts to allow users to choose whether to use mysqldump or xtraback for full backup.  

PS3="please select your backup type:";
select type in mysqldump xtraback;do
case $REPLY in
1)
[ -e /data/backup ] || mkdir /data/backup
mysqldump -A --single-transaction -F |gzip > /data/backup/mysqldump_full_backup-$(date '+%Y%m%d-%H%M').sql.gz || (echo 'backup failed!' && exit 2 )
echo 'mysqldump  full-backup successfully !'
break
;;
2)
xtrabackup --backup --target-dir=/data/backup/xtra_full_backup-$(date '+%Y%m%d-%H%M') &> /dev/null || (echo 'backup failed!' && exit 2 )
echo 'xtrabackup full-backup successfully !'
break
;;
*)
echo "please input number for your backup type "                                                                                            
esac
done

image

2. Configure Mysql master-slave synchronization  

Main server configuration (192.168.47.154):
1. Modify the configuration file
vim /etc/my.cnf
    server-id=1
    log-bin=/var/lib/mysql/logbin_tao

2.systemctl restart mariadb

3. Create user
grant replication slave on *.* to'repluser'@'192.168.47.%' identified by'tao'

4. View the binary log
MariaDB [mysql]> show master logs;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| logbin_tao.000001 | 404 |
+------- ------------+-----------+
1 row in set (0.00 sec)


Configuration from the server (192.168.47.101)
1. Modify the configuration file
vim /etc/my.cnf
    server-id=2
    read-only

2.CHANGE MASTER TO
        MASTER_HOST='192.168.47.154',
        MASTER_USER='repluser',
        MASTER_PASSWORD='tao',
        MASTER_PORT=3306,
        MASTER_LOG_FILE='logbin_tao.000001,
        MASTER_LOG_POS=404;

3.show slave status\G

4.start slave;

3. Use MHA to achieve Mysql high availability

1. Preparation:

1.) 4 hosts

192.168.47.100  (maha-manager)

192.168.47.101 (main server)

192.168.47.102 (slave server)

192.168.47.103 (slave server)

2.) Time synchronization, firewall off/selinux off

2. Installation  

centos7_2 [main server 192.168.47.101]  

#1. Install the database

yum install mariadb-server -y

#2. Modify the configuration file

/etc/my.cnf

server-id=2  

log-bin  

ship_name-resolve  

systemctl restart mariadb

#3. Create an account

MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.47.%' identified by 'tao';

MariaDB [(none)]> grant all on *.* to mhauser@'192.168.47.%' identified by 'tao';

        View binary log

        MariaDB [(none)]> show master logs;

                +--------------------+-----------+

                | Log_name           | File_size |

                +--------------------+-----------+

                | mariadb-bin.000001 |       534 |

                +--------------------+-----------+

                1 row in set (0.00 sec)

#4. Install mha4mysql-node-0.56-0.el6.noarch.rpm

yum install mha*.rpm

centos7_3, centos7_4 [from the server 192.168.47.102, 192.168.47.103]  

#1. Install the database

yum install mariadb-server -y

#2. Modify the configuration file

/etc/my.cnf

server-id=3  

log-bin  

read-only  

skip_name_resolve  

relay_log_purge=0  

server-id=4  

log-bin  

read-only  

skip_name_resolve  

relay_log_purge=0  

systemctl restart mariadb

#3. Execute commands

MariaDB [(none)]> CHANGE MASTER TO

MASTER_HOST='192.168.47.101',

MASTER_USER='repluser',

MASTER_PASSWORD = 'people',

MASTER_PORT=3306,

MASTER_LOG_FILE='mariadb-bin.000001',

MASTER_LOG_POS=245;

Check status

MariaDB [(none)]> show slave status\G

start up

MariaDB [(none)]> start slave;

The slave node has a read-only attribute

MariaDB [(none)]> show variables like 'read_only';

        +---------------+-------+

        | Variable_name | Value |

        +---------------+-------+

        | read_only     | ON    |

        +---------------+-------+

        1 row in set (0.06 sec)

#4. Install mha4mysql-node-0.56-0.el6.noarch.rpm

yum install mha*.rpm

maha-manager[192.168.47.100]  

#1. Prepare documents

mha4mysql-manager-0.56-0.el6.noarch.rpm  

mha4mysql-node-0.56-0.el6.noarch.rpm

#2. Install the software

yum install mha*.rpm  [open epel source]

#3. Key-based verification

        (1)ssh-keygen

        [root@mha-manager ~]#ls -a .ssh

         .  ..  id_rsa  id_rsa.pub

        (2)ssh-copy-id 192.168.47.100 (the IP of the local computer)

        cd .ssh

        [root@mha-manager .ssh]#ls

        authorized_keys  id_rsa  id_rsa.pub  known_hosts

        (3) Copy

        scp -r .ssh 192.168.47.101:/root/

        scp -r .ssh 192.168.47.102:/root/

        scp -r .ssh 192.168.47.103:/root/

        (4) Test:

        ssh 192.168.47.101

        ssh 192.168.47.102

        ssh 192.168.47.103

#4. Create a configuration file

mkdir /etc/mha/

vim /etc/mha/app1.cnf

------------------------

[server default]

user=mhauser

password = person

manager_workdir=/data/mastermha/app1/

manager_log=/data/mastermha/app1/manager.log

remote_workdir=/data/mastermha/app1/

ssh_user=root

repl_user=repluser

repl_password = person

ping_interval=1

[server1]

hostname=192.168.47.101

candidate_master=1

[server2]

hostname=192.168.47.102

candidate_master=1

[server3]

hostname=192.168.47.103  

#5. Check

masterha_check_ssh   --conf=/etc/mha/app1.cnf

image  

masterha_check_repl  --conf=/etc/mha/app1.cnf

image  

#6. Start

masterha_manager  --conf=/etc/mha/app1.cnf

image

test

View log

image

Main server

create databases tao;

create table testlog (id int auto_increment primary key,name char(10),age int default 20);

delimiter $$

create procedure  sp_testlog()
begin 
declare i int;
set i = 1;
while i <= 100000
do  insert into testlog(name,age) values (concat('wang',i),i);
set i = i +1;
end while;
end$$

delimiter ;

call  sp_testlog;

Shut down the main service computer directly

image

View log

image

image

image

image

View information of the computer that becomes the main server: [192.168.47.102]

image

Finally, remove the read_only in the configuration file /etc/my.cnf that becomes the main server

Guess you like

Origin blog.51cto.com/14814545/2545938