Mysql 5.7安装:https://www.cnblogs.com/shipment/p/14325309.html
KeepAlived:https://blog.csdn.net/qq_43377292/article/details/87364778
Rsync+inotify-tools:https://jackyu.cn/tech/rsync-plus-inotify-tools/
安装php:https://www.cnblogs.com/chenliang725/p/12575926.html

一、安装Mysql

  1. yum -y install mariadb-server
  2. systemctl start mariadb
  3. systemctl enable mariadb
  4. #初始化mariadb数据库
  5. mysql_secure_installation

Server1配置

  1. vim /etc/my.cnf
  2. #在mysqld下添加如下内容
  3. server-id = 1
  4. log-bin = mysql-bin
  5. sync_binlog = 1
  6. binlog_checksum = none
  7. binlog_format = mixed
  8. auto-increment-increment = 2
  9. auto-increment-offset = 1
  10. slave-skip-errors = all
  11. systemctl restart mariadb
  12. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
  13. flush privileges;

Server2配置

  1. vim /etc/my.cnf
  2. #在mysqld下添加如下内容
  3. server-id = 2
  4. log-bin = mysql-bin
  5. sync_binlog = 1
  6. binlog_checksum = none
  7. binlog_format = mixed
  8. auto-increment-increment = 2
  9. auto-increment-offset = 2
  10. slave-skip-errors = all
  11. systemctl restart mariadb

Server1配置

  1. flush tables with read lock;
  2. show master status;
  3. unlock tables;
  4. stop slave;
  5. change master to master_host='192.168.1.82',master_user='root',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=487;
  6. start slave;
  7. show slave status \G;

Server2配置

  1. flush tables with read lock;
  2. show master status;
  3. unlock tables;
  4. stop slave;
  5. change master to master_host='192.168.1.81',master_user='root',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=487;
  6. start slave;
  7. show slave status \G;

二、配置Keepalived

安装keepalived

  1. yum install keepalived ipvsadm -y

配置Server 1 keepalived

  1. vim /etc/keepalived/keepalived.conf
  2. #主节点标识,从节点不一致
  3. router_id QF_HA_Node01
  4. }
  5. vrrp_instance VI_1 {
  6. #表示这是master主节点
  7. state MASTER
  8. #标识虚拟ip绑定的网卡
  9. interface ens192
  10. virtual_router_id 51
  11. #优先级一般主节点比从节点的大
  12. priority 120
  13. advert_int 1
  14. #密码 主从一致
  15. authentication {
  16. auth_type PASS
  17. auth_pass 1111
  18. }
  19. #定义虚拟ip
  20. virtual_ipaddress {
  21. 192.168.1.83
  22. }
  23. }
  24. #定义后端真实成员
  25. virtual_server 192.168.1.83 80 3306 {
  26. delay_loop 6
  27. lb_algo rr
  28. lb_kind DR
  29. persistence_timeout 0
  30. protocol TCP
  31. real_server 192.168.1.81 80 3306 {
  32. weight 100
  33. TCP_CHECK {
  34. connect_timeout 3
  35. nb_get_retry 3
  36. delay_before_retry 3
  37. connect_port 80 3306
  38. }
  39. }
  40. real_server 192.168.1.82 80 3306 {
  41. weight 100
  42. TCP_CHECK {
  43. connect_timeout 3
  44. nb_get_retry 3
  45. delay_before_retry 3
  46. connect_port 80 3306
  47. }
  48. }
  49. }
  50. systemctl restart keepalived

Server2配置keepalived

  1. vim /etc/keepalived/keepalived.conf
  2. #主节点标识,从节点不一致
  3. router_id QF_HA_Node02
  4. }
  5. vrrp_instance VI_1 {
  6. #表示这是master主节点
  7. state MASTER
  8. #标识虚拟ip绑定的网卡
  9. interface ens192
  10. virtual_router_id 51
  11. #优先级一般主节点比从节点的大
  12. priority 119
  13. advert_int 1
  14. #密码 主从一致
  15. authentication {
  16. auth_type PASS
  17. auth_pass 1111
  18. }
  19. #定义虚拟ip
  20. virtual_ipaddress {
  21. 192.168.1.83
  22. }
  23. }
  24. #定义后端真实成员
  25. virtual_server 192.168.1.83 80 3306 {
  26. delay_loop 6
  27. lb_algo rr
  28. lb_kind DR
  29. persistence_timeout 0
  30. protocol TCP
  31. real_server 192.168.1.81 80 3306 {
  32. weight 100
  33. TCP_CHECK {
  34. connect_timeout 3
  35. nb_get_retry 3
  36. delay_before_retry 3
  37. connect_port 80 3306
  38. }
  39. }
  40. real_server 192.168.1.82 80 3306 {
  41. weight 100
  42. TCP_CHECK {
  43. connect_timeout 3
  44. nb_get_retry 3
  45. delay_before_retry 3
  46. connect_port 80 3306
  47. }
  48. }
  49. }
  50. systemctl restart keepalived

安装LAMP

  1. yum install httpd httpd-devel mariadb-libs mariadb-devel mariadb php-mysql
  2. yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath


  1. rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  2. rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  3. yum -y install php72w php72w-cli php72w-fpm php72w-common php72w-devel php72w-mysql

yum install -y php72w php72w-opcache php72w-xml php72w-mcrypt php72w-gd php72w-devel php72w-mysql php72w-intl php72w-mbstring

在网站根目录下创建test.php进行测试

  1. <?php
  2. echo phpinfo();
  3. ?>

systemctl start httpd
systemctl enable httpd
systemctl start keepalived
systemctl enable keepalived

安装rsync

Server1配置

  1. yum install rsync -y
  2. vim /etc/rsyncd.conf
  3. log file = /var/log/rsyncd.log
  4. #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
  5. pidfile = /var/run/rsyncd.pid
  6. #pid文件的存放位置
  7. lock file = /var/run/rsync.lock
  8. #支持max connections参数的锁文件
  9. secrets file = /etc/rsyncd.password
  10. #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
  11. motd file = /etc/rsyncd.Motd
  12. #rsync启动时欢迎信息页面文件位置(文件内容自定义)
  13. [web] #自定义名称
  14. path = /var/www/html/
  15. #rsync服务端数据目录路径
  16. comment = web
  17. #模块名称与自定义名称相同
  18. uid = root
  19. #设置rsync运行权限为root
  20. gid = root
  21. #设置rsync运行权限为root
  22. port = 873
  23. #默认端口
  24. use chroot = no
  25. #默认为true,修改为no,增加对目录文件软连接的备份
  26. read only = no
  27. #设置rsync服务端文件为读写权限
  28. list = yes
  29. #显示rsync服务端资源列表
  30. max connections = 200
  31. #最大连接数
  32. timeout = 300
  33. #设置超时时间
  34. auth users = web
  35. #虚拟用户名
  36. hosts allow = 192.168.1.82
  37. #允许进行数据同步的服务器,可以设置多个,用英文逗号隔开
  38. systemctl start rsyncd
  39. systemctl enable rsyncd
  40. echo "web:123456" > /etc/rsyncd.password
  41. echo "123456" > /etc/rsyncd.pw
  42. chmod 600 /etc/rsyncd.conf
  43. chmod 600 /etc/rsyncd.password
  44. chmod 600 /etc/rsyncd.pw

Server2配置

  1. yum install rsync -y
  2. vim /etc/rsyncd.conf
  3. log file = /var/log/rsyncd.log
  4. #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
  5. pidfile = /var/run/rsyncd.pid
  6. #pid文件的存放位置
  7. lock file = /var/run/rsync.lock
  8. #支持max connections参数的锁文件
  9. secrets file = /etc/rsyncd.password
  10. #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
  11. motd file = /etc/rsyncd.Motd
  12. #rsync启动时欢迎信息页面文件位置(文件内容自定义)
  13. [web]
  14. #自定义名称
  15. path = /var/www/html/
  16. #rsync服务端数据目录路径
  17. comment = web
  18. #模块名称与自定义名称相同
  19. uid = root
  20. #设置rsync运行权限为root
  21. gid = root
  22. #设置rsync运行权限为root
  23. port = 873
  24. #默认端口
  25. use chroot = no
  26. #默认为true,修改为no,增加对目录文件软连接的备份
  27. read only = no
  28. #设置rsync服务端文件为读写权限
  29. list = yes
  30. #显示rsync服务端资源列表
  31. max connections = 200
  32. #最大连接数
  33. timeout = 300
  34. #设置超时时间
  35. auth users = web
  36. #虚拟用户名
  37. hosts allow = 192.168.1.81
  38. #允许进行数据同步的服务器,可以设置多个,用英文逗号隔开
  39. systemctl start rsyncd
  40. systemctl enable rsyncd
  41. echo "web:123456" > /etc/rsyncd.password
  42. echo "123456" > /etc/rsyncd.pw
  43. chmod 600 /etc/rsyncd.conf
  44. chmod 600 /etc/rsyncd.password
  45. chmod 600 /etc/rsyncd.pw
  46. rsync --daemon

安装notify-tools

Server1配置

  1. sysctl -w fs.inotify.max_queued_events="99999999"
  2. sysctl -w fs.inotify.max_user_watches="99999999"
  3. sysctl -w fs.inotify.max_user_instances="65535"
  4. vi /etc/sysctl.conf #添加以下代码
  5. fs.inotify.max_queued_events=99999999
  6. fs.inotify.max_user_watches=99999999
  7. fs.inotify.max_user_instances=65535
  8. vim web.sh
  9. #!/bin/sh
  10. srcdir=/var/www/html/
  11. dstdir=web
  12. rsyncuser=web
  13. rsyncpassdir=/etc/rsyncd.pw
  14. dstip="192.168.1.82"
  15. for ip in $dstip
  16. do
  17. rsync -avH --port=873 --progress --delete $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
  18. done
  19. /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srcdir | while read file
  20. do
  21. for ip in $dstip
  22. do
  23. rsync -avH --port=873 --progress --delete $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
  24. echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
  25. done
  26. done
  27. chmod a+x web.sh
  28. vi /etc/rc.d/rc.local
  29. chmod +x /etc/rc.d/rc.local

Server2配置

  1. sysctl -w fs.inotify.max_queued_events="99999999"
  2. sysctl -w fs.inotify.max_user_watches="99999999"
  3. sysctl -w fs.inotify.max_user_instances="65535"
  4. vi /etc/sysctl.conf #添加以下代码
  5. fs.inotify.max_queued_events=99999999
  6. fs.inotify.max_user_watches=99999999
  7. fs.inotify.max_user_instances=65535
  8. vim web.sh
  9. #!/bin/sh
  10. srcdir=/var/www/html/
  11. dstdir=web
  12. rsyncuser=web
  13. rsyncpassdir=/etc/rsyncd.pw
  14. dstip="192.168.1.81"
  15. for ip in $dstip
  16. do
  17. rsync -avH --port=873 --progress --delete $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
  18. done
  19. /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srcdir | while read file
  20. do
  21. for ip in $dstip
  22. do
  23. rsync -avH --port=873 --progress --delete $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
  24. echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
  25. done
  26. done
  27. chmod a+x web.sh