您的位置:首页 > 运维架构 > Apache

apache+Mysql集群模式的实现

2013-05-13 16:44 495 查看

apache+Mysql集群模式的实现

人来疯 root

人来疯论坛 http://www.d-1701.com/bbs

MSN:[email]xu1701@hotmail.com[/email]

版权信息:未经许可不得转载,转载本文章请与人来疯论坛坛主root联系!

联系方法:使用msn

环境:

RedHat AS3 U5

说明:

如果不做说明,一般安装命令均使用ROOT权限,用 # 表示;一般命令用$表示。

特别说明:无

以下文章介绍Liunx 环境下的apache+Mysql安全备份模式的实现方法

一、前言

目前,Linux系统+Mysql数据库已成为众多中小企业及个人搭建网站环境的首选,因为它是完全免费的,而且性能相当不错。不过我见过的很多网站都是在单机上搭建此环境,然后购买价格相对较贵的备份产品及设备,其实如果我们熟悉一下apache和mysql,把购买备份产品及设备的钱省下再买几台服务器的话,完全可以解决问题。(服务器只是个代词,也可以根据你网站的规模选购性能好一点的PC机,都是没有问题的)

二、apache+Mysql安全备份模式图

见图1

从上图我们可以看出,用户通过访问http LVS服务器,获取Web服务,其实这个LVS服务器是个虚拟机,真正的Web服务是由这个虚拟机底下的两个http服务器提供的,这样,如果这两个 http服务器有一个有问题,那么可以改变Lvs模式,只让好的那台提供服务,为我们恢复故障节省了很多时间;而Mysql数据库采用Master及 Slave模式,避免由于单点故障而全盘皆输的窘迫局面,建议访问量较大的网站采用全服务器配置或至少LVS及Mysql服务器采用全服务器配置,而访问量属于中等偏下网站全部采用PC机都是没有问题的。LVS服务器使用双网卡(eth0和eth1)。eth0为虚拟服务器的IP地址(公网IP),eth1为内网IP地址,其他均为内网地址,这样可以更安全。

我们假定mysql及http服务器均已安装完成,并且在http服务器上跑的站点可以调用mysql数据库,mysql主数据库(Master)为 192.168.1.4,从数据库(slave)为192.168.1.5,站点调用的数据库名称为hello。如果你不知道怎么安装apache及 mysql,请参阅我写的《mysql环境下安装PHP504及apache2054》

三、实现方法

(1)下载RedHat_Cluster_Suite.tgz软件包,地址如下:

http://www.d-1701.com/tools/RedHat_Cluster_Suite.tgz

这个其实是redhat AS3自带的拓展服务包,用RPM安装也可以,但我们一般都只有前四张,后七张很少有人有,那我就把我手头上这个安装包给大家共享一下 :)

(2)开启系统vnc 服务并安装

# vncserver

连入linux系统,开一个终端

# cd /home/xuchen

# tar xzvf RedHat_Cluster_Suite.tgz

# cd RedHat_Cluster_Suite

# ./autorun

这将开启安装界面,没什么好说,直接安装就是了。

(3)配置前的准备

修改集群服务器的IP转发设置

# /sbin/sysctl -w net.ipv4.ip_forward=1

修改当前会话的值

# vi /etc/sysctl.conf

net.ipv4.ip_forward = 1

这样下次重启时设置可以自动生效

系统安装上Red hat cluster suite 集群软件后,使用一个名为Piranha的配置工具配置集群服务器,此配置工具使用 apache 作web Server 来提供基于web页面的配置接口。此配置接口的web页面站点的配置文件网页文件及其他相关文件存放在/etc/sysconfig/ha/ 目录中。

# vi /etc/sysconfig/ha/conf/httpd.conf

配置接口页面站点的相关参数。缺省的监听端口是3636,你可以改成其他的端口

Listen 3636

访问Piranha配置接口页面需要有帐号和密码,

帐号默认为piranha,用以下命令配置密码

# /usr/sbin/piranha-passwd //设置 piranha 的登录口令。

# /sbin/service piranha-gui start //启动Piranha配置接口页面站点

访问http://Your’s IP:3636/ 登录 Piranha。利用此页面直接配置接口配置集群服务。

配置之前要明白的几个概念:

虚拟服务器:用户访问http服务器(LVS服务器IP),感觉上就是访问一台高效的服务器。此台“高效的服务器”,我们称之为“虚拟服务器”,实际上就是这里的集群服务器。

