swoole redis 首先先安装redis 服务端
参考连接https://blog.csdn.net/hbh112233abc/article/details/54966407
环境介绍:我的服务器是阿里云的centos主机,php 7.0.14
1 安装和配置redis
参考方案 点击打开链接
1.1 下载redis
进入软件安装目录
- cd /usr/local/
- wget http://download.redis.io/redis-stable.tar.gz
1.2 编译安装
- tar xvzf redis-stable.tar.gz
- cd redis-stable
- make
- make install
可能遇到的问题
- 如果make出现了异常:
- 1)make[2]: cc: Command not found
- 异常原因:没有安装gcc
- 解决方案:yum install gcc-c++
- 2)zmalloc.h:51:31: error: jemalloc/jemalloc.h: No such file or directory
- 异常原因:一些编译依赖或原来编译遗留出现的问题
- 解决方案:make distclean。清理一下,然后再make。
- 在make成功之后,需要make test 在make test 出现异常
- 1) couldn't execute "tclsh8.5": no such file or directory
- 异常原因:没有安装tcl
- 解决方案:yum install -y tcl。
redis 功能说明
- make命令执行完成后,会在src目录下生成5个可执行文件,分别是redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump,它们的作用如下:
- redis-server:Redis服务器的daemon启动程序
- redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作
- redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
- redis-check-aof:更新日志检查
- redis-check-dump:用于本地数据库检查
- cp redis-server /usr/local/bin/
- cp redis-cli /usr/local/bin/
- 然后新建目录,存放配置文件
- mkdir /etc/redis
- mkdir /var/redis
- mkdir /var/redis/log
- mkdir /var/redis/run
- mkdir /var/redis/6379
- 在redis解压根目录中找到配置文件模板,复制到如下位置。
- cp redis.conf /etc/redis/6379.conf通过vim命令修改
- daemonize yes
- pidfile /var/redis/run/redis_6379.pid
- logfile /var/redis/log/redis_6379.log
- dir /var/redis/6379
- 最后运行redis:
- redis-server /etc/redis/6379.conf
- netstat –atln
有了127.0.0.1:6379这个说明redis已经成功运行了.
1.4 redis操作尝试
- redis 127.0.0.1:6379> set foo bar
- OK
- redis 127.0.0.1:6379> keys *
- 1) "foo"
- redis 127.0.0.1:6379> get foo
- "bar"
- redis 127.0.0.1:6379> del foo
2 安装php_redis扩展
扩展下载地址: https://github.com/phpredis/phpredis/tree/php7
下载压缩包
- wget https://github.com/phpredis/phpredis/archive/php7.zip
- unzip php7.zip
编译安装
- cd phpredis-php7
- /usr/bin/phpize
- ./configure --with-php-config=/usr/bin/php-config
- make
- make install
备注说明: 请注意 phpize和php-config根据个人安装的php目录地址不一样,可以用find命令查看他们所在的地址
- find / -name phpize
- find / -name php-config
配置
在php扩展配置目录里添加 redis.ini
通过phpinfo可以查看php的扩展配置目录
redis.ini中写入
- ;Enable redis extension module
- extension=redis.so
重启php-fpm
- service php-fpm restart
再次查看phpinfo就可以看到redis扩展已经有了
=============================分割线===============================
我编译的代码如下
2771 2018-05-03 10:16:51 wget http://download.redis.io/redis-stable.tar.gz
2772 2018-05-03 10:16:56 ls
2773 2018-05-03 10:17:03 which redis
2774 2018-05-03 10:17:09 whereis redis
2775 2018-05-03 10:17:19 tar -zxvf redis-stable.tar.gz
2776 2018-05-03 10:17:21 cd redis-stable/
2777 2018-05-03 10:17:22 ls
2778 2018-05-03 10:17:32 ls
2779 2018-05-03 10:17:34 make
2780 2018-05-03 10:19:01 make install
2781 2018-05-03 10:19:14 ls
2782 2018-05-03 10:19:17 cd src/
2783 2018-05-03 10:19:17 ls
2784 2018-05-03 10:19:26 ./redis-server
运行结果如下
这个时候swoole 需要安装hredis
参考连接 https://wiki.swoole.com/wiki/page/p-redis.html
Swoole-1.8.0
版本增加了对异步Redis客户端的支持,基于redis官方提供的hiredis库实现。Swoole提供了__call
魔术方法,来映射绝大部分Redis指令。
编译安装hiredis
使用Redis客户端,需要安装hiredis库。下载hiredis
源码后,执行
make -j
sudo make install
sudo ldconfig
- hiredis下载地址:https://github.com/redis/hiredis/releases
启用异步Redis客户端
编译swoole时,在configure
指令中加入--enable-async-redis
./configure --enable-async-redis
make clean
make -j
sudo make install
可能遇到的问题
php-m
发现swoole消失或者是通过php --ri swoole
没有显示async redis client
vi ~/.bash_profile
在最后一行添加 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
source ~/.bash_profile
重新编译安装swoole即可
这里我遇到的问题就是 这些都执行完后 php --ri swoole
没有显示async redis client
于是执行
然后重新编译swoole
=============================分割线===============================
我的编译如下
2732 2018-05-03 09:28:33 wget https://github.com/redis/hiredis/archive/v0.13.3.tar.gz
2733 2018-05-03 09:28:38 ls
2734 2018-05-03 09:28:48 tar -zxvf v0.13.3.tar.gz
2735 2018-05-03 09:28:51 ls
2736 2018-05-03 09:28:54 cd hiredis-0.13.3/
2737 2018-05-03 09:28:55 ls
2738 2018-05-03 09:29:11 make -j
2739 2018-05-03 09:29:18 make install
2740 2018-05-03 09:29:28 sudo ldconfig
中间20来行是没啥用的
2763 2018-05-03 09:36:08 ./configure --enable-async-redis
2764 2018-05-03 09:36:12 make clean
2765 2018-05-03 09:36:15 make -j
2766 2018-05-03 09:37:21 make install
2767 2018-05-03 09:37:37 php -m
2768 2018-05-03 09:37:44 php -m | grep swoo
2769 2018-05-03 09:37:53 serivce php-fpm restart
2770 2018-05-03 09:38:00 serivce php-fpm restart
2771 2018-05-03 09:38:05 service php-fpm restart
2772 2018-05-03 09:38:08 php -m | grep swoo
2773 2018-05-03 09:38:19 php --ri swoole
2774 2018-05-03 09:38:53 php --ri swoole | grep redis
2775 2018-05-03 09:39:03 vi ~/.bash_profile
2776 2018-05-03 09:39:23 source ~/.bash_profile
2777 2018-05-03 09:39:35 ./configure --enable-async-redis
2778 2018-05-03 09:41:05 ./configure --enable-async-redis --with-php-config=/usr/local/php/bin/php-config
2779 2018-05-03 09:44:18 make && make instatll
2780 2018-05-03 09:44:29 make install
2781 2018-05-03 09:44:36 history
2782 2018-05-03 09:44:49 php --ri swoole
2783 2018-05-03 09:45:00 php --ri swoole | grep redis
2784 2018-05-03 09:45:52 ls
2785 2018-05-03 09:46:53 ./configure --enable-async-redis --with-php-config=/usr/local/php/bin/php-config
2786 2018-05-03 09:47:00 make clean
2787 2018-05-03 09:47:07 make -j
2788 2018-05-03 09:47:59 make install
2789 2018-05-03 09:48:16 php --ri swoole
2790 2018-05-03 09:48:28 make
2791 2018-05-03 09:48:31 make install
2792 2018-05-03 09:49:12 php --ri swoole
最后出现这个就是好的了
最后结果
redis.php
<?php $client = new swoole_redis; $client->connect('127.0.0.1', 6379, function (swoole_redis $client, $result) { echo "connect\n"; var_dump($result); $client->set('key', 'swoole', function (swoole_redis $client, $result) { var_dump($result); $client->get('key', function (swoole_redis $client, $result) { var_dump($result); $client->close(); }); }); }); ?>
启用redis客户端查看下结果