目的:实现简单的Web服务器的负载均衡+mysql主从
所用环境:CentOS Linux release 7.5.1804 (Core)
规划:
主机名 | IP地址 | 部署服务 | 所用版本 |
---|---|---|---|
proxy | 192.168.4.80/24 | Nginx代理 | nginx/1.12.2 |
web1 | 192.168.4.90/24 | Nginx+Tomcat | nginx/1.12.2 |
web2 | 192.168.4.91/24 | Nginx+Tomcat | nginx/1.12.2 tomcat/9.0.6 |
web3 | 192.168.4.92/24 | Nginx+Tomcat | nginx/1.12.2 tomcat/9.0.6 |
mysql-1 | 192.168.4.93/24 | MySQL主从 | 5.7.17 MySQL Community Server |
mysql-2 | 192.168.4.94/24 | MySQL主从 | 5.7.17 MySQL Community Server |
[root@proxy ~]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@proxy ~]# scp /root/nginx-1.12.2.tar.gz root@192.168.4.91:/root
#后面两台也可以做好一台解压好的Nginx修改过版本信息的直接传目录
[root@proxy ~]# scp /root/nginx-1.12.2.tar.gz root@192.168.4.92:/root
[root@proxy ~]# scp /root/nginx-1.12.2.tar.gz root@192.168.4.93:/root
2.下载MySQL5.7-bundle包并传给需要的主机
[root@proxy ~]# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar
[root@proxy ~]# scp /root/mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar root@192.168.4.93:/root/
[root@proxy ~]# scp /root/mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar root@192.168.4.93:/root/
3.下载Tomcat,并传至web主机
[root@proxy ~]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.20/bin/apache-tomcat-9.0.20.tar.gz
配置好一台Web主机后,直接传文件给其他web主机
[root@proxy nginx-1.12.2]# yum -y install gcc pcre-devel openssl-devel
2.源码包安装Nginx;
[root@proxy ~]# tar -xf nginx-1.12.2.tar.gz
[root@proxy ~]# useradd -s /sbin/nologin nginx
[root@proxy ~]# cd nginx-1.12.2/
[root@proxy nginx-1.12.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
--with-stream
[root@proxy nginx-1.12.2]# vim src/http/ngx_http_header_filter_module.c #修改源码隐藏版本号
static u_char ngx_http_server_string[] = "Server: windowXP" CRLF; #检索Server能快速定位该行
static u_char ngx_http_server_full_string[] = "Server:windowsXP" CRLF;
static u_char ngx_http_server_build_string[] = "Server:windowsXP" CRLF;
:wq!
[root@proxy nginx-1.12.2]# ./configure --user=nginx --group=nginx --with-stream --without-http_autoindex_module
[root@proxy nginx-1.12.2]# make && make install
3.修改配置文件,设置代理;
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
upstream webserver { #设定一个weibserver调度集群
server 192.168.4.90:80 max_fails=2 fail_timeout=30s;
server 192.168.4.91:80 max_fails=2 fail_timeout=30s;
server 192.168.4.92:80 max_fails=2 fail_timeout=30s;
}
……
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://webserver; #代理到http://webserver
proxy_set_header host $host;
root html;
index index.html index.htm;
}
4.做一个Nginx的软链接,开启服务;
[root@proxy nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ #做一个软链接方便使用
[root@proxy nginx-1.12.2]# nginx #开启ngxin服务
[root@proxy nginx-1.12.2]# netstat -tunlp | grep 80 #确定nginx80端口已开启
1.安装Nginx,php-fpm;
[root@web1 nginx-1.12.2]# yum -y install gcc pcre-devel openssl-devel #安装Nginx依赖
[root@web1 nginx-1.12.2]# yum -y install php php-fpm php-mysql #安装PHP对应组件
[root@web1 ~]# tar -xf nginx-1.12.2.tar.gz
[root@web1 ~]# cd nginx-1.12.2/
[root@web1 nginx-1.12.2]# vim src/http/ngx_http_header_filter_module.c
static u_char ngx_http_server_string[] = "Server:WindowXP" CRLF;
static u_char ngx_http_server_full_string[] = "Server:WindowXP" CRLF;
static u_char ngx_http_server_build_string[] = "Server:WindowXP" CRLF; #修改版本信息
[root@web1 nginx-1.12.2]# useradd -s /sbin/nologin nginx
[root@web1 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --without-http_autoindex_module --with-stream --with-http_stub_status_module
[root@web1 nginx-1.12.2]# make && make install
2.修改Nginx配置文件;
[root@web1 nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
......
http {
include mime.types;
default_type application/octet-stream;
upstream tom { #定义一个Tomcat调度
server 127.0.0.1:8080 max_fails=2 fail_timeout=30s;
}
......
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ { #打开location匹配php结尾的,做动静分离
root html;
fastcgi_pass 127.0.0.1:9000; #也可以使用套接字连接,更为高效
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
location ~ \.jsp$ { #匹配以jsp结尾的调度到tom集群
proxy_pass http://tom;
}
......
:wq 保存
3.启动Nginx,php-fpm;
[root@web1 nginx-1.12.2]# /usr/local/nginx/sbin/nginx #启动Nginx服务
[root@web1 nginx-1.12.2]# php -m | grep mysql #确认php支持mysql数据库
mysql
mysqli
pdo_mysql
[root@web1 ~]# cd
[root@web1 ~]# systemctl start php-fpm
[root@web1 ~]# ss -tunlp | grep 80
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=4002,fd=6),("nginx",pid=4001,fd=6))
[root@web1 ~]# ss -tunlp | grep 9000
tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=4018,fd=0),("php-fpm",pid=4017,fd=0),("php-fpm",pid=4016,fd=0),("php-fpm",pid=4015,fd=0),("php-fpm",pid=4014,fd=0),("php-fpm",pid=4012,fd=6))
[root@web1 ~]# vim /usr/local/nginx/html/mysql.php #手动写一个php连接mysql测试页面
<?php
$mysqli = new mysqli('192.168.4.93','root','asd123...Z','mysql'); #IP,用户,密码,库
if (mysqli_connect_errno()){
die('Unable to connect!'). mysqli_connect_error();
}
$sql = "select * from user";
$result = $mysqli->query($sql);
while($row = $result->fetch_array()){
printf("Host:%s",$row[0]);
printf("");
printf("Name:%s",$row[1]);
printf("");
}[root@web1 ~]# echo 192.168.4.91 > /usr/local/nginx/html/index.html
?>
:wq 保存,还不能测试因为MySQL服务器还没有搭建好!
4.安装Tomcat;
[root@web1 ~]# yum -y install java-1.8.0-openjdk.x86_64 #安装支持java环境(所有主机都需执行)
[root@web1 ~]# tar -xf apache-tomcat-9.0.6.tar.gz
[root@web1 ~]# mv apache-tomcat-9.0.6 /usr/local/tomcat
[root@web1 ~]# echo tom > /usr/local/tomcat/webapps/ROOT/1.jsp #写入一个测试页面
[root@web1 ~]# /usr/local/tomcat/bin/startup.sh #开启tomcat服务
[root@web1 ~]# curl http://localhost:8080/1.jsp #访问测试
tom
5.后续操作,将nginx目录,tomcat目录,传给其他Web服务器,免除复杂操作;
[root@web1 ~]# scp -r /usr/local/nginx root@192.168.4.93:/usr/local/ #将Nginx目录传给其他Web服务器
[root@web1 ~]# scp -r /usr/local/nginx root@192.168.4.94:/usr/local/
[root@web1 ~]# scp -r /usr/local/tomcat root@192.168.4.93:/usr/local/ #将Tomcat直接传到其他web服务器
[root@web1 ~]# scp -r /usr/local/tomcat root@192.168.4.94:/usr/local/
[root@web2 ~]# yum -y install gcc pcre-devel openssl-devel
[root@web2 ~]# yum -y install php php-fpm php-mysql #安装PHP对应组件
[root@web2 ~]# useradd -s /sbin/nologin nginx
[root@web2 ~]# /usr/local/nginx/sbin/nginx #启动Nginx服务
[root@web1 ~]# echo 192.168.4.91 > /usr/local/nginx/html/index.html
[root@web2 ~]# yum -y install java-1.8.0-openjdk.x86_64 #安装支持java环境(所有主机都需执行)
[root@web2 ~]# /usr/local/tomcat/bin/startup.sh #开启tomcat服务
[root@web2 ~]# systemctl start php-fpm #启动php-fpm
[root@web2 ~]# systemctl enable php-fpm #设置php-fpm开机自启
一定要记得开启3个服务!
[root@web2 ~]# ss -tunlp | grep java
tcp LISTEN 0 1 ::ffff:127.0.0.1:8005 :::* users:(("java",pid=25894,fd=75))
tcp LISTEN 0 100 :::8009 :::* users:(("java",pid=25894,fd=53))
tcp LISTEN 0 100 :::8080 :::* users:(("java",pid=25894,fd=48))
[root@web2 ~]# ss -tunlp | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=25853,fd=6),("nginx",pid=25852,fd=6))
[root@web2 ~]# ss -tunlp | grep 9000
tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=26045,fd=0),("php-fpm",pid=26044,fd=0),("php-fpm",pid=26043,fd=0),("php-fpm",pid=26042,fd=0),("php-fpm",pid=26041,fd=0),("php-fpm",pid=26039,fd=6))
[root@mysql-1 ~]# ls #查看mysql5.7.bundle包是否在
[root@mysql-1 ~]# mkdir mysql5 #当前目录下创建个目录(因为bundle包解压出来东西太多了)
[root@mysql-1 ~]# tar -xf mysql-5.7.17-1.el7.x86_64.rpm-bundle.tar -C /root/mysql5
[root@mysql-1 ~]# cd /root/mysql5/
[root@mysql-1 mysql5]# ls
mysql-community-client-5.7.17-1.el7.x86_64.rpm
mysql-community-common-5.7.17-1.el7.x86_64.rpm
mysql-community-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
mysql-community-server-5.7.17-1.el7.x86_64.rpm
mysql-community-test-5.7.17-1.el7.x86_64.rpm
[root@mysql-1 mysql5]# yum -y install /root/mysql5/mysql-community-*.rpm #安装mysql的所有rpm包
安装时间较长,需耐心等待一阵子~
完毕!
2.修改初始密码;
[root@mysql-1 mysql5]# systemctl start mysqld #开启mysql服务(mysql服务初次启动初始密码在日志中)
[root@mysql-1 mysql5]# grep password /var/log/mysqld.log
2019-05-26T10:21:52.818781Z 1 [Note] A temporary password is generated for root@localhost: <9MAc!Xl1*oo
[root@mysql-1 mysql5]# mysql -uroot -p'<9MAc!Xl1*oo' #使用初始密码登陆
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> alter user user() identified by "asd123...Z"; #第一步先修改密码(只有修改密码了才能做其他操作,修改为Web测试页面中对应的)
Query OK, 0 rows affected (0.00 sec)
mysql> exit #退出,修改配置文件,做从库设置
3.更改配置文件,开启从库设置;
[root@mysql-2 mysql5]# systemctl stop mysqld
[root@mysql-2 mysql5]# vim /etc/my.cnf
[mysqld]
server_id=93 #在其中加入这行,开启服务器id(我这里使用的ip最后一位可以自定义多台mysql集群id别重复就行)
log_bin=master93 #开启binlog日志,并自定义binlog日志名(如做一个纯从库可以不用这步)
……
:wq 保存退出
[root@mysql-2 mysql5]# systemctl restart mysqld #重启MySQL服务
4.授权一个slave用户;
[root@mysql-1 mysql5]# mysql -uroot -p'asd123...Z'
mysql> create user testslave@"%" identified by "asd123...Z"; #创建一个mysql用户(mysql-5.7也可以直接授权,免去创建,但8.0开始不支持直接授权,需先创建用户
mysql> grant replication slave on *.* to testslave@"%"; #授权一个只能复制binlog日志的用户
mysql> show grants for testslave@"%"; #查看权限是否正确
5.查看主库master状态;
mysql> show master status; #主库上查看binlog日志状态
+----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| master93.000001 | 605 | | | | #偏移量可能不同,根据自己情况
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> exit #已完成主库所有操作
[root@mysql-2 ~]# ls #查看mysql5.7.bundle包是否在
[root@mysql-2 ~]# mkdir mysql5 #当前目录下创建个目录(因为bundle包解压出来东西太多了)
[root@mysql-2 ~]# tar -xf mysql-5.7.17-1.el7.x86_64.rpm-bundle.tar -C /root/mysql5
[root@mysql-2 ~]# cd /root/mysql5/
[root@mysql-2 mysql5]# ls
mysql-community-client-5.7.17-1.el7.x86_64.rpm
mysql-community-common-5.7.17-1.el7.x86_64.rpm
mysql-community-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
mysql-community-server-5.7.17-1.el7.x86_64.rpm
mysql-community-test-5.7.17-1.el7.x86_64.rpm
[root@mysql-2 mysql5]# yum -y install /root/mysql5/mysql-community-*.rpm #安装mysql的所有rpm包
安装时间较长,需耐心等待一阵子~
完毕!
2.修改初始密码;
[root@mysql-2 mysql5]# systemctl start mysqld #开启mysql服务(mysql服务初次启动初始密码在日志中)
[root@mysql-2 mysql5]# grep password /var/log/mysqld.log
2019-05-26T10:21:52.818781Z 1 [Note] A temporary password is generated for root@localhost: <9MAc!Xl1*oo
[root@mysql-2 mysql5]# mysql -uroot -p'<9MAc!Xl1*oo' #使用初始密码登陆
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> alter user user() identified by "asd123...Z"; #第一步先修改密码(只有修改密码了才能做其他操作)
Query OK, 0 rows affected (0.00 sec)
mysql> exit #退出,修改配置文件,做从库设置
3.更改配置文件,开启从库设置;
[root@mysql-2 mysql5]# systemctl stop mysqld
[root@mysql-2 mysql5]# vim /etc/my.cnf
[mysqld]
server_id=94 #在其中加入这行,开启服务器id(我这里使用的ip最后一位可以自定义多台mysql集群id别重复就行)
log_bin=slave94 #开启binlog日志,并自定义binlog日志名(如做一个纯从库可以不用这步)
log_slave_updates #将slave中继日志同步到binlog日志(如做一个纯从库可以不用这步),我打开是为了以后是否继续做主从从结构lianjie
……
:wq 保存退出
[root@mysql-2 mysql5]# systemctl restart mysqld #重启MySQL服务
4.查看slave状态;
[root@mysql-2 mysql5]# mysql -uroot -p'asd123...Z'
mysql> show slave status\G; #查看slave状态,目前没有
Empty set (0.00 sec)
5.从库上配置主库信息,完成主从同步。
mysql> change master to #从库上配置
-> master_host="192.168.4.93",master_user="testslave",master_password="asd123...Z",
-> master_log_file="master93.000001",master_log_pos=605; #对应的log日志文件名,与偏移量写主库中对应的值
Query OK, 0 rows affected, 2 warnings (0.59 sec)
mysql> start slave; #开启slave从库模式
6.再次查看slave状态;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_Stat,Nginx,Tomcat需要开机自动启动,可以放到e: Waiting for master to send event
Master_Host: 192.168.4.93
Master_User: testslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master93.000001
Read_Master_Log_Pos: 154 #这里偏移量对不上,只是因为我第一次主库binlog做错了,你们按步骤走没问题
Relay_Log_File: mysql-2-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master93.000001
Slave_IO_Running: Yes #IO线程 yes
Slave_SQL_Running: Yes #SQL线程 yes 两个线程均yes则成功
......
7.测试主从同步效果(主库执行命令);
#主库操作
mysql> grant all on *.* to root@"%" identified by "asd123...Z"; #主库执行一个授权并创建所有地点登录的root用户
mysql> create database test; #创建一个测试库
#从库验证
mysql> select user,host from mysql.user; #查看user表中所有地点登录的root用户是否存在
+-----------+-----------+
| user | host |
+-----------+-----------+
| root | % |
| mysql.sys | localhost |
| root | localhost |
+-----------+-----------+
3 rows in set (0.00 sec)
mysql> show databases; #查看创建的test库是否存在
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
主从同步完成!
[root@web1 ~]# yum -y install mariadb #简单安装一个mariadb,因为需要使用mysql命令连接数据库
[root@web1 ~]# mysql -h192.168.4.93 -uroot -p'asd123...Z' #访问mysql主库测试能否访问
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
2.测试Nginx负载均衡是否正确;
每台主机写入一个不同内容的页面,方便测试
[root@web1 ~]# echo 192.168.4.90 nginx > /usr/local/nginx/html/index.html
[root@web1 ~]# echo tomcat 4.90 > /usr/local/tomcat/webapps/ROOT/test.jsp
[root@web2 ~]# echo 192.168.4.91 nginx > /usr/local/nginx/html/index.html
[root@web2 ~]# echo tomcat 4.91 > /usr/local/tomcat/webapps/ROOT/test.jsp
[root@web3 ~]# echo 192.168.4.92 nginx > /usr/local/nginx/html/index.html
[root@web3 ~]# echo tomcat 4.92 > /usr/local/tomcat/webapps/ROOT/test.jsp
[root@client ~]# curl http://192.168.4.80
192.168.4.92 nginx
[root@client ~]# curl http://192.168.4.80
192.168.4.90 nginx
[root@client ~]# curl http://192.168.4.80
192.168.4.91 nginx
[root@client ~]# curl http://192.168.4.80
192.168.4.92 nginx
[root@client ~]# curl http://192.168.4.80
192.168.4.90 nginx
[root@client ~]# curl http://192.168.4.80
192.168.4.91 nginx
[root@client ~]# curl http://192.168.2.254/test.jsp
tomcat 4.92
[root@client ~]# curl http://192.168.2.254/test.jsp
tomcat 4.90
[root@client ~]# curl http://192.168.2.254/test.jsp
tomcat 4.91
[root@client ~]# curl -I http://192.168.2.254/mysql.php
HTTP/1.1 200 OK #状态码200正常
Server:WindowsXP
Date: Fri, 31 May 2019 11:09:21 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.4.16
[root@client ~]# curl http://192.168.2.254/mysql.php
Host:localhost</br>Name:root</br>Host:localhost</br>Name:mysql.sys</br>Host:%</br>Name:testslave</br>Host:%</br>Name:root</br>
#根据结果确认nginx调度正常
所有服务如需长期运行,可设置开机自动启动,mysqld,php-fpm,相应主机执行即可
Nginx与Tomcat则可以放到/etc/rc.lcoal下设置开机自动启动
[root@web1 ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@mysql-1 ~]# systemctl enable mysqld
[root@web1 ~]# vim /etc/rc.local
/usr/local/nginx/sbin/nginx
/usr/local/tomcat/bin/startup.sh
:wq 保存
[root@web1 ~]# chmod +x /etc/rc.local #加执行权限,注意/etc/rc.local为/etc/rc.d/rc.local的链接的权限
[root@web1 ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 1月 26 12:34 /etc/rc.local -> rc.d/rc.local #
第一次写博客,感谢大家的访问,很简单的结构,有很多做得不好的地方,也希望可以跟大家交流学习一下;
也还有很多可以优化的地方,希望后续再通过博客与大家分享,共同学习.