超詳細搭建百萬 pv 架構 (內附軟件包)

網站架構概述

       網站架構一般認爲是根據客戶需求分析的結果,準確定位網站目標羣體,設置網站的整體架構,規劃、設計網站欄目及其內容,制定網站開發流程的順序,最大限度地進行高效資源分配與管理的設計。

        PV(Page View,頁面瀏覽量)即點擊量,通常是衡量一個網絡新聞頻道或者網站的主要指標。對PV 的解釋是,一個訪問者在24 小時內到底看了網站的幾個頁面。這裏需要注意的是。同一個人瀏覽的同一頁面,不重複計算 PV 量。

案例概述

   本次案例設計採用四層模式實現,主要分爲前端反向代理、Web層、數據庫緩存和數據庫層。前端反向代理層採用主備模式,Web層採用羣集模式,數據庫緩存層採用主備模式,數據庫層採用主從模式。

       拓撲架構如圖所示,實線是正常情況下的數據流向連接,虛線是異常情況下的數據流向連接。

0.png

案例環境,如表所示。

主機名IP 地址系統版本用途
Master192.168.66.138
centos 7 (64 位)前端反向代理主機、redis 緩存主機、Mysql 數據庫主庫
Backup192.168.66.139centos 7 (64 位)前端反向代理備機、redis 緩存主機、Mysql 數據庫從庫
tomcat 1192.168.66.136centos 7 (64 位)Web 服務
tomcat 2192.168.66.137centos 7 (64 位)Web 服務
使用所需的軟件包可使用此鏈接獲得:

鏈接:https://pan.baidu.com/s/1JgWf8Zrc6KZsAU1-7-Lsug 

提取碼:0m8z

實施步驟

1.在前端搭建 Nginx 反向代理和 Keeepalived

(1)安裝帶有 nginx  rpm 軟件包 的源(主從都要安裝),關閉防火牆。

