Redis作Mysql快取伺服器
阿新 • • 發佈:2018-11-19
Redis作Mysql快取伺服器原理:
php預設從redis索取快取資料,只有redis過期或刪除,php才會從資料庫索求資料。
環境配置:
server1:php代理
server2:redis快取
server3:mysql資料庫
實驗部署:
1、安裝php的redis擴充套件及nginx
[[email protected] redis]# yum install -y php-*
[[email protected] ~]# rpm -ivh nginx-1.8.0-1.el6.ngx.x86_64.rpm
php外掛:
php-5.3.3-38.el6.x86_64.rpm php-cli-5.3.3-38.el6.x86_64.rpm php-common-5.3.3-38.el6.x86_64.rpm php-devel-5.3.3-38.el6.x86_64.rpm php-fpm-5.3.3-38.el6.x86_64.rpm php-gd-5.3.3-38.el6.x86_64.rpm php-mbstring-5.3.3-38.el6.x86_64.rpm php-mysql-5.3.3-38.el6.x86_64.rpm php-pdo-5.3.3-38.el6.x86_64.rpm
2、配置nginx及php
[[email protected] php-fpm.d]# useradd nginx
[[email protected] php-fpm.d]# id nginx
uid=500(nginx) gid=500(nginx) groups=500(nginx)
[[email protected] php-fpm.d]# vim /etc/php-fpm.d/www.conf
39 user = nginx
41 group = nginx
[[email protected] ~]# vim /etc/php.ini
946 date.timezone = Asia/Shanghai
[ [email protected] ~]# /etc/init.d/php-fpm start
示圖:php監聽埠9000
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
10 index index.php index.html index.htm;
30 location ~ \.php$ {
31 root html;
32 fastcgi_pass 127.0.0.1:9000;
33 fastcgi_index index.php;
34 fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
35 include fastcgi_params;
36 }
[[email protected] ~]# /etc/init.d/nginx start
[[email protected] ~]# vim /usr/share/nginx/html/index.php
<?php
phpinfo()
?>
[[email protected] ~]# /etc/init.d/nginx reload
示圖:php測試頁
3、設定server2為redis快取,server3為mysql資料庫
[[email protected] redis]# cp test.php /usr/share/nginx/html/
[[email protected] php.d]# vim /usr/share/nginx/html/test.php
3 $redis->connect('172.25.51.2',6379) or die ("could net connect redis server");
10 $connect = mysql_connect('172.25.51.3','redis','westos');
4、新增php支援的redis
[[email protected] redis]# unzip phpredis-master.zip
[[email protected] redis]# cd phpredis-master
[[email protected] phpredis-master]# phpize
[[email protected] phpredis-master]# ./configure
[[email protected] phpredis-master]# make && make install
[[email protected] ~]# /etc/init.d/php-fpm reload
[[email protected] ~]# cd /etc/php.d/
[[email protected] php.d]# cp mysql.ini redis.ini
[[email protected] php.d]# vim redis.ini
1 extension=redis.so
[[email protected] ~]# /etc/init.d/php-fpm reload
示圖:php支援redis
5、server2開啟redis服務
[[email protected] redis]# scp test.sql server2:
配置redis,server2為Master,server3為Slave
[[email protected] ~]# vim /etc/redis/6379.conf ##server3為server2的slave
283 slaveof 172.25.51.2 6379
[[email protected] ~]# redis-cli
127.0.0.1:6379> info ##檢視redis資訊
6、server3配置mysql資料庫
[[email protected] redis]# scp test.sql server3:
[[email protected] ~]# yum install -y mysql-server
[[email protected] ~]# /etc/init.d/mysqld start
[[email protected] ~]# mysql < test.sql ##將test資料庫匯入mysql
檢視test資料庫
[[email protected] ~]# mysql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
mysql> use test;
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test |
+----------------+
mysql> select * from test;
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
| 6 | test6 |
| 7 | test7 |
| 8 | test8 |
| 9 | test9 |
+----+-------+
mysql> grant all on test.* to [email protected]'%' identified by 'westos';
6、測試:訪問172.25.51.1
1、php預設從redis索取資料,第一次redis無快取,則php從mysql索取資料
2、將資料庫server3內容更新,則php默從redis索取資料,redis不索取資料
server3:更新資料庫
mysql> update test set name='zh' where id=1;
3、將資料庫server3內容更新,redis刪除快取節點資訊,則php從資料庫索取資料
server3:更新資料庫
mysql> update test set name='zh' where id=1;