Home  >  Article  >  Backend Development  >  Method of redis master-slave database status detection function implemented in PHP

Method of redis master-slave database status detection function implemented in PHP

墨辰丷
墨辰丷Original
2018-05-19 10:45:591472browse

This article mainly introduces the redis master-slave database status detection function implemented by PHP, involving PHP's connection, detection, error message output and email sending related operation skills for multiple redis master-slave databases. Friends in need can refer to the following

Example:

<?php
/**
 * 检测多个主从redis数据库是否挂掉
 * 建立从数据库$redis_db的二维数组,内容包含每个从服务器的配置数据
 */
header("Content-Type: text/html; charset=utf-8");
set_time_limit(0);
$redis_db = array(
  &#39;db1&#39;=>array(
    &#39;hostname&#39; => &#39;127.0.0.1&#39;,
    &#39;port&#39; => 6379,
    &#39;password&#39; => &#39;&#39;,
  ),
  &#39;db2&#39;=>array(
    &#39;hostname&#39; => &#39;192.168.2.179&#39;,
    &#39;port&#39; => 6379,
    &#39;password&#39; => &#39;111111&#39;,
  ),
);
$content = &#39;&#39;;
foreach ($redis_db as $db_key) {
  $host = $db_key[&#39;hostname&#39;];
  $port = $db_key[&#39;port&#39;];
  $redis = new Redis();
  //连接本地的 Redis 服务
  $status= $redis->connect($host, $port);
  if(!$status) {
    $content .= "redis从数据库( $host )无法连接 ! <br/>";
    continue;
  }
  if(!empty($db_key[&#39;password&#39;])) {
    $pass = $redis->auth($db_key[&#39;password&#39;]);
    if(!$pass) {
      $content .= "redis从数据库( $host )密码错误 ! <br/>";
      continue;
    }
  }
  try {
    $config = $redis->info();
    if(&#39;up&#39; == $config[&#39;master_link_status&#39;]) {
    } else {
      $content .= "redis从数据库( $host )挂掉了! <br/>";
    }
  }
  catch(RedisException $e)
  {
    $content .= "redis从数据库( $host )报错:" . $e->getMessage()."<br/>";
  }
}
//若报错信息不为空,发送报错邮件
if(!empty($content)) {
  $title = &#39;主从redis数据库状态检测报错 &#39;;
  $content = date("Y-m-d H:i:s",time()) . "<br/>" . $content;
  $sendurl = "http://localhost/api.com/test.php?title=".$title."&content=".$content;
  $result = file_get_contents($sendurl);
  if(&#39;ok&#39; != $result) {
    $message = date("Y-m-d H:i:s",time()).&#39; redisSlave.php 主从redis数据库状态检测报错 邮件发送失败!&#39;."\n";
    $content = str_replace("<br/>", "\n", $content);
    $message .= $content;
    error_log($message,3,"error.log");
  }
}

##Related recommendations:

Under Linux The installation method of the Redis database is shared with the automatic startup script

php implementationredis databaseMethod for specifying the database number migration,redis database _PHP tutorial

php implementationredis databaseMethod for specifying library number migration_PHP tutorial

The above is the detailed content of Method of redis master-slave database status detection function implemented in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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