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

Drbd+Heartbeat+Mysql Master / Slave High availability

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

I. preparatory work

System: Centos6.5

Two hosts need to resolve each other's domain names

Primary node (Primary Node) secondary node (Secondary Node) hostname ser5.hyzc.comser6.hyzc.comIP address 192.168.2.10192.168.2.11

1. Install DRBD

Refer to blog to install http://pengjc.blog.51cto.com/9255463/1835186

two。 Install heartbeat and mysql

Install the epel extension source:

# yum-y install epel-release

Both machines have heartbeat installed

# yum-y install heartbeat*

View heartbeat's configuration file ha.cf

Before enabling Heartbeat, configure three files (if none can be created manually) after installation: ha.cf, haresources, and authkeys. These three configuration files need to be under the / etc/ha.d directory, but there are no these three files by default. You can go to the official website for these three files, or you can find these three files in the source code package in the DOC subdirectory under the source directory.

View heartbeat's configuration file ha.cf

[root@ser5 ~] # grep-v "^ #" / etc/ha.d/ha.cf

Debugfile / var/log/ha-debug # incorrect log logfile / var/log/ha-log # log logfacility local0 # this is the log for setting heartbeat Here is the Syslog keepalive 2 # heartbeat frequency deadtime 10 # time of death, if other nodes respond for 10s, then consider death warntime 5 # if after death 5s if there is no connection, the warning message will be written to the log initdead 120 # the time it takes to wait before the system starts after other nodes die Generally twice as much as deadtime udpport 694 # Communication ucast eth0 192.168.2.11 # using port 694 of udp protocol # ip auto_failback on # setting of another node whether to re-enable when the dead node returns to normal It is easy to have data inconsistencies, which is a must, otherwise the hb_standby command cannot be used later. Node ser5.hyzc.com # Node name (queried by uname-n) node ser6.hyzc.com # Node name (queried by uname-n) # ping 10.1.88.254 # ping Gateway to check the network situation (used when the network or heartbeat fails) respawn hacluster / usr/lib64/ Heartbeat/ipfail # here is the ability to configure ip binding and switching Ipfail is the program that controls ip switching. According to the 64-bit configuration of the system, the lib64,32 bit is configured to automatically switch to lib auto_failback on # (the master node will automatically switch back after the master node is restored)

2. Set the communication key between nodes

[root@ser5 ~] # grep-v "^ #" / etc/ha.d/authkeys

Auth 1

1 crc

[root@ser5 ~] # chmod 600 authkeys

3. Use haresources of heartbeat to define resources

[root@ser5 ~] # mkdir / data

[root@ser5 ~] # grep-v "^ #" / etc/ha.d/haresources

Ser5.hyzc.com IPaddr::192.168.2.15/24/eth0:1 drbddisk::r0 Filesystem::/dev/drbd0::/data::ext4

Explanation:

Hostname of the ser5.hyzc.com host node

IPaddr::192.168.2.15/24/eth0 sets virtual IP

Drbddisk::r0 management resource R0

Filesystem::/dev/drbd0::/data::ext4 performs umount and mount operations

Secondary node operation:

Change 192.168.2.11 in ha.cf to 192.168.2.10

Start the heartbeat 2.11 and 2.10 machines

# service heartbeat start

As can be seen from the results, VIP has appeared.

[root@ser5 ha.d] # ip a

1: lo: mtu 16436 qdisc noqueue state UNKNOWN

Link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

Inet 127.0.0.1/8 scope host lo

Inet6:: 1/128 scope host

Valid_lft forever preferred_lft forever

2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000

Link/ether 00:0c:29:fe:2d:ab brd ff:ff:ff:ff:ff:ff

Inet 192.168.2.10/24 brd 192.168.2.255 scope global eth0

Inet 192.168.2.15/24 brd 192.168.2.255 scope global secondary eth0:1

Inet6 fe80::20c:29ff:fefe:2dab/64 scope link

Valid_lft forever preferred_lft forever

Stop the heartbeat service of ser5 or disconnect the network cable while monitoring the DRBD status of ser6

Ser6 operation:

Watch-n 1 / etc/init.d/drbd status

If everything is all right, you can see that the state is constantly changing.

4) restore the heartbeat service of the ser5 or connect the network cable, and monitor the DRBD status of the ser6. If the normal ser5 becomes the master node again (auto_failback on decides).

Installation of mysql

Note: DMYSQL_DATADIR=/data/mysql/data/

The installation location of the mysql data directory is on drbd

# mkdir-pv / data/mysql/data/

# chown-R mysql.mysql / data

Source package installation (the production environment uses the latest stable version of the source installation)

1. Install the compilation tool cmake first

[root@ser5 Desktop] # rpm-qa gcc gcc-c++ make ncurses-devel cmake openssl-devel openss

[root@ser5 Desktop] # yum-y install gcc-c++ ncurses-devel cmake openssl-devel openss

[root@ser5] # tar-zxvf cmake-2.8.10.2.tar.gz

[root@ser5] # cd cmake-2.8.10.2

[root@ser5 cmake-2.8.10.2] #. / bootstrap-- prefix=/usr/local/cmake / / installer

[root@ser5 cmake-2.8.10.2] # make & & make install

[root@ser5 cmake-2.8.10.2] # / usr/local/cmake/bin/cmake-- version / / verify that it is installed

Cmake version 2.8.10.2

two。 Install mysql

1) check whether mysql is installed, and if so, execute the following program

[root@ser5 cmake-2.8.10.2] # rpm-Q mysql-server

[root@ser5 cmake-2.8.10.2] # service mysqld stop

[root@ser5 cmake-2.8.10.2] # chkconfig mysqld stop

[root@ser5 cmake-2.8.10.2] # rm-rf / etc/my.cnf

[root@ser5 cmake-2.8.10.2] # rpm-e-nodeps mysql-server

2) add database management users

[root@ser5 cmake-2.8.10.2] # useradd-s / sbin/nologin-M mysql / / add database management user

[root@ser5 cmake-2.8.10.2] # grep mysql / etc/passwd

Mysql:x:501:501::/home/mysql:/sbin/nologin

3) install the database

[root@ser5] # tar-zxvf mysql-5.5.13.tar.gz

[root@ser5] # cd mysql-5.5.13

[root@ser5 mysql-5.5.13] # rm-rf / usr/local/mysql/

[root@ser5 mysql-5.5.13] # mv / root/Desktop/mysql.sh. / /. Represents the current

[root@ser5 mysql-5.5.13] # sh mysql.sh

/ usr/local/cmake/bin/cmake-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-DSYSCONFDIR=/etc-DMYSQL_DATADIR=/data/mysql/data/-DMYSQL_TCP_PORT=3306-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock-DMYSQL_USER=mysql-DEXTRA_CHARSETS=all-DWITH_READLINE=1-DWITH_SSL=system-DWITH_EMBEDDED_SERVER=1-DENABLED_LOCAL_INFILE=1-DWITH_INNOBASE_STORAGE_ENGINE=1

Note: if there is an error message, install the corresponding installation package according to the error message. Be sure to clear the CMakeCache.txt in this directory, and then execute it again.

[root@ser5 mysql-5.5.13] # make & & make install

3. Initialize the authorization library

[root@ser5 mysql-5.5.13] # cd / usr/local/mysql/

[root@ser5 mysql] # ls

The bin command data directory mysql-test scripts initializes the database

[root@ser5 ~] # cd / usr/local/mysql/

[root@ser5 mysql] # chown-R mysql.mysql.

[root@ser5 mysql] #. / scripts/mysql_install_db-- user=mysql-- datadir=/data/mysql/data/ initialize the database

# cd / data/mysql/data

[root@ser5 data] # ls / / check whether initialization is successful performance_schema

Mysql performance_schema test

4. Create the main configuration file and start the database service

[root@ser5 Desktop] # cp lnmp+memcached/mysql-5.5.13/support-files/my-medium.cnf / etc/my.cnf / / main configuration template

Start the database service

[root@ser5 Desktop] # netstat-anptul | grep: 3306

[root@ser5 mysql] # cd / usr/local/mysql/bin/

[root@ser5 bin] # / mysqld_safe-- user=mysql-- datadir=/data/mysql/data/ &

Log in to database service

[root@ser5] # / usr/local/mysql/bin/mysql-hlocalhost-uroot-p

Enter password:

5. Set quick startup, which is valid on all terminals, and set the database password

# vim ~ / .bash_profile

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin/ add command search path

Export PATH

# source ~ / .bash_profile

Set the password for the database administrator to log in from this machine to 123

[root@ser5 ~] # mysqladmin-hlocalhost-uroot password "123"

# cat / etc/ld.so.conf

# ldconfig

[root@ser5 ~] # jobs

[root@ser5 ~] # kill-9% 1

[root@ser5 ~] # / mysqld_safe-- user=mysql-- datadir=/data/mysql/data/ & / / restart the service

[root@ser5] # mysql-uroot-p123

Mysql >

6.service mysql restart writes a startup script for the source code package; set it to service startup mode:

[root@ser5 ~] # cd / root/Desktop/lnmp+memcached/mysql-5.5.13/support-files/

# vim mysql.serverbasedir=/usr/local/mysql/ datadir=/data/mysql/data/

[root@ser5 support-files] # cp mysql.server / etc/init.d/mysqld / / copy script

[root@ser5 support-files] # chmod + x / etc/init.d/mysqld / / add execution permissions

[root@ser5 support-files] # pkill-9 mysql

[root@ser5 support-files] # service mysqld restart

[root@ser5 support-files] # chkconfig mysqld on

[root@ser5 support-files] # chkconfig-- list mysqld

The mysqld service supports chkconfig, but it is not referenced at any level (run "chkconfig-- add mysqld")

[root@ser5 support-files] # chkconfig-- add mysqld

[root@ser5 support-files] # chkconfig-- list mysqld

Mysqld 0: off 1: off 2: enable 3: enable 4: enable 5: enable 6: close

Ser6.hyzc.com is also installed

Modify ha.cf

Since you are now managing the Mysql, the mysqld is managed by the heartbeat (both nodes are executed)

Ser5.hyzc.com IPaddr::192.168.2.15/24/eth0:1 drbddisk::r0 Filesystem::/dev/drbd0::/data::ext4 mysqld

Restart heartbeat

# service heartbeat restart

After startup, go to mysql, set up database db, then create table t, and insert data

[root@ser5 local] # mysql-uroot-p

After startup, go to mysql, set up database db, then create table t, and insert data

[root@ser5 resource.d] # mysql

Welcome to the MySQL monitor. Commands end with; or\ g. Your MySQL connection id is 2 Server version: 5.0.95 Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or'\ h' for help. Type'\ c'to clear the current input statement. Mysql > show databases; +-+ | Database | +-+ | information_schema | | lost+found | | mysql | | test | +-+ 5 rows in set (0.04 sec) mysql > create database db Query OK, 1 row affected (0.01 sec) mysql > use db Database changed mysql > create table t (id int (10), name char (10)); Query OK, 0 rows affected (0.05 sec) mysql > insert into t values (001, "ser1"), (002, "ser2"); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql > select * from t +-+-+ | id | name | +-+-+ | 1 | ser1 | | 2 | ser2 | +-+-+ 2 rows in set (0.00 sec) mysql > COMMIT;mysql > exit Bye

Then stop heartbeat and check whether there is mysql data in other nodes (ser6)

[root@ser5 /] # service heartbeat stop

Drbd has become a slave, and drbd0 has been uninstalled from database

[root@ser5 data] # cat / proc/drbd

Version: 8.4.6 (api:1/proto:86-101)

GIT-hash: 833d830e0152d1e457fa7856e71e11248ccf3f70 build by root@ser5.hyzc.com, 2016-08-05 02:12:56

0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-

Ns:1308 nr:348 dw:1656 dr:6085 al:8 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@ser5 data] # df-hT

Filesystem Type Size Used Avail Use% Mounted on

/ dev/sda2 ext4 96G 6.0G 85G 7% /

Tmpfs tmpfs 415m 224K 415m 1% / dev/shm

/ dev/sda1 ext4 194M 34M 151M 19% / boot

/ dev/sr0 iso9660 4.2G 4.2G 0100% / misc

Check whether drbd is dominant and whether drbd0 has database loaded in ser6.

[root@ser6 Desktop] # cat / proc/drbd

Version: 8.4.6 (api:1/proto:86-101)

GIT-hash: 833d830e0152d1e457fa7856e71e11248ccf3f70 build by root@ser5.hyzc.com, 2016-08-05 02:12:56

0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-

Ns:348 nr:1308 dw:1656 dr:6097 al:6 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@ser6 Desktop] # df-h

Filesystem Size Used Avail Use% Mounted on

/ dev/sda2 96G 6.0G 85G 7% /

Tmpfs 415m 224K 415m 1% / dev/shm

/ dev/sda1 194M 34M 151M 19% / boot

/ dev/sr0 4.2G 4.2G 0100% / misc

/ dev/drbd0 20g 203m 19G 2% / data

At this point, ser6 has become dominant, and drbd0 has been mounted to database.

Enter mysql to check whether the db database and t-table have been handed over.

[root@ser6 /] # mysql

Welcome to the MySQL monitor. Commands end with; or\ g. Your MySQL connection id is 2 Server version: 5.0.95 Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or'\ h' for help. Type'\ c'to clear the current input statement. Mysql > show databases; +-+ | Database | +-+ | information_schema | | db | | mysql | | test | +-+ 6 rows in set (0.04 sec) mysql > use db Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with-A Database changed mysql > select * from t; +-+-+ | id | name | +-+-+ | 1 | ser1 | | 2 | ser2 | +-+-+ 2 rows in set (0.00 sec)

Mysql has received the data. In this way, our drbd+heartbeat+mysql has implemented a highly available mysql database.

Problems encountered:

Error 1:

Aug 14 15:01:46 ser5.hyzc.com heartbeat: [85845]: ERROR: Bad permissions on keyfile [/ etc/ha.d//authkeys], 600 recommended.

Aug 14 15:01:46 ser5.hyzc.com heartbeat: [85845]: ERROR: Authentication configuration error.

Aug 14 15:01:46 ser5.hyzc.com heartbeat: [85845]: ERROR: Configuration error, heartbeat not started.

Solution:

Chmod 600authkeys is fine.

Error 2

[root@test6 /] # service heartbeat start

Logd is already running

Starting High-Availability services:

2012/03/14_21:36:50 INFO: Resource is stopped

[FAILED]

Heartbeat [6579]: 2012/03/14_21:36:50 ERROR: Client child command [/ usr/lib/heartbeat/ipfail] is not executable

Heartbeat [6579]: 2012/03/14_21:36:50 ERROR: Heartbeat not started: configuration error.

Heartbeat [6579]: 2012/03/14_21:36:50 ERROR: Configuration error, heartbeat not started.

Solution:

If this problem occurs, first check whether your system is 32-bit or 64-bit, if it is 64-bit, then in ha.cf

Change the lib to the lib64;32 bit in respawn hacluster / usr/lib/heartbeat/ipfail.

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