Maison > Article > développement back-end > PHP增添Redis模块及连接
PHP添加Redis模块及连接
上几篇文章介绍了Redis的安装及使用,下面将介绍php如何添加Redis扩展!
php手册并没有提供Redis的类和方法,也没有提供相关的扩展模块,但我们可以在Redis的官网下载PHP的扩展,里面的扩展比较多,仅以phpredis为例。
<span style="color: #008080;">1</span> tar -zxvf phpredis-2.2.7.tar.gz
phpize5
wget http:<span style="color: #008000;">//</span><span style="color: #008000;">pecl.php.net/get/igbinary-1.2.1.tgz</span>./<span style="color: #000000;">configuremakesudo make install</span>
./configure --enable-redis-<span style="color: #000000;">igbinarymake <br></span>sudo make install
extension=igbinary.<span style="color: #000000;">soextension</span>=redis.so
<span style="color: #008080;">1</span> <span style="color: #008000;">//</span><span style="color: #008000;">实例化</span><span style="color: #008080;">2</span> <span style="color: #800080;">$redis</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Redis();</span><span style="color: #008080;">3</span> <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span><span style="color: #008080;">4</span> <span style="color: #800080;">$redis</span>->connect('127.0.0.1',6379<span style="color: #000000;">);</span><span style="color: #008080;">5</span> <span style="color: #008000;">//</span><span style="color: #008000;">授权,password为你设置的授权密码,如未设置,可不使用该方法</span><span style="color: #008080;">6</span> <span style="color: #800080;">$redis</span>->auth(password<span style="color: #000000;">);</span><span style="color: #008080;">7</span> <span style="color: #008000;">//</span><span style="color: #008000;">keys方法,查询所有key</span><span style="color: #008080;">8</span> <span style="color: #800080;">$keys</span> = <span style="color: #800080;">$redis</span>->keys("*"<span style="color: #000000;">);</span><span style="color: #008080;">9</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$keys</span>);
参考文档:
Php-Redis安装测试汇总