redis3.2 php,mac环境下安装Redis及thinkphp3.2/thinkphp5中redis的使用

参考文章:http://www.jianshu.com/p/6b5eca8d908b安装

下载安装包 redis-*.*.*.tar.gz

(1.) 服务器上安装了redis

解压:tar -zvxf redis-*.*.*.tar.gz  将解压后的文件夹放到 /usr/local目录下

编译测试:

接下来在终端中切换到/usr/local/redis-3.2.8目录下,输入:sudo make test

详情如下....

$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz

$ tar xzf redis-3.2.8.tar.gz

$ cd redis-3.2.8

$ make

The binaries that are now compiled are available in the            src

directory. Run Redis with:$ src/redis-server

这句是 启动服务,启动后,会出现一个 图形。

这里不要关闭。11480:C 16 May 11:18:03.282 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf

11480:M 16 May 11:18:03.284 * Increased maximum number of open files to 10032 (it was originally set to 256).  _._

_.-``__ ''-._

_.-``    `.  `_.  ''-._           Redis 3.2.8 (2b9a2833/1) 64 bit

.-`` .-```.  ```\/    _.,_ ''-._

(    '      ,       .-`  | `,    )     Running in standalone mode

|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379

|    `-._   `._    /     _.-'    |     PID: 11480

`-._    `-._  `-./  _.-'    _.-'

|`-._`-._    `-.__.-'    _.-'_.-'|

|    `-._`-._        _.-'_.-'    |           http://redis.io

`-._    `-._`-.__.-'_.-'    _.-'

|`-._`-._    `-.__.-'    _.-'_.-'|

|    `-._`-._        _.-'_.-'    |

`-._    `-._`-.__.-'_.-'    _.-'

`-._    `-.__.-'    _.-'

`-._        _.-'

`-.__.-'

11480:M 16 May 11:18:03.290 # Server started, Redis version 3.2.8

11480:M 16 May 11:18:03.291 * DB loaded from disk: 0.001 seconds

11480:M 16 May 11:18:03.291 * The server is now ready to accept connections on port 6379

然后,另起一个终端。

You can interact with Redis using the built-in client:$ src/redis-cli

redis> set foo bar

OK

redis> get foo

"bar"

上面验证,服务器上 已经开启redis服务了,可以使用。

(2.) php 里 安装  redis 扩展。

有没有安装redis扩展:可先在自己的demo中查看phpinfo.php<?php

echo phpinfo();

二,实例

下面举例在thinkphp3.2.3,thinkphp5项目中,如何使用redis,

1.看网上教程,关于thinkphp3.2.3的,或者略过。

下载redis.rar,查看说明。

他里面写的:

这个是对windows的,我没有测试过,也是为了保证php的redis扩展安装好。

(1).cd /usr/local/php5

(2).sudo cp -r ~/Downloads/redis/redis扩展文件/ ./

(3)找到 /private/etc/php.ini

Php.ini加下面两行:

extension=php_igbinary.dll

extension=php_redis.dll

注:php_redis.dll需放在php_igbinary.dll之后

2.如果按照各种方法,把php的redis扩展安装好了。

写一个phpinfo.php。查看一下。

里面有redis.就OK

3.在thinkphp3.2项目的config.conf,添加'DATA_CACHE_PREFIX' => 'Redis_',//缓存前缀'DATA_CACHE_TYPE'=>'Redis',//默认动态缓存为

Redis'REDIS_RW_SEPARATE' => true, //Redis读写分离 true 开启

'REDIS_HOST'=>'127.0.0.1', //redis服务器ip,多台用逗号隔开;

读写分离开启时,第一台负责写,其它[随机]负责读;

'REDIS_PORT'=>'6379',//端口号'REDIS_TIMEOUT'=>'300',

//超时时间'REDIS_PERSISTENT'=>false,//是否长连接 false=短连接

'REDIS_AUTH'=>'',//AUTH认证密码

4.thinkphp3.2的项目中,使用时,这样写/**

* 测试redis

* author:

* http://wp.ccc/index.php/Home/Hq/redis_demo

*/

public function redis_demo(){

$redis = new \Redis();

$redis->connect('127.0.0.1',6379);

$redis->set('test','hello world');

echo $redis->get("test");

}

5.如果是thinkphp5项目。

config.php 这样写'cache' =>  [

// 使用复合缓存类型

'type'  =>  'complex',

// 默认使用的缓存

'default'   =>  [

// 驱动方式

'type'   =>

'File',

// 缓存保存目录

'path'   => CACHE_PATH,

// 缓存有效期 0表示永久缓存

'expire' => 0,    ],

// 文件缓存

'file'   =>  [

// 驱动方式

'type'   => 'file',

// 设置不同的缓存保存目录

'path'   => RUNTIME_PATH . 'file/',    ],

//redis缓存

'redis'   =>  [

// 驱动方式

'type'   =>

'redis',

// 服务器地址

'host'       => '127.0.0.1',

],],

6.thinkphp5中,使用时,这样写:use think\Cache;// 更新属性数据

public function updatecache(){

// 查找

$list = Db('Cascadedata')->select();

// var_dump($list);

Cache::store('redis')->set('cascadedata',$list);

$cascadedata = Cache::get('cascadedata');

var_dump($cascadedata);

}

三。可能出现的问题,

1.扩展没有安装好

2.

==

关于php重启的:

php-fpm重启

killall php-fpm

再执行(usr/local/php是php的安装目录)

/usr/local/php/sbin/php-fpm &

3.

==

如果出现:

:(

Redis server went away

是不是防火墙被关闭了。

查看,系统偏好设置。

2eac49d1d6920be1f60aed23c7a92b7a.png

===

再总结,使用的时候打开redis服务macdeMacBook-Pro:redis-3.2.8 mac$ src/redis-server

再开一个终端:

我以为设置了 auth,所以输了auth ****, 这一行可以略去。macdeMacBook-Pro:~ mac$ cd /usr/local/redis-3.2.8

macdeMacBook-Pro:redis-3.2.8 mac$ src/redis-cli

127.0.0.1:6379> auth *****

(error) ERR Client sent AUTH, but no password is set

127.0.0.1:6379>

测试一下。set foo bar

get foo

OK。

===

正式站线上,服务器oneinstack安装的环境:

linux下,查看服务器上redis有没有启动。

service redis-server status

redis-server (pid 2918) is running...

看端口有没有打开。

netstat an|grep 6379

0 127.0.0.1:6379              0.0.0.0:*                   LISTEN

准备好之后,

在项目里某个控制器里,写一个方法,测试一下。

/**

* 测试redis

* author:

* http://wp.ccc/index.php/Home/Hq/redis_demo

*/

public function redis_demo(){

$redis = new \Redis();

$redis->connect('127.0.0.1',6379);

$redis->set('test','hello world');

echo $redis->get("test");

}

ok!

关于这个知识点,如果你不会安装,或测试redis还没做出来,

欢迎到php技术问答群QQ群:292626152来提问。

==

另外打开端口的知识点:

一、查看哪些端口被打开  netstat -anp

二、关闭端口号:iptables -A INPUT -p tcp --drop 端口号-j DROP

iptables -A OUTPUT -p tcp --dport 端口号-j DROP

三、打开端口号:iptables -A INPUT -ptcp --dport  端口号-j ACCEPT

四、以下是linux打开端口命令的使用方法。

nc -lp 23 &(打开23端口,即telnet)

netstat -an | grep 23 (查看是否打开23端口)

五、linux打开端口命令每一个打开的端口,都需要有相应的监听程序才可以

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值