MySQL+Keepalived combat

My MySQL DBA course:

    DBA MySQL Database Engineer (2021 latest edition) Course: https://edu.51cto.com/course/27002.html   Author: Zhang Yanfeng, please indicate the source

    This chapter mainly introduces the actual combat of MySQL+Keepalived high-availability architecture, and lists the detailed operation process. MySQL database will definitely be exposed in daily operation and maintenance, so playing ⑥ MySQL architecture and daily operation and maintenance is the key to our salary increase.

    DBA MySQL Database Engineer (the latest version in 2021) course introduces database-related knowledge, principles + actual combat, from the simpler to the deeper. In-depth explanation of relational database + non-relational database (MySQL, MongoDB, ES, Redis). It covers all the core knowledge points of MySQL database, principle + actual combat, and real production environment case explanation. 24 hours after-sales Q&A is provided. Teacher Zhang takes everyone to take off. Welcome everyone to come and consult.


MySQL+Keepalived configuration:

Environmental description

Prepare 4 servers

surroundings

IP

software

Description

db01 main library

192.168.43.101

mysql、keepalived

101 and 102 are master and slave

103 and 104 are master and slave

db02 slave library

192.168.43.102

mysql

db03 main library

192.168.43.103

mysql、keepalived

db04 slave library

192.168.43.104

mysql

VIP:192.168.43.200


Prepare four installed mysql servers, the operations are as follows:

1. Download and upload the software to /server/tools

mkdir -p /server/tools

cd /server/tools/

Upload package


2. Decompression software

tar zxvf mysql-5.7.31-el7-x86_64.tar.gz

mkdir /application

mv mysql-5.7.31-el7-x86_64 /application/mysql

yum remove mariadb-libs -y

useradd -s /sbin/nologin mysql

echo "export PATH=/application/mysql/bin:$PATH" >>/etc/profile

source /etc/profile

mysql -V


3. Create a data path and authorize

mkfs.xfs /dev/sdb

[root@localhost ~]# blkid

/dev/sdb: UUID="4f4bed6c-4d81-434d-918d-8148a6591d84" TYPE="xfs"

[root@localhost ~]# vi /etc/fstab

UUID="4f4bed6c-4d81-434d-918d-8148a6591d84" /data xfs defaults 0 0

mkdir /data

mount -a


4. Authorization

chown -R mysql.mysql /application/*

chown -R mysql.mysql /data


5. Initialize data (create system data)

mkdir /data/mysql/data -p

chown -R mysql.mysql /data

mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data


6. Preparation of configuration files

cat >/etc/my.cnf <<EOF

[mysqld]

user=mysql

basedir=/application/mysql

datadir=/data/mysql/data

socket=/tmp/mysql.sock

server_id=6

port=3306

[mysql]

socket=/tmp/mysql.sock

EOF


7, start the database

cat >/etc/systemd/system/mysqld.service <<EOF

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf

LimitNOFILE = 5000

EOF

systemctl restart mysqld.service

Tip: After finishing, if you are using vm, you can do a snapshot operation


1. Clean up the environment (all nodes)

mkdir /data/binlog

chown mysql.mysql /data/*

pkill mysqld

\rm -rf /data/mysql/data/*

\rm -rf /data/binlog/*


2. Prepare the configuration file

#db01:

cat > /etc/my.cnf <<EOF

[mysqld]

basedir=/application/mysql/

datadir=/data/mysql/data

socket=/tmp/mysql.sock

server_id=1

port=3306

secure-file-priv=/tmp

autocommit=0

log_bin=/data/binlog/mysql-bin

binlog_format=row

gtid-mode=on

enforce-gtid-consistency=true

log-slave-updates=1

[mysql]

prompt=db01 [\\d]>

EOF


# db02 :

cat > /etc/my.cnf <<EOF

[mysqld]

basedir=/application/mysql

datadir=/data/mysql/data

socket=/tmp/mysql.sock

server_id=2

port=3306

secure-file-priv=/tmp

autocommit=0

log_bin=/data/binlog/mysql-bin

binlog_format=row

gtid-mode=on

enforce-gtid-consistency=true

log-slave-updates=1

[mysql]

prompt=db02 [\\d]>

EOF


# db03 :

cat > /etc/my.cnf <<EOF

[mysqld]

basedir=/application/mysql

datadir=/data/mysql/data

socket=/tmp/mysql.sock

server_id=3

port=3306

secure-file-priv=/tmp

autocommit=0

log_bin=/data/binlog/mysql-bin

binlog_format=row

gtid-mode=on

enforce-gtid-consistency=true

log-slave-updates=1

[mysql]

prompt=db03 [\\d]>

EOF


#db04:

cat > /etc/my.cnf <<EOF

[mysqld]

basedir=/application/mysql

datadir=/data/mysql/data

socket=/tmp/mysql.sock

server_id=4

port=3306

secure-file-priv=/tmp

autocommit=0

log_bin=/data/binlog/mysql-bin

binlog_format=row

gtid-mode=on

enforce-gtid-consistency=true

log-slave-updates=1

[mysql]

prompt=db04 [\\d]>

EOF


3. Initialize data (all 4 units are executed)

mysqld --initialize-insecure --user=mysql --basedir=/application/mysql  --datadir=/data/mysql/data 


4. Start the database (all three are executed)

systemctl restart mysqld


5. Build master-slave

db01 <==> db03

db01 <== db02

db03 <== db04


#db01 and db03:

grant replication slave  on *.* to repl@'192.168.43.%' identified by '123';


#db01:

change master to 

master_host='192.168.43.103',

master_user='repl',

master_password='123' ,

MASTER_AUTO_POSITION=1;

start slave;


# db02 :

change master to 

master_host='192.168.43.101',

master_user='repl',

master_password='123' ,

MASTER_AUTO_POSITION=1;

start slave;


# db03 :

change master to 

master_host='192.168.43.101',

master_user='repl',

master_password='123' ,

MASTER_AUTO_POSITION=1;

start slave;


#db04:

change master to 

master_host='192.168.43.103',

master_user='repl',

master_password='123' ,

MASTER_AUTO_POSITION=1;

start slave;


6. Configure the master/scheduled Keepalived (101 and 103 machines) (configure on the master library)

101 machines:

[root@master ~]# yum -y install keepalived

[root@master ~]# vi /etc/keepalived/keepalived.conf 

! Configuration File for keepalived


global_defs {

   router_id mysql1

}


vrrp_script check_run {

    script "/etc/keepalived/check_mysql.sh"

    interval 5

}


vrrp_instance VI_1 {

    state MASTER

    interface ens33

    virtual_router_id 88

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {

        check_run

    }

    virtual_ipaddress {

        192.168.43.200

    }

}

[root@master ~]# systemctl restart keepalived


103 machines:

[root@backup ~]# yum -y install keepalived

[root@backup ~]# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived


global_defs {

   router_id mysql2

}


vrrp_script check_run {

    script "/etc/keepalived/check_mysql.sh"

    interval 5

}


vrrp_instance VI_1 {

    state MASTER

    interface ens33

    virtual_router_id 88

    priority 50

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {

        check_run

    }

    virtual_ipaddress {

        192.168.43.200

    }

}

[root@backup ~]# systemctl restart keepalived


7. Configure the mysql status detection script (the same script for the two MySQL)

[root@master ~]# vi /etc/keepalived/check_mysql.sh

#!/bin/bash

/usr/bin/mysql -uroot -e "show status" &>/dev/null

if [ $? != 0 ];then

    systemctl stop keepalived

be

[root@master ~]# chmod a+x /etc/keepalived/check_mysql.sh

[root@master ~]# systemctl restart keepalived


8. Test

C:\Users\岩峰>mysql -urepl -p123 -h 192.168.43.200

mysql> create database db1;

Query OK, 1 row affected (0.00 sec)


mysql> show databases;

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

| Database           |

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

| information_schema |

| db1 |

| mysql              |

| performance_schema |

| test               |

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

5 rows in set (0.00 sec)


[root@master ~]# systemctl stop mariadb

mysql> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| test               |

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

4 rows in set (0.00 sec)


Guess you like

Origin blog.51cto.com/12760547/2663891