久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

redis服務器環境下mysql如何實現lnmp架構緩存

167次閱讀
沒有評論

共計 6823 個字符,預計需要花費 18 分鐘才能閱讀完成。

自動寫代碼機器人,免費開通

這篇文章主要為大家展示了“redis 服務器環境下 mysql 如何實現 lnmp 架構緩存”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓丸趣 TV 小編帶領大家一起研究并學習一下“redis 服務器環境下 mysql 如何實現 lnmp 架構緩存”這篇文章吧。

配置環境:redhat6.5

server1:redis(172.25.254.1)

server2:php(172.25.254.2)

server3:mysql(172.25.254.3)

配置步驟:

server2:

1、server2 安裝 php 的 redis 相應模塊

redis 服務器環境下 mysql 如何實現 lnmp 架構緩存

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:
* 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 配置

[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 架構緩存

php 測試:

[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 ]

物理機訪問:

redis 服務器環境下 mysql 如何實現 lnmp 架構緩存

4、php 配置 redis+mysql

[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);
 // 如果沒有找到 $key, 就將該查詢 sql 的結果緩存到 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 模塊

[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 架構緩存

第二次索取數據后:

redis 服務器環境下 mysql 如何實現 lnmp 架構緩存

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 架構緩存

以上是“redis 服務器環境下 mysql 如何實現 lnmp 架構緩存”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道!

向 AI 問一下細節

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-12-04發表,共計6823字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 鄂伦春自治旗| 龙泉市| 恩施市| 太湖县| 白山市| 西藏| 六安市| 龙井市| 佛冈县| 新乡县| 苍南县| 黔西| 丽江市| 潢川县| 交城县| 海南省| 长子县| 荆州市| 清苑县| 宁德市| 申扎县| 翼城县| 永州市| 刚察县| 清苑县| 海城市| 吉安市| 罗源县| 青州市| 霍邱县| 金华市| 石狮市| 浙江省| 旌德县| 湾仔区| 吴旗县| 广安市| 昌乐县| 望都县| 衡东县| 武义县|