真实服务器:即真实提供服务的服务器,此类服务器可以有多台,而且可以增加。

(4)配置LVS

选择GLOBAL SETTINGS 选项卡。进行全局设置。

Primary server public IP: 211.100.1.1 集群服务器的外网ip

Primary server private IP::为空

本例中采用LVS NAT模式的集群,以下是集群的不同模式区别:

1. Virtual Server via Network Address Translation(VS/NAT)

通过网络地址转换,调度器重写请求报文的目标地址,根据预设的调度算法,将请求分派给后端的真实服务器;真实服务器的响应报文通过调度器时,报文的源地址被重写,再返回给客户,完成整个负载调度过程。

2. Virtual Server via IP Tunneling(VS/TUN)

采用NAT技术时,由于请求和响应报文都必须经过调度器地址重写,当客户请求越来越多时,调度器的处理能力将成为瓶颈。为了解决这个问题,调度器把请求报文通过IP隧道转发至真实服务器,而真实服务器将响应直接返回给客户,所以调度器只处理请求报文。由于一般网络服务应答比请求报文大许多,采用 VS/TUN技术后,集群系统的最大吞吐量可以提高10倍。

3. Virtual Server via Direct Routing(VS/DR)

VS/DR通过改写请求报文的MAC地址,将请求发送到真实服务器,而真实服务器将响应直接返回给客户。同VS/TUN技术一样,VS/DR技术可极大地提高集群系统的伸缩性。这种方法没有IP隧道的开销,对集群中的真实服务器也没有必须支持IP隧道协议的要求,但是要求调度器与真实服务器都有一块网卡连在同一物理网段上。

点击NAT按钮,出现NAT网络设置:

NAT Router IP:192.168.1.2 NAT路径名称

NAT Router netmask:255.255.255.0 NAT掩码

NAT Router device:eth1 NAT 路径装置

选择VIRTUAL SERVERS 选项卡。

点击ADD按钮,添加虚拟服务器:

点击EDIT按钮,配置该虚拟服务器:

Name:http 此虚拟服务器的名,可以随意,建议以提供的服务命名。

Application port:80 应用监听端口。Web服务为80端口

Protocol:tcp 采用tcp协议通信

Virtual IP Address:211.100.1.1 此虚拟服务器的有效外网ip地址,供用户访问

Virtual IP Network Mask:255.255.255.0 此虚拟服务器的子网掩码,请查看网卡设置。

Firewall Mask:(空)

Device:eth0 外网通信网卡

Re-entry timeout:15 重入时间(S)

Service timeoute:6 服务延时(S)

Quiesce server:NO

Load monitoring tool:none 不加载监控工具

Scheduling:Weighted least-connections 调度策略(权重最小连接)

Persistence:1440 持续性(S)

Persistence Network Mask:255.255.255.0 持续性屏蔽

配置完毕,点击ACCEPT将配置写入配置文档 /etc/sysconfig/ha/lvs.cf

如果想要让同一个IP在一段时间内只访问同一个真实服务器,就要设置Persistence 值,在这指定时间内,无论采用何种负载均衡算法,同一个IP都会访问同一个真实服务器。

虚拟服务器配置完后,为此虚拟服务器添加真实服务器。

点击子选项卡REAL SERVER:

点击ADD按钮添加真实服务器:

点击EDIT按钮配置此真实服务器:

Weight 为该真实服务器的权重。虚拟服务器按调度策略根据此设置值进行调度。

配置完点击ACCEPT按钮将配置写入配置文档 /etc/sysconfig/ha/lvs.cf 。

重新点击子选项卡REAL SERVER可添加多个真实服务器。

点击(DE)ACTIVATE按钮可使真实服务器生效(UP)。不过建议在完成所有配置后再启动。

点击子选项卡MONITORING SCRIPTS :

配置发送和接收字符串,让lvs确定real server是否存在。因为当前lvs模式有弊端,确定包文在网络流量大的时候,会丢包,从而造成real server掉线的假象,所以现在采取脚本send.sh发送和确认,保证每次都能返回确认字符串。

/root/send.sh 的内容很简单:

#!/bin/sh

echo "http"

echo 的内容就是期待的内容。

至此,已经为虚拟服务器添加完毕真实服务器。启动真实服务器的apache服务。进入LVS配置的相关界面,点击(DE)ACTIVATE按钮使设备UP起来。

