本篇参照安装redis和php的redis扩展进行安装
安装redis5.0.3
yum install redis systemctl enable redis
nano /usr/local/redis/etc/redis.conf
启动redis
systemctl start redis
- 安装redis6.2.4
cd /lnmp yum remove redis wget https://www.fanlog.org/wp-content/uploads/2021/06/redis-6.2.4.zip unzip redis-6.2.4.zip tar xzf redis-6.2.4.tar.gz cd redis-6.2.4 make make test make install PREFIX=/usr/local/redis mkdir -p /usr/local/redis/etc mv /lnmp/redis-6.2.4/redis.conf /usr/local/redis/etc
修改redis.conf如下:
nano /usr/local/redis/etc/redis.conf 修改如下: daemonize yes
添加系统服务
nano /usr/lib/systemd/system/redis.service 添加如下 [Unit] Description=redis After=network.target [Service] Type=forking ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf ExecStop=/usr/local/redis/bin/redis-cli shutdown Restart=always [Install] WantedBy=multi-user.target
- 安装php7.4的redis插件
cd /lnmp wget https://www.fanlog.org/wp-content/uploads/2021/06/phpredis-5.2.0.zip unzip phpredis-5.2.0.zip tar xzf phpredis-5.2.0.tar.gz cd phpredis-5.2.0/ /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make make install
修改php.ini
nano /usr/local/php/lib/php.ini 添加 extension=redis.so
重启php-pfm
systemctl restart php-fpm
使用redis1.php进行测试
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server successfully"; //查看服务是否运行 echo "Server is running: " . $redis->ping(); ?>
显示结果如下:
Connection to server successfullyServer is running: 1
使用redis2.php进行测试
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server successfully"; //设置 redis 字符串数据 $redis->set("tutorial-name", "Redis tutorial"); // 获取存储的数据并输出 echo "Stored string in redis:: " . $redis->get("tutorial-name"); ?>
显示结果如下:
Connection to server successfullyStored string in redis:: Redis tutorial
使用redis3.php进行测试
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server successfully"; //存储数据到列表中 $redis->lpush("tutorial-list", "Redis"); $redis->lpush("tutorial-list", "Mongodb"); $redis->lpush("tutorial-list", "Mysql"); // 获取存储的数据并输出 $arList = $redis->lrange("tutorial-list", 0 ,5); echo "Stored string in redis"; print_r($arList); ?>
显示结果如下:
Connection to server successfullyStored string in redisArray ( [0] => Mysql [1] => Mongodb [2] => Redis [3] => Mysql [4] => Mongodb [5] => Redis )
使用redis4.php进行测试
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server successfully"; // 获取数据并输出 $arList = $redis->keys("*"); echo "Stored keys in redis:: "; print_r($arList); ?>
显示结果如下:
Connection to server successfullyStored keys in redis:: Array ( [0] => PHPREDIS_SESSION:16vggb42sp1ildndl9mts2ro53 [1] => PHPREDIS_SESSION:fuutmo2np1jo6gmmga4jvpc181 [2] => tutorial-list [3] => tutorial-name [4] => PHPREDIS_SESSION:6dg28l765fe2tp9rsmoh6ue5pl [5] => PHPREDIS_SESSION:htk39rjc4v4cs32vau4auba9l4 [6] => PHPREDIS_SESSION:vrg3a8in7btsrl7oml0u1jaif2 [7] => PHPREDIS_SESSION:a9ftc67bc9qqhir62jhsqnmc7d [8] => PHPREDIS_SESSION:fkes93qa36acmll25r5gvg7fje [9] => PHPREDIS_SESSION:6qq60safhi9v8ctv4qufua1aj0 [10] => PHPREDIS_SESSION:gvae9sjliceb9fckus04bljk7m )
参考资料:
- https://www.daehub.com/archives/9663.html
- https://www.runoob.com/redis/redis-php.html