[root@backup ~]# systemctl stop firewalld
[root@backup ~]# setenforce 0
[root@backup ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
獲取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
警告:/var/tmp/rpm-tmp.bQAX0y: 頭V4 RSA/SHA1 Signature, 密鑰 ID 7bd9bf62: NOKEY
準備中...                          ################################# [100%]
正在升級/安裝...
    1:nginx-release-centos-7-0.el7.ngx ################################# [100%]

(2)使用centos 默認倉庫完成下面的安裝 keepalived 和 nginx

[root@backup ~]# yum install -y keepalived nginx  

(3)編輯前端代理主機 keepalived 配置文件(主服務器:master)

[root@master ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
vrrp_script nginx {                                          //nginx 的觸發腳本,需要創建/opt/nginx.sh
      script "/opt/shell/nginx.sh"
      interval 2

global_defs {

router_id NGINX_HA                             //注意:router_id 羣集名稱,從服務器羣集名稱爲:NGINX_HB
}

vrrp_instance VI_1 {
     state MASTER                                  //主服務器狀態爲 MASTER, 從服務器的狀態爲:BACKUP
     interface eNS33
     virtual_router_id 51                       //虛擬 id 主從也要不一樣,從爲52
     priority 100                                       //優先級 ,從服務器的優先級要小於主服務器的優先級
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     track_script {                                  //觸發 nginx 腳本
         nginx
     }

     virtual_ipaddress {
         192.168.66.100                          //虛擬 IP
         192.168.200.100
     }
}

(4)編輯nginx的啓動腳本

[root@master ~]# mkdir /opt/shell
[root@master ~]# vim /opt/shell/nginx.sh

#!/bin/bash   
k=`ps -ef | grep keepalived | grep -v grep | wc –l`             //查看當前所有進程,過濾 keepalived 進程,反向過濾 grep 進程
if [ $k -gt 0 ];then
         /bin/systemctl start ngingx.service                             //啓動nginx 服務
fi

給腳本執行權限

[root@master ~]# chmod +x /opt/shell/nginx.sh

從服務器的 keepalived 配置文件與主服務器基本一致,注意 route id 、state、和優先級有區別

(5)配置nginx前端調度功能,nginx 反向代理 tomcat (主從服務器配置一樣)。

[root@master shell]# vim /etc/nginx/nginx.conf                   (在gzip on 下方,添加以下內容)

#gzip  on;
    upstream tomcat_pool {                               //節點服務器 tomcat 池
               server 192.168.66.136:8080;             //節點 Web 服務器 IP 地址,Tomcat默認端口8080
               server 192.168.66.137:8080;
               ip_hash;                                                 // 會話穩固功能,否則無法通過vip地址登陸  
        }
        server {
               listen 80;
               server_name 192.168.66.100;           //虛擬 IP 地址        
               location / {
                       proxy_pass http://tomcat_pool;
                       proxy_set_header X-Real-IP $remote_addr;
               }
        }

檢查nginx的配置文件語法

[root@master shell]# nginx -t -c /etc/nginx/nginx.conf              //檢查nginx的配置文件語法
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

(6)啓動 keepalived 服務,nginx 服務夜壺自動啓動

[root@master ~]# systemctl start keepalived.service
[root@master ~]# netstat -ntap | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      43058/nginx: master

2.配置節點服務器,在 Web 層安裝 Tomcat 服務

(1)解壓 jdk 和 tomcat 軟件包(關閉防火牆)

[root@promote pv]# tar zxvf jdk-8u144-linux-x64.tar.gz -C /opt/

[root@promote pv]# tar zxvf apache-tomcat-8.5.23.tar.gz -C /opt/

將解壓後的軟件包移動到 /usr/local/目錄下,並重命名

[root@promote opt]# mv jdk1.8.0_144/ /usr/local/java
[root@promote opt]# mv apache-tomcat-8.5.23/ /usr/local/tomcat8
[root@promote opt]# cd /usr/local/
[root@promote local]# ls
bin  etc  games  include  java  lib  lib64  libexec  sbin  share  src  tomcat8

(2)添加環境變量,使系統能夠識別,能夠使用 java 中的命令

[root@promote local]# vim /etc/profile

export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib

生效環境變量,查看 java 版本

[root@promote local]# source /etc/profile
[root@promote local]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)

(3)啓動 tomcaT 服務

[root@promote local]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[root@promote local]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown


[root@promote local]# tomcatup                            //使用命令 tomcatup
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /use/local/java/jre
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.

查看 tomcat 默認端口是否啓動

[root@promote local]# netstat -ntap | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      58040/java         
tcp6       0      0 192.168.66.136:8080     192.168.66.1:52004      FIN_WAIT2   -                  
tcp6       0      0 192.168.66.136:8080     192.168.66.1:52003      FIN_WAIT2   -         

(4)在瀏覽器測試 tomcat 網站

1.png      

更改測試首頁(節點服務器1),網站首頁路徑

[root@promote webapps]# cd /usr/local/tomcat8/webapps/
[root@promote webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@promote webapps]# cd ROOT
[root@promote ROOT]# ls
asf-logo-wide.svg  bg-nav-item.png  favicon.ico        tomcat.css  tomcat-power.gif
bg-button.png      bg-nav.png       index.jsp          tomcat.gif  tomcat.svg
bg-middle.png      bg-upper.png     RELEASE-NOTES.txt  tomcat.png  WEB-INF
[root@promote ROOT]# mv index.jsp index.jsp.bk            //將原來的默認網站首頁重命名

重新編輯網站默認首頁

[root@promote ROOT]# vim index.jsp

<h1>server 136!!!</h1>

 驗證頁面

2.png

(5)節點服務器2的配置與1相同,測試網站

3.png

編輯默認首頁

[root@promote ROOT]# vim index.jsp

<h1>server 137!!!</h1>

驗證首頁

4.png

最後通過前端反向代理虛擬 IP 地址訪問測試 http://192.168.66.100  如圖所示

5.jpg

模擬故障,關閉節點服務器2,再次訪問,看能否訪問到節點服務器1 的首頁 

[root@tomcat2 bin]#tomcatdown 

Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr/local/java/jre
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar

6.jpg

從上圖中我們可以看到虛擬 IP 沒有變化,但是後端響應的服務器已經改變,說明 Web 服務器羣集配置完成

(6) 編輯 server.xml 文件

[root@promote ROOT]# cd /usr/local/tomcat8/conf/
[root@promote conf]# ls
Catalina             context.xml           logging.properties  tomcat-users.xsd
catalina.policy      jaspic-providers.xml  server.xml          web.xml
catalina.properties  jaspic-providers.xsd  tomcat-users.xml
[root@promote conf]# vim server.xml


7.jpg

3.在主從服務器上部署mysql數據庫

(1)使用 yum 方式安裝 mariadb 數據庫,兩臺服務器上都要安裝

[root@master ~]# yum install -y mariadb-server mariadb

啓動數據庫,並查看端口是否開啓

[root@master ~]# systemctl enable mariadb.service                   //開機自啓動
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@master ~]# systemctl start mariadb.service                      //啓動數據庫
[root@master ~]# netstat -ntap | grep 3306                      
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      78702/mysqld

(2)進行常規安全設置,使用命令 mysql_secure_installation 

[root@master ~]# mysql_secure_installation

              …….


Enter current password for root (enter for none):          //按回車進入
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y               //設置數據庫 root 用戶密碼 ,yes
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
  ... Success!

Remove anonymous users? [Y/n] n            //是否清除其匿名用戶, no
  ... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y      //管理員本地登錄,yes 
  ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n         //刪除測試數據庫,no
  ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y               //重新加載 tables ,yes
  ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!                     //mariadb 數據庫安裝完成。

(3)登錄數據庫

[root@master ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;                //查看數據庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

(4)導入數據庫,(主數據庫和從數據庫都要導入)。

[root@master ~]# mkdir /aaa
[root@master ~]# mount.cifs //192.168.66.1/rhel7 /aaa              //掛載需要導入的數據庫
Password for root@//192.168.66.1/rhel7: 
[root@master pv]# ls
apache-jmeter-4.0            JDK1.8.0                    slsaledb-2014-4-10.sql
apache-jmeter-4.0.zip        jdk-8u144-linux-x64.tar.gz  SLSaleSystem.tar.gz
apache-tomcat-8.5.23.tar.gz  mysql-5.6.26.tar.gz

導入數據庫並查看

[root@master pv]# mysql -u root -p < slsaledb-2014-4-10.sql
Enter password:              

登錄數據庫查詢

[root@master pv]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;               //查看數據庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| slsaledb           |                  //數據庫導入成功
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> use slsaledb;            //進入slsabledb 數據庫
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [slsaledb]> show tables;          //查看錶
+-------------------------+
| Tables_in_slsaledb      |
+-------------------------+
| BASICS_PARAMETER        |
| INFO_ANNEXES            |
| INVENTORY               |
| MULTI_LAN               |
| ORDER_INFO              |
| ORDER_LIST              |
| USER_ACCOUNT_201312     |
| USER_ACCOUNT_201404     |
| USER_ACCOUNT_LOG_201404 |
| USER_BUY                |
| USER_BUY_BONUS          |
| USER_CASH               |
| USER_PAIR_201312        |
| USER_PLACE              |
| USER_POINT              |
| USER_POINT_GOODS        |
| USER_RECHARGE           |
| USER_REFER              |
| affiche                 |
| au_authority            |
| au_function             |
| au_role                 |
| au_user                 |
| data_dictionary         |
| goods_info              |
| goods_pack              |
| goods_pack_affiliated   |
| information             |
| leave_message           |
| reply                   |
| uploadtemp              |
+-------------------------+
31 rows in set (0.01 sec)

從數據庫也要導入數據庫,操作一樣。

(5)在數據庫上給網上商城項目授權使用導入數據庫 slsaledb  (主從數據庫都要授予權限)

MariaDB [(none)]> GRANT all ON slsaledb.* TO 'root'@'%' IDENTIFIED BY 'abc123';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;                //刷新數據庫
Query OK, 0 rows affected (0.00 sec)  


4在tomcat節點服務器上部署商城項目(兩個節點服務器都要做)


(1)解壓商城項目到 /usr/local/tomcat8/webapps/

[root@promote pv]# tar zxvf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/

編輯商城項目中的文件,路徑爲。


9.jpg

8.jpg

(2)重啓tomcat

[root@promote classes]# tomcatdown
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr/local/java/jre
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
[root@promote classes]# tomcatup
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr/local/java/jre
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.

(3)在瀏覽器上測試 tomcat 節點是否能正常訪問 SL會員商城,輸入用戶名:admin ,密碼:123456

10.jpg

11.jpg

在訪問另一個 tomcat 節點

13.jpg

使用虛擬 IP 地址訪問

12.jpg

5接下來我們開始安裝並配置 redis 主從緩存服務器。

(1)Redis 簡介

      Redis 是一個高性能的 key-value 數據庫,和 Memcached 類似,但它支持的 value 類型更多。與 Memcached 一樣,爲了保證效率,數據都是緩存再去愛內存中。區別是 redis 會週期性地把更新的數據寫入磁盤或者把修改操作寫入追加的記錄文件中,並且在此基礎上實現了 master-slave (主從)同步。

       Redis 的出現,很大程度上補償了 Memcached 這類 key-value 存儲的不足,在部分場合可以對關係型數據庫起到很好的補償作用。

(2)安裝並配置 redis 

[root@master pv]# yum install -y epel-release

[root@master pv]# yum install redis –y

修改主緩存服務器的 redis 主配置文件 /etc/redis.conf 中的 redis 監聽端口

[root@master pv]# vim /etc/redis.conf

61 bind 0.0.0.0             //默認所有網段都能訪問(61行)

port 6379     //默認端口(不用更改)

主緩存服務器啓動服務

[root@master pv]# systemctl start redis.service
[root@master pv]# netstat -ntap | grep 6379
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      99160/redis-server 

客戶端連接主緩存服務器

[root@master pv]# redis-cli -h 192.168.66.138 -p 6379

手動插入一條數據庫並查詢

192.168.66.138:6379> set name test
OK
192.168.66.138:6379> get name
"test"

修改從緩存服務器修改主配置文件 /etc/redis.conf 。需要在 265 # slaveof <masterip> <masterport> 行下面增加主緩存服務地址。

[root@master pv]# vim /etc/redis.conf

61 bind 0.0.0.0             //默認所有網點都能訪問

265 # slaveof <masterip> <masterport>
266   slaveof 192.168.66.138 6379           //添加主服務器地址

從緩存服務器啓動服務

[root@backup pv]# systemctl start redis.service
[root@backup pv]# netstat -antp | grep 6379
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      44175/redis-server 
tcp        0      0 192.168.66.139:45519    192.168.66.138:6379     ESTABLISHED 44175/redis-server 

在從緩存服務器上,使用 redis 客戶端連接連接從緩存 redis 服務,查詢剛在朱緩存服務器上插入的 name 值。

[root@backup pv]# redis-cli -h 192.168.66.139 -p 6379
192.168.66.139:6379> get name
"test"

結果說明主從緩存服務器同步正常。測試沒問題後刪除剛剛插入的值,使用命令 del name 即可。

(3)回到 tomxat 節點服務器上配置商城項目中連接redis的參數(兩臺tomcat 節點服務器都要做)

vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml

47                 <constructor-arg value="192.168.66.100"/>           //連接地址給爲虛擬 IP
48                 <constructor-arg value="6379"/>                          //端口默認

(4)在 redis 緩存服務器的客戶端使用虛擬 IP 測試緩存結果

[root@master pv]# redis-cli -h 192.168.66.100 -p 6379
192.168.66.100:6379> info

       ........

keyspace_hits:1          //命中數

keyspace_misses:0     //未命中數

登錄商城,然後反覆點擊需要數據庫參與的操作頁面,再回來檢查keyspace_hits或者keyspace_misses: 值變化。

(5)配置 redis 羣集主從切換(注意:只在 redis 主緩存服務器上做

首先獲取當前服務器的角色

[root@master pv]# redis-cli -h 192.168.66.138 info Replication
# Replication
role:master              //當前服務器爲主緩存服務器
connected_slaves:1            
slave0:ip=192.168.66.139,port=6379,state=online,offset=1527,lag=0
master_repl_offset:1527
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1526

配置redis集羣主從切換---只在主服務器上操作

[root@master pv]# vim /etc/redis-sentinel.conf

17  protected-mode no                //開啓羣集功能(第17行,去掉前面的 #)      

69 sentinel monitor mymaster 192.168.66.138 6379 1           //設置主服務器,最後的 1 表示:有一臺從服務器

98 sentinel down-after-milliseconds mymaster 3000             //故障切換時間單位是毫秒

啓動 redis 羣集功能(在主從服務器上都要開啓)

[root@master pv]# service redis-sentinel start
Redirecting to /bin/systemctl start redis-sentinel.service
[root@master pv]# netstat -ntap | grep 26379
tcp        0      0 0.0.0.0:26379           0.0.0.0:*               LISTEN      111984/redis-sentin
tcp6       0      0 :::26379                :::*                    LISTEN      111984/redis-sentin

查看羣集信息

[root@master pv]# redis-cli -h 192.168.66.138 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.66.138:6379,slaves=1,sentinels=3            //此時主服務器還是138

驗證主從切換,關閉主服務器的 redis

[root@master pv]# systemctl stop redis
[root@master pv]# redis-cli -h 192.168.66.138 -p 26379 info Sentinel         //查看羣集狀態
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.66.139:6379,slaves=1,sentinels=3         //此時主服務器以切換到139

再次啓動主服務器的 redis ,看是否恢復爲主

[root@master pv]# systemctl start redis

[root@master pv]# redis-cli -h 192.168.66.138 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=odown,address=192.168.66.139:6379,slaves=1,sentinels=3

       結果顯示當主服務器出現故障時,從服務器會自動切換爲主服務器,當原來的主服務器重新上線啓動,並不能搶佔爲 master,只有當現在的主服務器宕機,才能恢復爲master,說明 redis 緩存服務器羣集的主從切換配置成功。

驗證主從同步

[root@master pv]# redis-cli -h 192.168.66.138 -p 6379
192.168.66.138:6379> set name2 test2
OK
192.168.66.138:6379> get name2
"test2"

在從服務器上查看

[root@backup pv]# redis-cli -h 192.168.66.139 -p 6379
192.168.66.139:6379> get name2
"test2"

6.配置mysql 主從(在主從服務器上都做)

[root@master pv]# vim /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1                      //從服務器的 server_id=2 ,其他與主相同                           
log_slave_updates=true
sync_binlog=1

修改配置文具後,需要重啓數據庫

[root@master pv]# systemctl restart mariadb
[root@master pv]# netstat - ntap | grep 3306

tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      45543/mysqld     

登錄數據庫  

[root@master pv]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 42
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show master status;         //查看 master 狀態,記錄日誌文件名稱和 位置值

 +------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000002 |      245 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to 'rep'@'192.168.66.%' identified by '123456';      //給訪問權限
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> flush privileges;           //刷新
Query OK, 0 rows affected (0.01 sec)

從數據庫也要修改配置文件,並重啓服務在從服務器上指向主服務器

MariaDB [(none)]> change master to master_host='192.168.66.138',master_user='rep',master_password='123456',master_log_file='mysql_bin.000002',master_log_pos=245;

Query OK, 0 rows affected (0.06 sec)

MariaDB [(none)]> start slave;             //開啓 slave
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G;       //查看 slave狀態
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 192.168.66.138
                   Master_User: rep
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql_bin.000002
           Read_Master_Log_Pos: 625
                Relay_Log_File: mariadb-relay-bin.000002
                 Relay_Log_Pos: 909
         Relay_Master_Log_File: mysql_bin.000002
              Slave_IO_Running: Yes              //這兩項都爲 Yes
             Slave_SQL_Running: Yes

               Replicate_Do_DB:
           Replicate_Ignore_DB:
            Replicate_Do_Table:

mysql數據庫主從配置完成,整個架構部署完成。

使用虛擬IP 登錄會員商城

15.jpg



查看 redis 命中數

14.jpg

登錄商城,然後反覆點擊需要數據庫參與的操作頁面,再回來檢查keyspace_hits或者keyspace_misses: 值變化。說明redis 已經在運行





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章