在學習完Redis後,相信很多人都對明白了mysql+redis的開發模式有了一些心得認識。那麼,在這裏給大家先分析一下Mysql+Redis的開發優勢。
做過項目開發的人應該都知道,讀寫一體的操作對mysql的壓力是很大的,這張圖片中完美的解釋了mysql+redis的開發是怎樣的一種模式。
現在介紹一下相關環境的搭建:
一、ubuntu16.04下的ngnix+php+mysql環境搭建:
安裝LNMP環境
1.apt-get安裝
apt-get install nginx mysql-server php
(注意哦,新版本的mysql安裝時必須要設密碼的,要是你沒有設置密碼,那恭喜你走上了我的不歸路了,做好時刻準備重新安裝的準備。因爲新版的額mysql的root用戶必須設置密碼,如果在安裝過程中,你也像小編一樣直接一直enter過去,mysql是會給你默認生成一堆密碼的。然後你就登錄不了。!!!)
2.配置nginx,支持php
vi /etc/nginx/sites-available/default ...... # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 #修改後如下: location ~ \.php$ { try_files $uri /index.php =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } ......
3.重啓nginx,測試
vi /var/www/html/info.php <?php phpinfo();?>
然後訪問頁面看到php的相關信息,基礎環境就算搭建完成了。(哦,對了。這裏測試的時候注意點,你要在配置文件中讓nginx來解析php,否則你訪問只能把php文件下載下來,而沒辦法讓其去解析。我在配置文件中已經修改過了哦。)
1.安裝redis和php的redis擴展
apt-get install redis-server apt-get install git php-dev git clone https://github.com/nicolasff/phpredis.git //這裏注意一下那個有php7的是沒辦法完成的,應該是github上的變化,現在直接克隆這個就可以了。 cd phpredis/ phpize ./configure make make install
2.配置php的redis擴展
sudo vim /etc/php/7.0/fpm/conf.d/redis.ini //這個是寫一個配置文件
extension=redis.so //配置文件的內容(沒錯,就這一句)
3.重啓fpm,訪問info.php,就能看到 擴展
/etc/init.d/php7.0-fpm restart
五、讀取測試
<?php //連接本地Redis服務 $redis=new Redis(); $redis->connect('localhost','6379') or die ("Could net connect redis server!"); //$redis->auth('*****'); //登錄驗證密碼,返回【true | false】 $redis->ping(); //檢查是否還再鏈接,[+pong] $redis->select(0);//選擇redis庫,0~15 共16個庫 //設置數據 $redis->set('school','Xi`An University of Posts & Communications'); //設置多個數據 $redis->mset(array('name'=>'caoyouming0609','age'=>22,'height'=>'1.70')); //存儲數據到列表中 $redis->lpush("tutorial-list", "Redis"); $redis->lpush("tutorial-list", "Mongodb"); $redis->lpush("tutorial-list", "Mysql"); //獲取存儲數據並輸出 echo $redis->get('school'); echo '<br/>'; $gets=$redis->mget(array('name','age','height')); print_r($gets); echo '<br/>'; $tl=$redis->lrange("tutorial-list", 0 ,5); print_r($tl); echo '<br/>'; //釋放資源 $redis->close(); ?>
OK,到這裏mysql+redis的配置就算完成了。