声明:该方案不能进行主从自由切换,如果要在主节点当机在恢复使用必须时候手动来操作,首先要进行同步数据,然后在做同步复制,最后在切换!这一切操作必须人为干预!
应用需求:
双机热备提供备份,冗余功能
安装环境:
NODE1 主机名 master IP地址 10.10.10.101
NODE2 主机名 slave IP地址 10.10.10.102
VIA IP(漂移IP) 10.10.10.100
NODE1为主节点,NODE2为从节点,同步的数据库名fire9
在安装之前请确认下面的安装包不存在
rpm -e mysql-devel-4.1.20-1.RHEL4.1
rpm -e mysql-bench-4.1.20-1.RHEL4.1
rpm -e php-mysql-4.3.9-3.15
rpm -e libdbi-dbd-mysql-0.6.5-10.RHEL4.1
rpm -e mod_auth_mysql-2.6.1-2.2
rpm -e mysql-server-4.1.20-1.RHEL4.1
rpm -e MySQL-python-1.0.0-1.RHEL4.1.i386
rpm -e MyODBC-2.50.39-21.RHEL4.1.i386
rpm -e qt-MySQL-3.3.3-9.3.i386
rpm -e mysqlclient10-devel-3.23.58-4.RHEL4.1.i386
rpm -e mysqlclient10-3.23.58-4.RHEL4.1
rpm -e cyrus-sasl-sql-2.1.19-5.EL4.i386
rpm -e perl-DBD-MySQL-2.9004-3.1.i386
rpm -e mysql-4.1.20-1.RHEL4.1
安装准备:我已经把相关的软件和配置文件都放在工具包里面了
redhat as 4 update4 32位
mysql-5.0.45-linux-i686-icc-glibc23.tar.gz
libnet-1.1.2.1-1.rh.el.um.1.i386.rpm
heartbeat-pils-2.0.4-1.el4.i386.rpm
heartbeat-stonith-2.0.4-1.el4.i386.rpm
heartbeat-2.0.4-1.el4.i386
perl-5.8.8.tar.gz
DBI-1.59.tar.gz
DBD-mysql-4.005.tar.gz
Time-HiRes-01.20.tar.gz
Period-1.20.tar.gz
Convert-BER-1.31.tar.gz
Mon-0.11.tar.gz
mon-0.99.3-47.tar.gz
一、安装MYSQL 主从都要做
# tar zxvf mysql-5.0.45-linux-i686-icc-glibc23.tar.gz -C /usr/local/
# cd /usr/local/
# mv mysql-5.0.45-linux-i686-icc-glibc23 mysql
# cd mysql
# groupadd mysql
# useradd -g mysql mysql
#passwd mysql
# ./scripts/mysql_install_db --user=mysql
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# /etc/rc.d/init.d/mysqld start
把提供的MY.CNF文件拷贝主机的/etc/目录下,根据下面的提示修改所需要的参数
把提供的MY.CNF文件拷贝从机的/etc/目录下,根据下面的提示修改所需要的参数
主机和从机一样进行操作:vi /etc/my.cnf
从机需要注意的是关闭server-id =1 打开server-id = 2;关闭log-bin=mysql-bin和binlog-do-db=fire9这两个参数;主机打开server-id =1 关闭server-id =2;打开log-bin=mysql-bin 和打开replicate-do-db=fire9 。(fire9指的是需要主从备份的数据库)
mysql主从复制的配置
在主机上操作
# mysql
Mysql> create database fire9; #从机也需要建立一样的数据库
Mysql>set password for root@localhost = password (‘123456’); #给ROOT用户建立密码
Mysql>flush privileges;
Mysql>GRANT replication slave ON fire9.* TO slave@10.10.10.102 IDENTIFIED BY 'password';
# mysqldump -uroot -p fire9 > fire9.sql
# scp fire9.sql root@10.10.10.102:/tmp
在从机上操作
导入主机的数据库
Mysql> create database fire9; #从机需要建立与主机一样的数据库
# mysql -uroot -p hipiao < /tmp/fire9.sql
登入数据库操作
Mysql>set password for root@localhost = password (‘123456’); #给ROOT用户建立密码
Mysql>flush privileges;
Mysql> CHANGE MASTER TO
MASTER_HOST = '10.10.10.101',
MASTER_PORT = 3306,
MSTER_USER = 'slave',---------------- 此帐号和密码是在主服务器上建立一个复制帐号
MASTER_PASSWORD = 'password',
MASTER_LOG_FILE = 'mysql-bin.000001',------- 这个在主机上通过
Mysql>show master status;命令获得
MASTER_LOG_POS = 98;---------------- 这个在主机上通过
Mysql>show master status;命令获得
Mysql> Slave start;
Mysql>show slave statusG;
检查一下Master_Log_File 和Master_Log_Pos是不是和主机的一样同时下面两个必须都为YES才对。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
二、HEARTBEAT软件包安装---------主从都需要作
# rpm -ivh libnet-1.1.2.1-1.rh.el.um.1.i386.rpm
# rpm -ivh heartbeat-pils-2.0.4-1.el4.i386.rpm
#rpm -ivh heartbeat-stonith-2.0.4-1.el4.i386.rpm
# rpm -ivh heartbeat-2.0.4-1.el4.i386.rpm
配置PERL环境
# rpm -e perl-DBI-1.40-8
# rm -rf /usr/lib/perl
# rm -rf /usr/lib/perl5
# rm -rf /usr/bin/perl*
# rm -rf /usr/share/man/man1/perl*
# rm -rf /usr/local/bin/perl*
# tar zxvf perl-5.8.8.tar.gz -C /usr/lib/
#cd /usr/lib
# mv perl-5.8.8 perl
# cd perl
# ./Configure -de
# make
# make test
# make install
# tar zxvf DBI-1.59.tar.gz
#cd DBI-1.59
# perl Makefile.PL
# make
# make test
# make install
# tar zxvf DBD-mysql-4.005.tar.gz -C /usr/lib/
# cd /usr/lib/
# mv DBD-mysql-4.005 dbd
# cd dbd
# perl Makefile.PL
# make
# make install
配置HEARTBEAT相关文件
cp /usr/share/doc/heartbeat-2.0.4/authkeys /etc/ha.d
# cd /etc/ha.d/
Authkeys配置
vi authkeys
#
# Authentication file. Must be mode 600
#
#
# Must have exactly one auth directive at the front.
# auth send authentication using this method-id
#
# Then, list the method and key that go with that method-id
#
# Available methods: crc sha1, md5. Crc doesn't need/want a key.
#
# You normally only have one authentication method-id listed in this file
#
# Put more than one to make a smooth transition when changing auth
# methods and/or keys.
#
#
# sha1 is believed to be the "best", md5 next best.
#
# crc adds no security, except from packet corruption.
# Use only on physically secure networks.
#
auth 1
#1 crc
1 sha1 HI!
#3 md5 Hello!
# chmod 600 authkeys
cp /usr/share/doc/heartbeat-2.0.4/