Home > Backend Development > PHP Tutorial > PHP增添Redis模块及连接

PHP增添Redis模块及连接

WBOY
Release: 2016-06-13 12:23:23
Original
985 people have browsed it

PHP添加Redis模块及连接

  上几篇文章介绍了Redis的安装及使用,下面将介绍php如何添加Redis扩展!

  php手册并没有提供Redis的类和方法,也没有提供相关的扩展模块,但我们可以在Redis的官网下载PHP的扩展,里面的扩展比较多,仅以phpredis为例。

  1. phpredis下载
    1. 点击"Repository"进入Redis的学习库,里面有Redis扩展的代码文件和相关的介绍以及类和方法
    2. 点击"releases"进入phpredis的源文件
    3. 下载最新版的phpredis源文件,选择"tar.gz"下载
  2. phpredis配置
    1. 解压phpredis文件
      <span style="color: #008080;">1</span> tar -zxvf phpredis-2.2.7.tar.gz
      Copy after login
    2. 安装配置phpredis
      1. 在phpredis目录下运行phpize5命令,生成configure文件
        phpize5
        Copy after login
      2. 安装配置igbinary
        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>
        Copy after login
      3. 安装配置phpredis
        ./configure --enable-redis-<span style="color: #000000;">igbinarymake <br></span>sudo make install
        Copy after login
      4. 修改php.ini,添加(要按顺序,先添加igbinary.so,再添加redis.so)
        extension=igbinary.<span style="color: #000000;">soextension</span>=redis.so
        Copy after login
      5. 查看phpredis
        1. 使用phpinfo()查看
      6. php连接redis数据库
        1. 开启数据库服务
        2. 新建一个redis.php文件,并键入以下代码进行连接
          <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>);
          Copy after login
        3. 在浏览器中打开该文件,查看查询结果
        4. 参考文档:

            Php-Redis安装测试汇总

Related labels:
source:php.cn
Previous article:php给图片添文字水印 Next article:PHP实现无级递归分门别类(ThinkPHP框架)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template