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

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

如果它不运行,我想退回到使用数据库。

我使用的是 FuelPHP 框架,所以我对基于此的解决方案,或者只是标准的 PHP 持开放态度。

135031 次浏览

您可以尝试获取一个实例(Redis: : instance ()) ,并像下面这样处理它:

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

但是你最好知道红外线是否在运行。这只是在飞行中检测它的方法。

你可以这样做。

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


echo $redis->ping();

然后检查它是否打印 +PONG,显示重新编辑服务器正在运行。

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

redis-cli ping

你该回去了

PONG

显示红迪斯正在运行。

所有的答案都很棒,

A 另一种方法是检查 if default REDIS port is listening

即端口号6379 lsof -i:6379

如果您没有得到以上命令的任何输出,那么它意味着 redis 没有运行。

redis-cli -h host_url -p 6379 ping

这是为那些运行 节点-雷迪斯的人准备的。

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
}