溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

redis環(huán)境下mysql是如何實(shí)現(xiàn)lnmp架構(gòu)緩存的

發(fā)布時(shí)間:2020-05-08 11:23:37 來(lái)源:億速云 閱讀:297 作者:三月 欄目:MySQL數(shù)據(jù)庫(kù)

本文主要給大家介紹redis環(huán)境下mysql是如何實(shí)現(xiàn)lnmp架構(gòu)緩存的,文章內(nèi)容都是筆者用心摘選和編輯的,具有一定的針對(duì)性,對(duì)大家的參考意義還是比較大的,下面跟筆者一起了解下redis環(huán)境下mysql是如何實(shí)現(xiàn)lnmp架構(gòu)緩存的吧。

配置環(huán)境:centos 7.2

server1:redis(172.25.254.1)

server2:php(172.25.254.2)

server3:mysql(172.25.254.3)

配置步驟:

server2:

1、server2安裝php的redis相應(yīng)模塊

redis環(huán)境下mysql是如何實(shí)現(xiàn)lnmp架構(gòu)緩存的

2、nginx安裝

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[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:
* https://nginx.org/en/docs/
commercial subscriptions for nginx are available on:
* https://nginx.com/products/
  
----------------------------------------------------------------------
[root@server2 php-fpm.d]# id nginx
uid=498(nginx) gid=499(nginx) groups=499(nginx)

3、nginx和php配置

1、php配置

?

1
2
3
4
5
6
7
8
9
10
11
12
13
[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配置

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[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環(huán)境下mysql是如何實(shí)現(xiàn)lnmp架構(gòu)緩存的

php測(cè)試:

?

1
2
3
4
5
6
7
8
9
[root@server2 conf.d]# cd /usr/share/nginx/html/
[root@server2 html]# vim index.php
[root@server2 html]# cat index.php
<!--php
phpinfo()
-->
[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 ]


物理機(jī)訪問(wèn):

redis環(huán)境下mysql是如何實(shí)現(xiàn)lnmp架構(gòu)緩存的

4、php配置redis+mysql

?

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

5、添加php支持的redis模塊

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[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

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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、開(kāi)啟mysql,并導(dǎo)入測(cè)試數(shù)據(jù)庫(kù)

?

1
2
3
4
5
6
7
8
9
10
11
12
[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、數(shù)據(jù)庫(kù)授權(quán)

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[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)

測(cè)試:訪問(wèn)172.25.254.2/test.php

1、php默認(rèn)從redis 索取數(shù)據(jù),第一次redis無(wú)緩存,則php從mysql'索取數(shù)據(jù)

第一次無(wú)緩存

redis環(huán)境下mysql是如何實(shí)現(xiàn)lnmp架構(gòu)緩存的

第二次索取數(shù)據(jù)后:

redis環(huán)境下mysql是如何實(shí)現(xiàn)lnmp架構(gòu)緩存的

redis節(jié)點(diǎn)也可查看

?

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

2、將數(shù)據(jù)庫(kù)server3節(jié)點(diǎn)內(nèi)容更新并刪除節(jié)點(diǎn),則php從數(shù)據(jù)庫(kù)索取數(shù)據(jù)節(jié)點(diǎn)更新內(nèi)容

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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主機(jī)刪除節(jié)點(diǎn)內(nèi)容

?

1
2
3
4
5
6
7
[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)

刷新頁(yè)面,再次訪問(wèn)

redis環(huán)境下mysql是如何實(shí)現(xiàn)lnmp架構(gòu)緩存的

看完以上關(guān)于redis環(huán)境下mysql是如何實(shí)現(xiàn)lnmp架構(gòu)緩存的,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業(yè)知識(shí)信息 ,可以持續(xù)關(guān)注我們的行業(yè)資訊欄目的。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI