redis+mysql实现lnmp架构缓存

配置环境:redhat6.5
server1:redis(172.25.254.1)
server2:php(172.25.254.2)
server3:mysql(172.25.254.3)
配置步骤:

一、mysql+redis简单配置

server2:

1、server2安装php的redis相应模块

redis+mysql实现lnmp架构缓存_第1张图片

2、nginx安装

[root@server2 php-fpm.d]# rpm -ivh nginx-1.8.0-1.el6.ngx.x86_64.rpm 
warning: nginx-1.8.0-1.el6.ngx.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing...                ########################################### [100%]
   1:nginx                  ########################################### [100%]
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
[root@server2 php-fpm.d]# id nginx
uid=498(nginx) gid=499(nginx) groups=499(nginx)

3、nginx和php配置

1、php配置

[root@server2 php-fpm.d]# cd /etc/php-fpm.d/
[root@server2 php-fpm.d]# id nginx
uid=498(nginx) gid=499(nginx) groups=499(nginx)
[root@server2 php-fpm.d]# vim www.conf 
 39 user = nginx
 41 group = nginx
 [root@server2 php-fpm.d]# vim /etc/php.ini
 946 date.timezone = Asia/Shanghai
[root@server2 php-fpm.d]# /etc/init.d/php-fpm start
Starting php-fpm:                                          [  OK  ]
[root@server2 php-fpm.d]# netstat -antlp | grep php
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      1125/php-fpm        
[root@server2 php-fpm.d]# vim /etc/php.ini 

2、nginx配置

[root@server2 ~]# cd /etc/nginx/conf.d/
[root@server2 conf.d]# ls
default.conf  example_ssl.conf
[root@server2 conf.d]# vim 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     }
[root@server2 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@server2 conf.d]# nginx 
[root@server2 conf.d]# netstat -anplt |grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1141/nginx          

redis+mysql实现lnmp架构缓存_第2张图片
php测试:

[root@server2 conf.d]# cd /usr/share/nginx/html/
[root@server2 html]# vim index.php
[root@server2 html]# cat index.php 

[root@server2 html]# /etc/init.d/php-fpm reload
Reloading php-fpm: [14-Jul-2018 01:09:13] NOTICE: configuration file /etc/php-fpm.conf test is successful

                                                           [  OK  ]

物理机访问:
redis+mysql实现lnmp架构缓存_第3张图片

4、php配置redis+mysql

[root@server2 ~]# cd /usr/share/nginx/html/
[root@server2 html]# vim test.php 
  1 connect('172.25.254.1',6379) or die ("could net connect redi    s server");
  4   #      $query = "select * from test limit 9";
  5         $query = "select * from test";
  6         for ($key = 1; $key < 10; $key++)
  7         {
  8                 if (!$redis->get($key))
  9                 {
 10                         $connect = mysql_connect('172.25.254.3','redis','wes    tos');
 11                         mysql_select_db(test);
 12                         $result = mysql_query($query);
 13                         //如果没有找到$key,就将该查询sql的结果缓存到redis
 14                         while ($row = mysql_fetch_assoc($result))
 15                         {
 16                                 $redis->set($row['id'],$row['name']);
 17                         }
 18                         $myserver = 'mysql';
 19                         break;
 20                 }
 21                 else
 22                 {
 23                         $myserver = "redis";
 24                         $data[$key] = $redis->get($key);
 25                 }
 26         }
 27 
 28         echo $myserver;
 29         echo "
"; 30 for ($key = 1; $key < 10; $key++) 31 { 32 echo "number is $key"; 33 34 echo "
"; 35 36 echo "name is $data[$key]" ; 37 38 echo "
"; 39 } 40 ?>

5、添加php支持的redis模块

[root@server2 ~]# unzip phpredis-master.zip 
[root@server2 ~]# cd phpredis-master
[root@server2 phpredis-master]# phpize 
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[root@server2 phpredis-master]# ls
acinclude.m4    config.sub      library.c         README.markdown
aclocal.m4      configure       library.h         redis.c
autom4te.cache  configure.in    ltmain.sh         redis_session.c
build           CREDITS         Makefile.global   redis_session.h
common.h        debian          missing           run-tests.php
config.guess    debian.control  mkdeb-apache2.sh  serialize.list
config.h.in     igbinary        mkinstalldirs     tests
config.m4       install-sh      php_redis.h
[root@server2 phpredis-master]# ./configure 
[root@server2 phpredis-master]# make && make install 
[root@server2 ~]# cd /etc/php.d/
[root@server2 php.d]# ls
curl.ini      json.ini      mysql.ini      pdo_sqlite.ini  zip.ini
fileinfo.ini  mbstring.ini  pdo.ini        phar.ini
gd.ini        mysqli.ini    pdo_mysql.ini  sqlite3.ini
[root@server2 php.d]# cp mysql.ini redis.ini
[root@server2 php.d]# vim redis.ini 
  2 extension=redis.so
  [root@server2 php.d]# /etc/init.d/php-fpm reload
