架构图:
服务器准备
192.168.1.10:nginx(做缓存和负载均衡)
192.168.1.11:http1(业务)
192.168.1.12:http2(业务)
192.168.1.13:amoeba(mysql高可用)
192.168.1.14:mysql主
192.168.1.15:mysql从
192.168.1.16:nfs
192.168.1.17:数据备份服务器
1、配置两台http服务器(11、12)
yum -y install httpd php php-mysql
systemctl start httpd
systemctl enable httpd
2、配置nfs服务器
nfs服务器操作(16)
安装nfs服务端相关包,开启共享目录读写权限,开启服务并查看服务状态
yum -y install nfs-utils rpcbind
mkdir -p /backup/http
echo "/backup/http 192.168.1.0/24(rw,sync,no_all_squash)" >/etc/exports
systemctl start rpcbind
systemctl enable rpcbind
systemctl start nfs
systemctl enable nfs
exportfs
两台http服务器操作(11、12)
安装nfs客户端,并将nfs服务器共享目录/backup/http/挂载到 /var/www/html/,写入系统配置文件
yum -y install nfs-utils
mount 192.168.1.16:/backup/http/ /var/www/html/
df -Th
echo "192.168.1.16:/backup/http /var/www/html nfs defaults,_netdev 0 0" >>/etc/fstab
3、配置主从mysql,开启慢查询日志和半同步复制,并配置主从同步+amoeba读写分离
配置主从同步
主库(14):
yum -y install mariadb mariadb-server
vim /etc/my.cnf
log-bin=mysql-bin
server-id=1
从库(15):
yum -y install mariadb mariadb-server
vim /etc/my.cnf
log-bin=mysql-bin
server-id=2
配置慢查询日志
主库添加:
vim /etc/my.cnf
slow_query_log = ON
slow_query_log_file = /var/lib/mysql/logs/slow.log
long_query_time = 1
开启半同步复制
主库需添加配置:
plugin-load=rpl_semi_sync_master=semisync_master.so
rpl_semi_sync_master_enabled=1
从库:
plugin-load=rpl_semi_sync_slave=semisync_slave.so
rpl_semi_sync_slave_enabled=1
综上:
主库配置文件:
从库配置文件:
配置后开启
systemctl start mariadb.service
systemctl enable mariadb.service
测试:
测试主从同步
主库:
mysql
grant replication slave on *.* to slave@'192.168.1.%' identified by '123';
show master status;
找到mysql-bin.000003和master_log_pos=373,使用这两条数据配置到从库
从库:
mysql #登入
change master to master_user='slave',master_password='123',master_host='192.168.1.14',master_log_file='mysql-bin.000003',master_log_pos=373;
start slave;
show slave status\G
主库插入数据:
create database school;
use school
create table students(id int,name varchar(20));
insert into students values(1,'zhangsan'),(2,'lisi');
select * from students;
从库查看是否同步(可看到已经同步),然后暂停同步额外插入一条数据(为读写分离做测试)
stop slave;
use school
insert into students values(3,'wangwu');
start slave;
select * from students;
主库查看慢查询是否开启:
show variables like "slow%";
查看半同步复制是否开启
主库
show variables like "rpl_semi_sync_master%";
状态统计
show status like "rpl%";
从库
show variables like "rpl_semi_sync_slave%";
4、amoeba读写分离
jdk环境变量
下载包amoeba-n.zip
unzip amoeba-n.zip
yum -y install gcc gcc-c++
cd amoeba-n
mkdir /amoeba
tar -xf jdk-7u40-linux-x64.gz
cp -a jdk1.7.0_40/ /amoeba/jdk
vim /etc/profile
#java-amoeba
export JAVA_HOME=/amoeba/jdk
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/bin/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH
source /etc/profile
java -version
配置amoeba
unzip amoeba-mysql-1.3.1-BETA.zip -d /usr/local/amoeba
cd /usr/local/amoeba/
chmod +x bin/*
vim /usr/local/amoeba/conf/amoeba.xml
<server>
<!-- proxy server绑定的端口 -->
<property name="port">8066</property>
<property name="ipAddress">192.168.1.13</property>
<!-- proxy server net IO Read thread size -->
<property name="readThreadPoolSize">20</property>
<!-- proxy server client process thread size -->
<property name="clientSideThreadPoolSize">30</property>
<!-- mysql server data packet process thread size -->
<property name="serverSideThreadPoolSize">30</property>
<!-- socket Send and receive BufferSize(unit:K) -->
<property name="netBufferSize">128</property>
<!-- Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). -->
<property name="tcpNoDelay">true</property>
<!-- 对外验证的用户名 -->
<property name="user">amoeba</property>
<!-- 对外验证的密码 -->
<property name="password">123</property>
<!-- query timeout( default: 60 second , TimeUnit:second) -->
<property name="queryTimeout">60</property>
</server>
<!--
每个ConnectionManager都将作为一个线程启动。
manager负责Connection IO读写/死亡检测
-->
<connectionManagerList>
<connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
<property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>
<!--
default value is avaliable Processors
<property name="processors">5</property>
-->
</connectionManager>
</connectionManagerList>
<dbServerList>
<dbServer name="server1">
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">defaultManager</property>
<property name="port">3306</property>
<property name="ipAddress">192.168.1.14</property>
<property name="schema">school</property>
<property name="user">abc</property>
<property name="password">123</property>
</factoryConfig>
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">200</property>
<property name="maxIdle">200</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<dbServer name="server2">
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">defaultManager</property>
<property name="port">3306</property>
<property name="ipAddress">192.168.1.15</property>
<property name="schema">school</property>
<property name="user">abc</property>
<property name="password">123</property>
</factoryConfig>
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">200</property>
<property name="maxIdle">200</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<dbServer name="write" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<property name="loadbalance">1</property>
<property name="poolNames">server1</property>
</poolConfig>
</dbServer>
<dbServer name="read" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<property name="loadbalance">1</property>
<property name="poolNames">server1,server2</property>
</poolConfig>
</dbServer>
</dbServerList>
<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
<property name="ruleConfig">${amoeba.home}/conf/rule.xml</property>
<property name="functionConfig">${amoeba.home}/conf/functionMap.xml</property>
<property name="ruleFunctionConfig">${amoeba.home}/conf/ruleFunctionMap.xml</property>
<property name="LRUMapSize">1500</property>
<property name="defaultPool">write</property>
<property name="writePool">write</property>
<property name="readPool">read</property>
<property name="needParse">true</property>
</queryRouter>
vim /usr/local/amoeba/bin/amoeba
测试
主库服务器授权给abc用户grant all on . to abc@'%' identified by '123';
从库自然会同步select user,host from mysql.user;
amoeba客户端登录测试
yum -y install mariadb
mysql -uabc -p123 -h 192.168.1.14
mysql -uabc -p123 -h 192.168.1.15
登录成功继续即可
登录成功后开启amoeba并登陆自己
nohup bash -x /usr/local/amoeba/bin/amoeba &
netstat -anpt | grep 8066
mysql -uamoeba -p123 -P8066 -h192.168.1.13
测试轮询
use school;
select * from students;
测试高可用
宕机一台mysql尝试轮询
nfs服务器
在nfs服务器(16)挂载目录安装dizcuzdb,之后数据库指向主库(14)
安装yum -y install httpd
cd /backup/http/
加入包,放在/backup/http/目录下Discuz_X3.3_SC_UTF8.zip
yum -y install unzip.x86_64
unzip Discuz_X3.3_SC_UTF8.zipmv upload/* .
rm -rf Discuz_X3.3_SC_UTF8.zip readme/ utility/ upload
chown -R apache:apache .
测试页面输入:192.168.1.11和192.168.1.12
配置nginx实现轮询(10)
安装nginx(10)nginx-1.6.0.tar.gz
yum -y install gcc gcc-c++ pcre-devel openssl openssl-devel zlib-devel ncurses-devel cmake bison libxml2-devel libpng-devel
useradd -M -s /sbin/nologin nginx
tar xf nginx-1.6.0.tar.gz -C /usr/src; cd /usr/src/nginx-1.6.0
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
echo $?
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
vim /usr/local/nginx/conf/nginx.conf
upstream apache {
server 192.168.1.11:80;
server 192.168.1.12:80;
}
location / {
proxy_pass http://apache;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
开启nginx
浏览器输入192.168.1.10,测试负载均衡
配置discuzdb
首先在mysql主服务器中创建数据库discuzdbcreate database discuzdb;
数据库名字:discuzdb
浏览器IP界面输入192.168.1.13:8066
在浏览器页面数据库使用的是amoeba登录本机所用的用户(amoeba)
下一步
登陆成功:
apache、nginx、nfs和mysql服务器实现备份
首先这5台服务器:(10nginx 11http1 14mysql主 15mysql从 16nfs 备份服务器17 )把包和脚本都放到家目录下
inotify-tools-3.14.tar.gz
inotify_install.sh
#!/bin/bash
yum -y install gcc gcc-c++
tar xvf inotify-tools-3.14.tar.gz -C /usr/local
cd /usr/local/inotify-tools-3.14 ; ./configure
make && make install
rsync.sh
#!/bin/bash
#安装rsync包
yum -y install rsync
#创建rsync用户
useradd -M -s /sbin/nologin rsync
#创建备份目录,并修改属主和属组(已经创建,可以忽略)
mkdir /backup &>/dev/null
chown -R rsync.rsync /backup
#对配置文件进行修改
cat <<EOF>/etc/rsyncd.conf
uid = root
gid = root
use chroot = yes
max connections = 200
pid file = /var/run/rsyncd.pid
timeout = 900
ignore errors
list = false
fake super = yes
read only = false
############################
[backup]
comment = welcome to backup
auth users = rsync_user
path = /backup
secrets file = /etc/rsync.password
EOF
#创建认证文件,修改权限
echo "rsync_user:123" > /etc/rsync.password
chmod 600 /etc/rsync.password
#启动和自启动服务
systemctl start rsyncd
systemctl enable rsyncd
netstat -anput | grep :873
#客户端创建密码文件
echo "123" >/etc/rsync.pass
chmod 600 /etc/rsync.pass
nginx_send.sh
#!bin/bash
# 需要同步端的ip(这里需要改)
IP=192.168.1.17
# 需要同步的目录
DIR=/usr/local/nginx/conf/
#开启文件增删改监控并指定监控的目录
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write,modify,delete,create,attrib $DIR | while read line &>/dev/null
do
#持续监控VIP是否存在再自己的服务器端,如果存在,立刻进行备份
rsync -avz --delete $DIR rsync_user@$IP::nginx --password-file=/etc/rsync.pass &>/dev/null
done
http配置(192.18.1.11即可)
inotify-tools-3.14.tar.gz
inotify_install.sh
rsync.sh
apache_send.sh
#!bin/bash
# 需要同步端的ip(这里需要改)
IP=192.168.1.17
# 需要同步的目录
DIR=/etc/httpd/conf/
#开启文件增删改监控并指定监控的目录
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write,modify,delete,create,attrib $DIR | while read line &>/dev/null
do
#持续监控VIP是否存在再自己的服务器端,如果存在,立刻进行备份
rsync -avz --delete $DIR rsync_user@$IP::apache --password-file=/etc/rsync.pass
done
mysql主库配置(14)
inotify-tools-3.14.tar.gz
inotify_install.sh
rsync.sh
dayback.sh
#!/bin/bash
DIR=/var/lib/mysql
find $DIR -mtime +7 -type f -name "mysql-bin*" ! -name mysql-bin.index -exec rm -rf {} \;
cd $DIR
/usr/bin/tar czvf binlog-$(date +%F).tar.gz mysql-bin*
/usr/bin/rsync -avz binlog-$(date +%F).tar.gz rsync_user@192.168.1.17::mysql --password-file=/etc/rsync.pass
rm -rf binlog-$(date +%F).tar.gz
删除7天以前的binlog日志并且备份当天的日志到备份服务器上
mysql从库配置(15)
inotify-tools-3.14.tar.gz
inotify_install.sh
rsync.sh
weekback.sh
#!/bin/bash
database=discuzdb
mysqldump $database > $database.sql-$(date +%F)
/usr/bin/tar cvzf $database.sql-$(date +%F).week.tar.gz /var/lib/mysql/$database
/usr/bin/rsync -avz $database.sql-$(date +%F).week.tar.gz rsync_user@192.168.1.17::mysql --password-file=/etc/rsync.pass
rm -rf $database.sql-$(date +%F).week.tar.gz
每周对整个数据库进行一次打包备份
nfs配置(16):
inotify-tools-3.14.tar.gz
inotify_install.sh
rsync.sh
nfs_send.sh
#!bin/bash
# 需要同步端的ip(这里需要改)
IP=192.168.1.17
# 需要同步的目录
DIR=http
cd /backup
#开启文件增删改监控并指定监控的目录
/usr/bin/tar cvzf nfsbak-$(date +%F).tar.gz $DIR
/usr/bin/rsync -avz --delete nfsbak-$(date +%F).tar.gz rsync_user@$IP::nfs --password-file=/etc/rsync.pass
rm -rf nfsbak-$(date +%F).tar.gz
备份打包nfs中共享http的数据文件,并发送给备份服务器
备份服务器配置(17):
inotify-tools-3.14.tar.gz
inotify_install.sh
rsync.sh
运行脚本,开启各个服务器的inotify发送脚本后台执行
备份服务器(17)
sh inotify_install.sh
sh rsync.sh
额外操作
mkdir -p /backup/{nginx,apache,nfs,mysql}
vim /etc/rsyncd.conf
修改模块
[apache]
comment = welcome to backup
auth users = rsync_user
path = /backup/apache
secrets file = /etc/rsync.password
[nginx]
comment = welcome to backup
auth users = rsync_user
path = /backup/nginx
secrets file = /etc/rsync.password
[mysql]
comment = welcome to backup
auth users = rsync_user
path = /backup/mysql
secrets file = /etc/rsync.password
[nfs]
comment = welcome to backup
auth users = rsync_user
path = /backup/nfs
secrets file = /etc/rsync.password
systemctl restart rsyncd
nginx(10)
sh inotify_install.sh
sh rsync.sh
sh nginx_send.sh &
apache(11)
sh inotify_install.sh
sh rsync.sh
sh apache_send.sh &
mysql主(14)
sh inotify_install.sh
sh rsync.sh
写入周期性计划任务,每天备份一次
crontab -e
0 3 * * * /usr/bin/sh /root/dayback.sh
mysql从(15)
sh inotify_install.sh
sh rsync.sh
写入周期性计划任务每周备份一次
crontab -e
0 3 * * 0 /usr/bin/sh /root/weekback.sh
nfs(16)
sh inotify_install.sh
sh rsync.sh
每天备份
crontab -e
0 3 * * * /usr/bin/sh nfs_send.sh
测试
可以实时备份服务器开启监控
inotifywait -mrq -e create,delete,move,modify,attrib /backup/(这里写目录:apache,mysql,nginx,nfs)
nginx和http在配置文件目录下创建修改文件查看是否同步,mysql主从服务器和nfs服务器直接执行日备份和周备份脚本查看是否同步过去
所有发送脚本执行一遍,在这四个目录下查看是否有备份文件产生
查看备份目录
nginx缓存
http段加入
proxy_temp_path /data/ngx_cache/proxy_temp_dir;
proxy_cache_path /tmp/ngx_cache/proxy_cache_dir levels=1:2 keys_zone=filecache:30m inactive=1d max_size=100m;
location段加入
proxy_pass http://apache;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache filecache; # 使用缓存区 缓存键空间名要和proxy_cache_path 对应上
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_cache_valid 200 304 12h; #指定状态码的缓存时间
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;
proxy_cache_key $host$uri$is_args$args; #指定键key的格式
proxy_set_header Host $host; #传递主机名给后端节点
proxy_set_header X-Forwarded-For $remote_addr; #传递客户端IP给后端节点
add_header X-Cache-Status "$upstream_cache_status from $server_addr"; #自定义头部信息 crul -I 可以查看缓存命中状态,哪个
服务器响应的。
proxy_headers_hash_max_size 512;
proxy_headers_hash_bucket_size 128;
expires 1d; #过期时间
nginx -s reload 即可
最后可以尝试LAMP配置调优,此处省略
发布者:LJH,转发请注明出处:https://www.ljh.cool/5213.html