赞
踩
目录
2.1.3 vim修改/etc/php.ini,添加redis的扩展
2.1.5 在httpd的发布目录下编辑如下php页面,然后访问
环境规划
ip地址 | 环境 |
192.168.150.19 | redis |
192.168.150.20 | web |
192.168.150.21 | mysql |
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
同步时间
yum -y install ntp ntpdate
ntpdate cn.pool.ntp.org
hwclock --systohc
yum install php php-fpm php-cli php-common php-gd php-mbstring php-mysql php-pdo php-devel php -xmlrpc php-xml php-bcmath php-dba php-enchant -y
wget http://pecl.php.net/get/redis-2.2.7.tgz
tar -zxvf redis-2.2.7.tgz
cd redis-2.2.7/phpize./configuremake install
systemctl restart httpd
- [root@192 html]# cat index.php
- <?php
- phpinfo();
- ?>
注意:找到redis才算ok
wget -c -t 0 http://download.redis.io/releases/redis-2.8.19.tar.gz
tar -xvf redis-2.8.19.tar.gz
make
修改redis.conf将daemonize no改为yes
启动redis
src/redis-server redis.conf
vim redis.php
- <?php
- $redis = new Redis();
- $redis->connect('192.168.150.19',6379) or die ("could net connect redis server"); ## 这里面ip 是redis服务器ip
- # $query = "select * from testab limit 5";
- $query = "select * from testab"; ##这里改成自己mysql存在的表
- for ($key = 1; $key < 10; $key++)
- {
- if (!$redis->get($key))
- {
- $connect = mysql_connect('192.168.150.21','redis','Zcf@201104'); ##mysql的ip
- mysql_select_db(testab);
- $result = mysql_query($query);
- $arr = [];
- while ($row = mysql_fetch_assoc($result))
- {
- $redis->setex($row['id'],10,$row['name']);
- $arr[] = $row;
- }
- $myserver = 'mysql';
- $data = $arr;
- break;
- }
- else
- {
- $myserver = "redis";
- $data[$key] = $redis->get($key);
- }
- }
- echo $myserver;
- echo "<br>";
- for ($key = 1; $key < 10; $key++)
- {
- echo "number is <b><font color=#FF0000>$key</font></b>";
- echo "<br>";
- if ($myserver == "mysql") {
- $arr2 = array_map('end',$data);
- echo "name is <b><font color=#FF0000>$arr2[$key]</font></b>";
- echo "<br>";
- }
- else {
- echo "name is <b><font color=#FF0000>$data[$key]</font></b>";
- echo "<br>";
- }
- }
在mysql服务器创建用户redis
create user 'redis'@'%' indentified by 'Zcf@201104'
alter user 'redis'@'%' indentified with mysql_native_password by 'Zcf@201104'
flush privileges
vim mysql.php
- <?php
- $link=mysql_connect('192.168.150.21','redis','Zcf@201104');
- if($link) echo "Success!!";
- else echo "Fail!!";
- mysql_close();
- ?>
-
- mysql> select * from testab;
- +------+------+
- | id | name |
- +------+------+
- | 1 | tom |
- | 2 | tom1 |
- | 3 | tom2 |
- | 4 | tom3 |
- | 5 | tom4 |
- | 6 | tom5 |
- | 7 | tom6 |
- | 8 | tom7 |
- | 9 | tom8 |
- | 10 | tom9 |
- +------+------+
- 10 rows in set (0.00 sec)
-
第一次访问,redis中没有对应的KEY值
刷新,再次访问,此时redis中已有相关数据
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。