Reloading php-fpm: [14-Jul-2018 01:21:56] NOTICE: configuration file /etc/php-fpm.conf test is successful

                                                           [  OK  ]
[root@server2 php.d]# php -m |grep redis
redis

server3:mysql配置

1、安装mysql-server

[root@server3 ~]# rpm -qa | grep mysql
mysql-community-common-5.7.17-1.el6.x86_64
mysql-community-client-5.7.17-1.el6.x86_64
mysql-community-libs-compat-5.7.17-1.el6.x86_64
mha4mysql-node-0.56-0.el6.noarch
mysql-community-libs-5.7.17-1.el6.x86_64
mysql-community-server-5.7.17-1.el6.x86_64
[root@server3 ~]# rpm -e `rpm -qa|grep mysql` --nodeps   ##不考虑依赖性删除mysql
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave
[root@server3 ~]# rpm -qa | grep mysql
[root@server3 ~]# cd /var/lib/mysql/
[root@server3 mysql]# rm -fr *
[root@server3 mysql]# ls
[root@server3 mysql]# yum install -y mysql-server  ##安装

2、开启mysql,并导入测试数据库

[root@server3 ~]# /etc/init.d/mysqld start
[root@server3 ~]# mysql < test.sql
[root@server3 ~]# mysql < test.sql 
[root@server3 ~]# cat test.sql 
use test;
CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9');

#DELIMITER $$
#CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN
#    SET @RECV=gman_do_background('syncToRedis', json_object(NEW.id as `id`, NEW.name as `name`)); 
#  END$$
#DELIMITER ;

3、数据库授权

[root@server3 ~]# mysql
mysql> grant all on test.* to redis@'%' identified by 'westos';
Query OK, 0 rows affected (0.00 sec)
mysql>  select * from test.test;
+----+-------+
| id | name  |
+----+-------+
|  1 | test1 |
|  2 | test2 |
|  3 | test3 |
|  4 | test4 |
|  5 | test5 |
|  6 | test6 |
|  7 | test7 |
|  8 | test8 |
|  9 | test9 |
+----+-------+
9 rows in set (0.00 sec)

测试:访问172.25.254.2/test.php
1、php默认从redis 索取数据,第一次redis无缓存,则php从mysql’索取数据
第一次无缓存
redis+mysql实现lnmp架构缓存_第4张图片
第二次索取数据后:
redis+mysql实现lnmp架构缓存_第5张图片
redis节点也可查看

[root@server1 redis-4.0.1]# redis-cli
127.0.0.1:6379> get 2
"test2"

2、将数据库server3节点内容更新并删除节点,则php从数据库索取数据节点更新内容

mysql> update test.test set name='westos' where id=1;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql>  select * from test.test;
+----+--------+
| id | name   |
+----+--------+
|  1 | westos |
|  2 | test2  |
|  3 | test3  |
|  4 | test4  |
|  5 | test5  |
|  6 | test6  |
|  7 | test7  |
|  8 | test8  |
|  9 | test9  |
+----+--------+
9 rows in set (0.00 sec)

redis的master主机删除节点内容

[root@server1 redis-4.0.1]# redis-cli
127.0.0.1:6379> get 2
"test2"
127.0.0.1:6379> del 1
(integer) 1
127.0.0.1:6379> get 1
(nil)

刷新页面,再次访问
redis+mysql实现lnmp架构缓存_第6张图片
以上redis 作为 mysql 的缓存服务器,但是如果更新了 mysql,redis中仍然会有对应的 KEY,数据就不会更新,此时就会出现 mysql 和 redis 数据不一致的情况。

二、mysql和redis触发数据同步

mysq 触发器+gearman+php.worker

1、php装载gearman插件

[root@server2 ~]# yum install -y libgearman-1.1.8-2.el6.x86_64.rpm  gearmand-1.1.8-2.el6.x86_64.rpm
[root@server2 ~]# /etc/init.d/gearmand start 

这里写图片描述

[root@server2 ~]# tar zxf gearman-1.1.2.tgz 
[root@server2 ~]# cd gearman-1.1.2
[root@server2 gearman-1.1.2]# phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[root@server2 gearman-1.1.2]# ./configure 
[root@server2 gearman-1.1.2]# make && make install 

安装过程可能需要的依赖性包
redis+mysql实现lnmp架构缓存_第7张图片
安装完成后配置文件:

[root@server2 gearman-1.1.2]# cd /etc/php.d/
[root@server2 php.d]# cp redis.ini gearman.ini
[root@server2 php.d]# vim gearman.ini 
extension=gearman.so
[root@server2 php.d]# /etc/init.d/php-fpm reload

查看php加载的模块
redis+mysql实现lnmp架构缓存_第8张图片

2、php.worker配置

