配置 nginx 负载均衡 + Rsync+inotify-tools 同步 +Mysql (master-master-salve) 安装环境: 操作系统:Centos 5.4 X64 应用软件: Nginx 1.2.7 php 5.3.22 Mysql Rsync 2.6.8 inotify-tools-3.14
主机A 192.168.1.21 Lnmp Nginx 主web1 Mysql-master rsync+inotify 主机A 192.168.1.21 LNMP Nginx web2 Mysql-master rsync 主机A 192.168.1.21 Lnmp Nginx web3 Mysql-slave rsync
(我这里是测试环境所以机器都是服务都是mysql跟web没分开了)
Nginx 做负载均衡 Rsync+inotify 做数据时时同步 Mysql 5.5 master-master-slave 数据库时时同步
1 :配置负载均衡
安装lnmp 我这里就不说了
现在开始配置nginx的负载均衡
我们选中主机A做入口机器来做分流所以在主机A 的nginx配置文件加入 (注意这个要加入到Http { }下面,当然你也可以写成独立的文件,再在nginx.conf里面包进来也可以)
配置服务器列表:
(这里注视下: nginx 的 upstream目前支持 4 种方式的分配 1)、轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 2)、weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 2)、ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 3)、fair(第三方) 按后端服务器的响应时间来分配请求,响应时间短的优先分配。 4)、url_hash(第三方)
) upstream lrm {
server 192.168.1.22:80 weight=6; server 192.168.1.21:80 backup; server 192.168.1.23:80 down; #ip_hash; }
(注视: down 表示单前的server暂时不参与负载 weight 默认为1.weight越大,负载的权重就越大。 max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误 fail_timeout:max_fails 次失败后,暂停的时间。 backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
) 建立虚拟主机来做代理来完成负载的目的:
server { listen 80; server_name www.lrm.com ;
location / {
proxy_pass http://lrm ; #使用定义的服务器列表 proxy_set_header Host $host; #一下都是设置一些代理的参数,可有可无 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
就这样配置完成重启nginx就OK 了
2:配置文件同步 首先都要安装rsync 服务 ,在主节点A上安装rsync 加inotify-tools yum -y install rsync
下载安装inotify-tools wget https://github.com/downloads/rvo ... y-tools-3.14.tar.gz tar zxvf inotify-tools-3.14.tar.gz cd inotify-tools ./configure && make && make install
做好链接ln -sv /usr/local/lib/libinotify* /usr/lib/ 如果是64位系统 ln -sv /usr/local/lib/libinotify* /usr/lib64 要不然启动会报这个错误“/usr/local/bin/inotifywait: error while loading shared libraries: libinotifytools.so.0”
在主机B C 上配置rsync 建立rsyncd.conf 配置文件 cat /etc/rsyncd.conf uid = www #指定该模块传输文件时守护进程应该具有的uid gid = www #指定该模块传输文件时守护进程应该具有的gid use chroot = no hosts allow =192.168.1.22 192.168.1.23 192.168.1.21 #充许任何主机连接 max connections = 10 #客户端最大连接数,默认0(没限制) pid file = /var/run/rsyncd.pid #运行进程的ID写到哪里 lock file = /var/run/rsync.lock #lock记录文件 log file = /var/log/rsyncd.log #日志记录文件 [lrm] # 这里是认证的模块名,在client端需要指定 path =/usr/local/nginx/html/ # 需要做备份的目录 comment = dwg file ignore errors # 可以忽略一些无关的IO错误 read only = no #no客户端可上传文件,yes只读 list = false auth users = fdw # 认证的用户名,如果没有这行,则表明是匿名 secrets file = /etc/server.pas # 指定认证口令文件位置
建立认证密码文件 cat /etc/server.pas fdw:123456
chmod 600 /etc/server.pas
启动服务 rsync --daemon
然后在主机A 上建立 密码文件 cat /etc/server.pas 123456
chmod 600 /etc/server.pas
启动服务 rsync --daemon
然后在主接点 A 上创建监控脚本 监控/usr/local/nginx/html/目录的 modify,delete,create,attrib 情况有这些情况就直接推送到22 23服务器上去 cat /root/rsync.sh #!/bin/bash src=/usr/local/nginx/html/
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%T %w %f' -e modify,delete,create,attrib $src | while read file do rsync -vzrtopg --delete --progress --password-file=/etc/server.pas $src fdw@192.168.1.22::lrm rsync -vzrtopg --delete --progress --password-file=/etc/server.pas $src fdw@192.168.1.23::lrm done (注视: -m 是保持一直监听 -r 是递归查看目录 -q 是打印出事件 --timefmt 是指定时间的输出格式 --format 指定文件变化的详细信息 -e create,move,delete,modify,attrib 是指 “监听 创建 移动 删除 写入 权限” 事件 )
然后运行
nohup ./rsync.sh &
配置完成,可以是时时在A 上创建 删除文件 看服务器B C 会不会时时同步了,当然这个是单项的时时同步,根据自己需求去做双向的吧。
3:配置mysql 的master-master-slave mysql为5.5
备注: 主机A 为master 主机B 为master 主机C 为slave
在主机A上添加 授权slave的帐号
GRANT REPLICATION SLAVE ON *.* TO ‘backup@192.168.1.22‘ IDENTIFIED BY ‘123456’;
GRANT REPLICATION SLAVE ON *.* TO ‘backup@192.168.1.23‘ IDENTIFIED BY ‘123456’;
在A 上设置要同步的数据库和不同步的数据库 在my.cnf 上添加 log-bin=mysql-bin #必须开启的二进制日志 server-id = 1 #主ID log-bin=/var/log/mysql/updatelog #日志文件路径 binlog-do-db=test #要同步的数据库 binlog-do-db=lrm #要同步的数据库 binlog-ignore-db=mysql #不要同步的数据库 #replicate-do-db=test #这些是当slave的时候要添加的要复制的数据库 #replicate-do-db=lrm #这些是当slave的时候要添加的要复制的数据库 #replicate-ignore-db=mysql #这些是当slave的时候要添加的不要复制的数据库 expire_logs_days = 7 #保存日志的天数 slave-skip-errors = 1062 #跳过错误
然后重启mysql 在查看 mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | updatelog.000001 | 107| test,lrm | mysql | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) (然后现在就不要动他了)
再在主机B C 上添加 #log-bin=mysql-bin #必须开启的二进制日志 server-id = 2 #主ID log-bin=/var/log/mysql/updatelog #日志文件路径 #binlog-do-db=test #要同步的数据库 当master的时候要开启的 #binlog-do-db=lrm #要同步的数据库 当master的时候要开启的 #binlog-ignore-db=mysql #不要同步的数据库 当master的时候要开启的 replicate-do-db=test #这些是当slave的时候要添加的要复制的数据库 replicate-do-db=lrm #这些是当slave的时候要添加的要复制的数据库 replicate-ignore-db=mysql #这些是当slave的时候要添加的不要复制的数据库 expire_logs_days = 7 #保存日志的天数 slave-skip-errors = 1062 #跳过错误
然后重启mysql 进入控制台 mysql>change master to master_host='192.168.1.21',master_user='rsync',master_password='123456',master_log_file='updatelog.000001',master_log_pos=107; mysql> slave start mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.21 Master_User: rsync Master_Port: 3306 Connect_Retry: 60 Master_Log_File: updatelog.000001 Read_Master_Log_Pos: 107 Relay_Log_File: mysqld-relay-bin.000001 Relay_Log_Pos: 107 Relay_Master_Log_File: updatelog.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: test,lrm Replicate_Ignore_DB: mysql Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 107 Relay_Log_Space: 107 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec)
看到这两个 Slave_IO_Running: Yes Slave_SQL_Running: Yes
说明同步完成。
然后再来做主机B跟主机A互为master 在主机B上创建同步帐号
GRANT REPLICATION SLAVE ON *.* TO ‘backup@192.168.1.21‘ IDENTIFIED BY ‘123456’;
在主机A的my.cnf上添加 #replicate-do-db=test #这些是当slave的时候要添加的要复制的数据库 #replicate-do-db=lrm #这些是当slave的时候要添加的要复制的数据库 #replicate-ignore-db=mysql #这些是当slave的时候要添加的不要复制的数据库 重启 在主机B的my.cnf上添加 #binlog-do-db=test #要同步的数据库 当master的时候要开启的 #binlog-do-db=lrm #要同步的数据库 当master的时候要开启的 #binlog-ignore-db=mysql #不要同步的数据库 当master的时候要开启的 重启 在主机B查看mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | updatelog.000004 | 107 | test,lrm | mysql | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) 然后在主机A 上做 mysql>change master to master_host='192.168.1.22',master_user='rsync',master_password='123456',master_log_file='updatelog.000004',master_log_pos=107; mysql> slave start mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.22 Master_User: rsync Master_Port: 3306 Connect_Retry: 60 Master_Log_File: updatelog.000004 Read_Master_Log_Pos: 107 Relay_Log_File: mysqld-relay-bin.000004 Relay_Log_Pos: 107 Relay_Master_Log_File: updatelog.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: test,lrm Replicate_Ignore_DB: mysql Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 107 Relay_Log_Space: 107 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec) 看到这两个 Slave_IO_Running: Yes Slave_SQL_Running: Yes
说明同步完成。现在可以操作任意两个master的数据库三台在数据库的数据都是一样的。
到此配置 nginx 负载均衡 + Rsync+inotify-tools 同步 +Mysql (master-master-salve) 完成,如果有不对的地方还请提出谢谢。 这里发现个问题就是我以前用mysql 5.1的时候在做同步的时候配置文里面是要填写 主服务器的IP 帐号 密码等的的。在mysql5.5 只要第一次在控制台输入就OK 了,本人重启也试过了没问题。
|