以上所有的配置结果都ACCEPT后,将写入配置文件/etc/sysconfig/ha/lvs.cf 中。如果你熟悉了LVS 的配置,可以直接修改此配置文件,而不需要使用piranha配置工具。

# /sbin/service piranha-gui stop //关闭配置界面,增强安全性

# /sbin/service pulse start //启动lvs服务,此时进程列表里应含用 pulse 和 lvs 的进程名。

# ipvsadm //查看当前的路由表

IP Virtual Server version 1.0.8 (size=65536)

Prot LocalAddressort Scheduler Flags

->; RemoteAddressort Forward Weight ActiveConn InActConn

TCP 211.100.1.1:http wlc persistent 1440 mask 255.255.255.0

->; 192.168.1.2:http Masq 100 128 24929

->; 192.168.1.3:http Masq 100 137 21351

如果上述的192.168.1.2:http真实服务器显示的是主机名而不是ip地址,为了加快路由转发,把它们的主机名对应的ip解析加在了负载均衡器的/etc/hosts中。最好把每一个真实服务器的域名解析都放到此文件中。

当然你要把所有机器的apache都起来,如果有一个没有起来,上面的列表中就会少一行。Lvs每隔一段时间会检测一下真实服务器(即上述的发包监控)。

注意!!!

如果修改了/etc/sysconfig/ha/lvs.cf(无论是手工编辑还是通过Piranha)只有重新启动pulse服务才会起作用。

实践操作中,pulse服务起来之后如果被停止掉,它会把外网和内网的两个网卡全部down掉,只好通过控制台再启动两个网卡和pulse服务。

所以在远程控制lvs集群负载机的时候,如果要重新启动pulse,应该直接reboot机器,以防止网卡down掉,无法访问远程机器。

改动当前的配置,还可以使用 ipvsadm直接修改内核路由表,相关操作命令请查阅ipvsadm的命令行帮助。

例如:假设当前已经有了websvr1(192.168.1.2)和websvr2(192.168.1.3)两个真实服务器,扩容需要新增另外一台 websvr3(192.168.1.6)真实服务器。直接在/etc/sysconfig/ha/lvs.cf 中加入了websvr3的信息,但是无法立即生效,这时可以用以下命令:

ipvsadm -a -t 211.100.1.1:http -r 192.168.1.3:http -m -w 100

这样就可以加入到内核路由表并立即生效。

以上就是一个典型的集群服务器的安装和配置过程,大家可以参考配置自己的集群服务器。

(5)配置mysql数据库的Master-Slave 模式

mysql数据库的Master-Slave机制

mysql数据库支持Master-Slave主从数据库的集群模式。Slave (从)数据库根据Master(主)数据库的日志文件(master_log_file)及日志位置(master_log_pos)实时从Master数据库读取数据,自动实现数据同步。

mysql数据库Master-Slave实现

①Slave数据库服务器要拥有访问Master资源的权限,因此首先要在Master上为Slave创建访问帐号并赋予文档复制权限。

#mysql -h192.168.1.4 –uroot –p –D hello

mysql>;GRANT REPLICATION SLAVE ON *.* TO 'testbak'@'%' IDENTIFIED BY 'testbak';

②根据mysql数据库的Master-Slave机制,Slave数据库实施数据同步操作的依据是Master数据库的日志文件及该日志文件的日志位置。因此,在为Master新增一台Slave服务器的时候要确保为Slave配置的参数(master_log_file及 master_log_pos)就是Master当前使用的日志文件及当前的操作节点。如果Master数据库的数据一直处于运营状态(即有插入、修改、删除等操作),将不能满足上述要求。所以目前的维护操作是:系统扩容为Master增加一台Slave的时候,要停止整个业务系统,以停止对Master
数据库数据的读写操作,然后查看Master当前的日志文件及日志位置。

#mysql -h192.168.1.4 –uroot –p –D hello

mysql>; show master status;

有个改进的操作,可以将Master数据库锁住为只读,防止数据的更新。此时不用停止业务系统,但用户将不能进行信息修改、注册等操作。(待测试验证)

③将与当前Master数据库master_log_file、master_log_pos的值匹配的数据从Master拷贝到Slave上。可以采用mysqldump方法,也可以直接拷贝数据文件。注意,拷贝数据之前要用

# mysqladmin –u root –p shutdown

关闭主数据库。这里我们直接把数据文件压缩为mysql.tar.gz

④修改Master数据库的配置文件

