php 如何检查Redis服务器是否正在运行

b1payxdu  于 2023-01-16  发布在  PHP
关注(0)|答案(7)|浏览(309)

如何检查Redis服务器是否在运行?
如果它没有运行,我想回退到使用数据库。
我使用的是FuelPHP框架,所以我愿意接受基于此框架的解决方案,或者只是标准PHP。

k5ifujac

k5ifujac1#

您可以使用命令行来确定Redis是否正在运行:

redis-cli ping

你该回去了

PONG

这表明Redis已经启动并运行。

p1iqtdky

p1iqtdky2#

redis-cli -h host_url -p 6379 ping
mbzjlibv

mbzjlibv3#

你可以尝试获取一个示例(\Redis::instance()),然后像这样使用它:

try
{
    $redis = \Redis::instance();
    // Do something with Redis.
}
catch(\RedisException $e)
{
    // Fall back to other db usage.
}

但是你最好知道redis是否在运行,这只是一种在运行中检测它的方法。

tjjdgumg

tjjdgumg4#

所有答案都很棒,
a另一种方法是选中if default REDIS port is listening
即端口号6379 lsof -i:6379
如果您没有得到上述命令任何输出,则意味着redis没有运行。

3xiyfsfu

3xiyfsfu5#

你可以这样做。

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

echo $redis->ping();

然后检查它是否打印+PONG,这表明redis-server正在运行。

ftf50wuq

ftf50wuq6#

这适用于运行Node-Redis的用户。

const redis = require('redis');

const REDIS_PORT = process.env.REDIS_PORT || 6379

const client = redis.createClient(REDIS_PORT)

const connectRedis = async () => {
  await client.PING().then(

    async () => {
      // what to run if the PING is successful, which also means the server is up.

      console.log("server is running...")
    }, 
    async () => {
      // what to run if the PING is unsuccessful, which also means the server is down.

      console.log("server is not running, trying to connect...")
      client.on('error', (err) => console.log('Redis Client Error', err));
      await client.connect();
    })
return
}
luaexgnf

luaexgnf7#

您可以在linux上使用以下命令:

systemctl status redis-server

它将给予以下输出:

● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; disabled; vendor preset: enabled)
Active: active (running) since Wed 2023-01-11 19:14:30 UTC; 1 day 4h ago
Docs: http://redis.io/documentation, man:redis-server(1)
Main PID: 43270 (redis-server)
Status: "Ready to accept connections"
Tasks: 5 (limit: 477)
Memory: 18.5M
CPU: 3min 34.450s
CGroup: /system.slice/redis-server.service
└─43270 "/usr/bin/redis-server 127.0.0.1:6379" "" "" "" "" "" "" ""
Jan 11 19:14:30 ip-172-31-17-230 systemd[1]: Starting Advanced key-value store...
Jan 11 19:14:30 ip-172-31-17-230 systemd[1]: Started Advanced key-value store.

相关问题