[root@server2 ~]# vim worker.php
  1 addServer();
  4 $worker->addFunction('syncToRedis', 'syncToRedis');
  5 
  6 $redis = new Redis();
  7 $redis->connect('172.25.254.1', 6379);   ##redis的ip
  8 
  9 while($worker->work());
 10 function syncToRedis($job)
 11 {
 12         global $redis;
 13         $workString = $job->workload();
 14         $work = json_decode($workString);
 15         if(!isset($work->id)){
 16                 return false;
 17         }
 18         $redis->set($work->id, $work->name);
 19 }
 20 ?>
[root@server2 ~]# cp worker.php  /usr/local/
[root@server2 ~]# nohup php /usr/local/worker.php &
[1] 3928
[root@server2 ~]# nohup: ignoring input and appending output to `nohup.out'

[root@server2 ~]# 

3、mysql端配置

1、mysql端需要的安装包
这里写图片描述
2、生成lib_mysqludf_json.so模块

[root@server3 redis]# yum install -y mysql-devel
[root@server3 redis]# yum install -y gcc
[root@server3 redis]# unzip lib_mysqludf_json-master.zip 
Archive:  lib_mysqludf_json-master.zip
37f851c808c4161beb4d5e535771dc0c59c82de6
   creating: lib_mysqludf_json-master/
  inflating: lib_mysqludf_json-master/README.md  
  inflating: lib_mysqludf_json-master/lib_mysqludf_json.c  
  inflating: lib_mysqludf_json-master/lib_mysqludf_json.html  
  inflating: lib_mysqludf_json-master/lib_mysqludf_json.so  
  inflating: lib_mysqludf_json-master/lib_mysqludf_json.sql  
[root@server3 redis]# cd lib_mysqludf_json-master
[root@server3 lib_mysqludf_json-master]# gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
[root@server3 lib_mysqludf_json-master]# cp lib_mysqludf_json.s
lib_mysqludf_json.so   lib_mysqludf_json.sql  
[root@server3 lib_mysqludf_json-master]# cp lib_mysqludf_json.s
lib_mysqludf_json.so   lib_mysqludf_json.sql  
[root@server3 lib_mysqludf_json-master]# cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/

3、注册json函数

[root@server3 ~]# mysql
mysql> show global variables like 'plugin_dir';
+---------------+-------------------------+
| Variable_name | Value                   |
+---------------+-------------------------+
| plugin_dir    | /usr/lib64/mysql/plugin |
+---------------+-------------------------+
1 row in set (0.00 sec)

mysql> create function json_object returns string soname 'lib_mysqludf_json.so'
    -> ;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from mysql.func;
+-------------+-----+----------------------+----------+
| name        | ret | dl                   | type     |
+-------------+-----+----------------------+----------+
| json_object |   0 | lib_mysqludf_json.so | function |
+-------------+-----+----------------------+----------+
1 row in set (0.00 sec)

redis+mysql实现lnmp架构缓存_第9张图片
4、注册udf函数

[root@server3 redis]# tar zxf gearman-mysql-udf-0.6.tar.gz 
[root@server3 redis]# yum install lib*
[root@server3 gearman-mysql-udf-0.6]# ./configure --libdir=/usr/lib64/mysql/plugin/
[root@server3 gearman-mysql-udf-0.6]# make && make install
[root@server3 gearman-mysql-udf-0.6]# cd /usr/lib64/mysql/plugin/
[root@server3 plugin]# ll libgearman_mysql_udf.so
lrwxrwxrwx 1 root root 29 Jul 15 01:17 libgearman_mysql_udf.so -> libgearman_mysql_udf.so.0.0.0
[root@server3 ~]# mysql
mysql> create function gman_do_background returns string soname 'libgearman_mysql_udf.so';
mysql> create function gman_servers_set returns string soname 'libgearman_mysql_udf.so';
mysql> select * from mysql.func;

redis+mysql实现lnmp架构缓存_第10张图片
5、指定gearman 的服务信息

mysql> select gman_servers_set('172.25.254.2:4730');
+---------------------------------------+
| gman_servers_set('172.25.254.2:4730') |
+---------------------------------------+
| 172.25.254.2:4730                     |
+---------------------------------------+
1 row in set (0.00 sec)

6、编写mysql触发器

[root@server3 ~]# vim test.sql 
  1 use test;
  2 #CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY     (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  3 #INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(    7,'test7'),(8,'test8'),(9,'test9');
  4 
  5 DELIMITER $$
  6 CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN
  7     SET @RECV=gman_do_background('syncToRedis', json_object(NEW.id as `id`, NEW.name as `name`));
  8   END$$
  9 DELIMITER ;
  [root@server3 ~]# mysql < test.sql 

7、查看触发器

[root@server3 ~]# mysql
mysql> show triggers from test;

redis+mysql实现lnmp架构缓存_第11张图片
测试:
数据库写入:

mysql> update test set name='redhat' where id=1;

redis+mysql实现lnmp架构缓存_第12张图片

mysql> update test set name='test1' where id=1;

redis+mysql实现lnmp架构缓存_第13张图片

你可能感兴趣的:(企业级运维方案)