一般在/etc/my.cnf

以192.168.1.4 Master数据库的配置文件为例:

修改/etc/my.cnf ,在[mysqld]区段内加入参数

# vi /etc/my.cnf

log-bin

server-id=1

sql-bin-update-same

binlog-do-db=hello

******************************************************************************

# Example mysql config file for very large systems.

#

# This is for large system with memory of 1G-2G where the system runs mainly

# MySQL.

#

# You can copy this file to

# /etc/my.cnf to set global options,

# mysql-data-dir/my.cnf to set server-specific options (in this

# installation this directory is /usr/local/mysql/var) or

# ~/.my.cnf to set user-specific options.

#

# One can in this file use all long options that the program supports.

# If you want to know which options a program support, run the program

# with --help option.

# The following options will be passed to all MySQL clients

[client]

#password = your_password

port = 3306

socket = /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server

[mysqld]

port = 3306

socket = /tmp/mysql.sock

skip-locking

set-variable = key_buffer=384M

set-variable = max_allowed_packet=1M

set-variable = table_cache=512

set-variable = sort_buffer=2M

set-variable = record_buffer=2M

set-variable = thread_cache=8

# Try number of CPU's*2 for thread_concurrency

set-variable = thread_concurrency=8

set-variable = myisam_sort_buffer_size=64M

log-bin

server-id = 1

sql-bin-update-same

binlog-do-db=hello

# Replication Master Server (default)

#log-bin # required for replication

#server-id = 1 # required unique id between 1 and 2^32 - 1

# defaults to 1 if master-host is not set

# but will not function as a master if omitted

# Replication Slave Server (comment out master section to use this)

#master-host = # MUST BE SET

#master-user = # MUST BE SET

#master-password = # MUST BE SET

#master-port = # optional--defaults to 3306

#log-bin # not required for slaves, but recommended

#server-id = 2 # required unique id between 2 and 2^32 - 1

# (and different from the master)

# defaults to 2 if master-host is set

# but will not function as a slave if omitted

# Point the following paths to different dedicated disks

#tmpdir = /tmp/

#log-update = /path-to-dedicated-directory/hostname

# Uncomment the following if you are using BDB tables

#set-variable = bdb_cache_size=384M

#set-variable = bdb_max_lock=100000

# Uncomment the following if you are using InnoDB tables

#innodb_data_home_dir = /usr/local/mysql/var/

#innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend

#innodb_log_group_home_dir = /usr/local/mysql/var/

#innodb_log_arch_dir = /usr/local/mysql/var/

# You can set .._buffer_pool_size up to 50 - 80 %

# of RAM but beware of setting memory usage too high

#set-variable = innodb_buffer_pool_size=384M

#set-variable = innodb_additional_mem_pool_size=20M

# Set .._log_file_size to 25 % of buffer pool size

#set-variable = innodb_log_file_size=100M

#set-variable = innodb_log_buffer_size=8M

#innodb_flush_log_at_trx_commit=1

#set-variable = innodb_lock_wait_timeout=50

[mysqldump]

quick

set-variable = max_allowed_packet=16M

[mysql]

no-auto-rehash

# Remove the next comment character if you are not familiar with SQL

#safe-updates

[isamchk]

set-variable = key_buffer=256M

set-variable = sort_buffer=256M

set-variable = read_buffer=2M

set-variable = write_buffer=2M

[myisamchk]

set-variable = key_buffer=256M

set-variable = sort_buffer=256M

set-variable = read_buffer=2M

set-variable = write_buffer=2M

[mysqlhotcopy]

interactive-timeout

******************************************************************************

⑤修改Slave数据库的配置文件

以192.168.1.5 Slave数据库的配置文件为例

修改/etc/my.cnf ,在[mysqld]区段内加入参数

# vi /etc/my.cnf

master-host=192.168.1.3

master-user=testbak

master-password=**** //自己指定

master-port=3306

server-id=2

master-connect-retry=60 预设重试间隔60秒

replicate-do-db=hello 告诉slave只做ephd数据库的更新

log-slave-updates

*******************************************************************************

# Example mysql config file for very large systems.

#

# This is for large system with memory of 1G-2G where the system runs mainly

# MySQL.

#

# You can copy this file to

# /etc/my.cnf to set global options,

# mysql-data-dir/my.cnf to set server-specific options (in this

# installation this directory is /usr/local/mysql/var) or

# ~/.my.cnf to set user-specific options.

