redis服務(wù)器環(huán)境下mysql實(shí)現(xiàn)lnmp架構(gòu)緩存
配置環(huán)境: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相應(yīng)模塊

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

php測(cè)試:
[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):

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);
//如果沒(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模塊
[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、開(kāi)啟mysql,并導(dǎo)入測(cè)試數(shù)據(jù)庫(kù)
[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)
[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ú)緩存

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

redis節(jié)點(diǎn)也可查看
[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)容
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)容
[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 作為 mysql 的緩存服務(wù)器,但是如果更新了 mysql,redis中仍然會(huì)有對(duì)應(yīng)的 KEY,數(shù)據(jù)就不會(huì)更新,此時(shí)就會(huì)出現(xiàn) mysql 和 redis 數(shù)據(jù)不一致的情況。
總結(jié)
以上所述是小編給大家介紹的redis服務(wù)器環(huán)境下mysql實(shí)現(xiàn)lnmp架構(gòu)緩存,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- MySQL的查詢緩存和Buffer Pool
- mysqldump造成Buffer Pool污染的研究
- MySQL和Redis實(shí)現(xiàn)二級(jí)緩存的方法詳解
- mysql select緩存機(jī)制使用詳解
- MySQL緩存的查詢和清除命令使用詳解
- PHP使用redis實(shí)現(xiàn)統(tǒng)計(jì)緩存mysql壓力的方法
- MySQL高速緩存啟動(dòng)方法及參數(shù)詳解(query_cache_size)
- 清空mysql 查詢緩存的可行方法
- 使用Memcache緩存mysql數(shù)據(jù)庫(kù)操作的原理和緩存過(guò)程淺析
- 詳解MySQL中的緩沖池(buffer pool)
相關(guān)文章
mysql數(shù)據(jù)庫(kù)SQL子查詢(史上最詳細(xì))
這篇文章主要給大家介紹了關(guān)于mysql數(shù)據(jù)庫(kù)SQL子查詢的相關(guān)資料,子查詢指的是嵌套在某個(gè)語(yǔ)句中的SELECT語(yǔ)句, MySQL支持標(biāo)準(zhǔn)SQL所要求的所有子查詢形式和操作,此外還進(jìn)行了一些擴(kuò)展,需要的朋友可以參考下2024-05-05
MySQL多個(gè)字段拼接去重的實(shí)現(xiàn)示例
在MySQL中,我們經(jīng)常會(huì)遇到需要將多個(gè)字段進(jìn)行拼接并去重的情況,本文就來(lái)介紹一下MySQL多個(gè)字段拼接去重的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
淺談mysql數(shù)據(jù)庫(kù)中的換行符與textarea中的換行符
下面小編就為大家?guī)?lái)一篇淺談mysql數(shù)據(jù)庫(kù)中的換行符與textarea中的換行符。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
MySQL?使用觸發(fā)器記錄用戶的操作日志問(wèn)題
使用?MySQL?觸發(fā)器可以記錄哪些用戶、什么時(shí)間對(duì)數(shù)據(jù)表進(jìn)行了增、刪、改操作。如果執(zhí)行刪除操作,則記錄刪除之前的數(shù)據(jù)記錄;如果執(zhí)行更新操作,記錄更新之前的數(shù)據(jù)記錄,這篇文章主要介紹了MySQL?使用觸發(fā)器記錄用戶的操作日志,需要的朋友可以參考下2022-12-12
MySQL合并查詢結(jié)果的實(shí)現(xiàn)
本文主要介紹了MySQL合并查詢結(jié)果的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
UCenter info: MySQL Query Error SQL:SELECT value FROM [Table
大家先看下數(shù)據(jù)庫(kù)權(quán)限問(wèn)題,然后再進(jìn)行如下操作。2011-09-09

