Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Keepalived to achieve High availability of mysql

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Description: three nodes are required, master node (202.207.178.6), slave node (202.207.178.7), test node (202.207.178.8), virtual address: 202.207.178.4. Configuration to achieve mysql database master-slave replication, and use keepalived to achieve high availability, you can achieve the master database downmachine, slave database available!

For more information on master-slave copy of mysql, please see my blog http://10927734.blog.51cto.com/10917734/1868323.

Premise:

Since it is a highly available service, first configure some basic conditions for the implementation of the highly available service.

1) the node name must be consistent with the execution result of the uname-n command

Node1:

# hostname node1

# vim / etc/sysconfig/network

HOSTNAME=node1

Node2:

# hostname node2

# vim / etc/sysconfig/network

HOSTNAME=node2

2) Node must communicate with each other through ssh

[root@node1] # ssh-keygen-t rsa-f ~ / .ssh/id_rsa-P''

[root@node1 ~] # ssh-copy-id-I. ssh / id_rsa.pub root@202.207.178.7

[root@node2] # ssh-keygen-t rsa-f ~ / .ssh/id_rsa-P''

[root@node2 ~] # ssh-copy-id-I. ssh / id_rsa.pub root@202.207.178.6

3) the time between the nodes in the cluster must be synchronized

Use the ntp server to synchronize time

Ntpdate ip (host address configured with ntp service)

4) configure local resolution:

[root@node1 ~] # vim / etc/hosts

202.207.178.6 node1

202.207.178.7 node2

[root@node1 ~] # scp / etc/hosts node2:/etc/

First, install keepalived to achieve high availability

On the primary node:

1. Install keepalived

# tar xf keepalived-1.2.1.tar.gz

# cd keepalived-1.2.1

#. / configure-with-kernel-dir=/usr/src/kernels/2.6.32-642.11.1.el6.i686/

# make & & make install

Note:

You may need to install this package:

# yum install popt-devel

/ usr/src/kernels/ is found to be empty and kernel source code needs to be installed

# yum install kernel-devel

2. Configure keepalived

1) set a variable

# DIR=/usr/local/

2) execute the following statement to place the configuration file for keepalived in the relevant directory under / etc

# cp $DIR/etc/rc.d/init.d/keepalived / etc/rc.d/init.d/

# cp $DIR/etc/sysconfig/keepalived / etc/sysconfig/ & & mkdir-p

/ etc/keepalived

# cp $DIR/sbin/keepalived / usr/sbin/

3) provide configuration files for keepalived

# cd / etc/keepalived/

# vim keepalived.conf (add the following)

! Configuration File for keepalived

Global_defs {

Notification_email {

2663154088@qq.com

}

Notification_email_from wgkgood@139.com

Smtp_server 127.0.0.1

Smtp_connect_timeout 30

Router_id LVS_DEVEL

}

# VIP1

Vrrp_instance VI_1 {

State MASTER

Interface eth0

Lvs_sync_daemon_inteface eth0

Virtual_router_id 151

Priority 100

Advert_int 5

# non-preemptive nopreempt

Authentication {

Auth_type PASS

Auth_pass 2222

}

Virtual_ipaddress {

202.207.178.4

}

}

Virtual_server 202.207.178.4 3306 {

Delay_loop 6

Lb_algo wrr

Lb_kind DR

Persistence_timeout 60

Protocol TCP

Real_server 202.207.178.6 3306 {

Weight 100

Notify_down / data/sh/mysql.sh

TCP_CHECK {

Connect_timeout 10

Nb_get_retry 3

Delay_before_retry 3

Connect_port 3306

}

}

}

4) start the service

# / etc/init.d/keepalived restart

# ps-ef | grep keepalived

At this point, it is found that the service has started!

# tail-fn 100 / var/log/messages

At this time, the screen has been scrolling all the time, but a module is missing. This module is loaded below.

# modprobe ip_vs

# / etc/init.d/keepalived restart

# tail-fn 100 / var/log/messages

Everything is fine at this time.

On the slave node:

Configure the same as the master node, just modify the following items in the configuration file:

State BACKUP

Priority 90

Real_server 202.207.178.7 3306

Second, configure related operations and test

1. Add the following two files on the master-slave node and authorize them to stop keepalived on one side's database downmachine, and switch between master and slave roles.

# vim / data/sh/mysql.sh

/ etc/init.d/keepalived stop

# chmod + x / data/sh/nginx.sh

2. Authorize the database on the master and slave nodes respectively

Mysql > grant all on fsy.* to root@'202.207.178.%' identified by '123456'

3. You can start the test at this time.

1) start another virtual machine and use this virtual machine to connect to the database

# mysql-uroot-h302.207.178.4-p

2) stop the mysql database on the primary node, and you will find that the database of the test node is still connected normally!

So far, mysql high availability has been achieved using keepalived!

Welcome to criticize and correct!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report