#

# One can in this file use all long options that the program supports.

# If you want to know which options a program support, run the program

# with --help option.

# The following options will be passed to all MySQL clients

[client]

#password = your_password

port = 3306

socket = /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server

[mysqld]

port = 3306

socket = /tmp/mysql.sock

skip-locking

set-variable = key_buffer=384M

set-variable = max_allowed_packet=1M

set-variable = table_cache=512

set-variable = sort_buffer=2M

set-variable = record_buffer=2M

set-variable = thread_cache=8

# Try number of CPU's*2 for thread_concurrency

set-variable = thread_concurrency=8

set-variable = myisam_sort_buffer_size=64M

master-host=192.168.1.3

master-user=testbak

master-password=testbak

master-port=3306

server-id=2

master-connect-retry=60

replicate-do-db=hello

log-slave-updates

# Point the following paths to different dedicated disks

#tmpdir = /tmp/

#log-update = /path-to-dedicated-directory/hostname

# Uncomment the following if you are using BDB tables

#set-variable = bdb_cache_size=384M

#set-variable = bdb_max_lock=100000

# Uncomment the following if you are using InnoDB tables

#innodb_data_home_dir = /usr/local/mysql/var/

#innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend

#innodb_log_group_home_dir = /usr/local/mysql/var/

#innodb_log_arch_dir = /usr/local/mysql/var/

# You can set .._buffer_pool_size up to 50 - 80 %

# of RAM but beware of setting memory usage too high

#set-variable = innodb_buffer_pool_size=384M

#set-variable = innodb_additional_mem_pool_size=20M

# Set .._log_file_size to 25 % of buffer pool size

#set-variable = innodb_log_file_size=100M

#set-variable = innodb_log_buffer_size=8M

#innodb_flush_log_at_trx_commit=1

#set-variable = innodb_lock_wait_timeout=50

[mysqldump]

quick

set-variable = max_allowed_packet=16M

[mysql]

no-auto-rehash

# Remove the next comment character if you are not familiar with SQL

#safe-updates

[isamchk]

set-variable = key_buffer=256M

set-variable = sort_buffer=256M

set-variable = read_buffer=2M

set-variable = write_buffer=2M

[myisamchk]

set-variable = key_buffer=256M

set-variable = sort_buffer=256M

set-variable = read_buffer=2M

set-variable = write_buffer=2M

[mysqlhotcopy]

interactive-timeout

*******************************************************************************

配置完毕,重启主数据库,由于配置文件中加入了log-bin参数,因此开始有index产生,在/var/lib/mysql目录下有.index档案纪录数据库的异常log。

配置Slave数据库

将192.168.1.4 master 的备份mysql.tar.gz复制到192.168.1.5 slave上

#cd /var/lib/

#tar xzvf mysql.tar.gz

#chown –R mysql:mysql mysql

配置一般在mysql命令行下进行。

#mysql -h192.168.1.5 –uroot –p

mysql>; change master to master_log_file='ephdb05-bin.003',master_log_pos=169;

启动Slave数据库:

mysql>; start slave;

mysql>; show slave status;

查看Slave数据同步操作的依据Master_Log_File,Read_Master_Log_Pos是否与当前Master的一致。这时在 /var/lib/mysql目录会出现master.info,此档案纪录了Master MySQL server的信息。

数据库优化

采用了blob的数据类型,长时间运行会造成数据碎片,需要进行整理。

在处理数据优化前,需要关闭mysql服务。

/usr/local/mysql/bin/mysqladmin shutdown

修复和整理数据库

/usr/local/mysql/bin/myisamchk -r /usr/local/mysql/var/hello/1_tbl

对第一个索引进行重索引

/usr/local/mysql/bin/myisamchk -R 1 /usr/local/mysql/var/hello/1_tbl

将索引按照倒序排序,加快检索速度

/usr/local/mysql/bin/myisamchk -S /usr/local/mysql/var/hello/1_tbl

对数据库关联优化

/usr/local/mysql/bin/myisamchk -a /usr/local/mysql/var/hello/1_tbl

(其他数据库、表的操作类似)

四、总结

apache+php+mysql越来越多被应用于网站建设,如何才能更安全,更有效的保护好数据是系统管理员的头等大事,希望能有越来越多的好工具,好方法,好思路来协助我们做好这个工作,而且我坚信,以后的路是越走越宽的,套用老话结束这个文章------技术无极限!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: