本文實(shí)例講述了PHP實(shí)現(xiàn)的redis主從數(shù)據(jù)庫狀態(tài)檢測功能。分享給大家供大家參考,具體如下:
實(shí)例:
<?php /** * 檢測多個主從redis數(shù)據(jù)庫是否掛掉 * 建立從數(shù)據(jù)庫$redis_db的二維數(shù)組,內(nèi)容包含每個從服務(wù)器的配置數(shù)據(jù) */ header("Content-Type: text/html; charset=utf-8"); set_time_limit(0); $redis_db = array( 'db1'=>array( 'hostname' => '127.0.0.1', 'port' => 6379, 'password' => '', ), 'db2'=>array( 'hostname' => '192.168.2.179', 'port' => 6379, 'password' => '111111', ), ); $content = ''; foreach ($redis_db as $db_key) { $host = $db_key['hostname']; $port = $db_key['port']; $redis = new Redis(); //連接本地的 Redis 服務(wù) $status= $redis->connect($host, $port); if(!$status) { $content .= "redis從數(shù)據(jù)庫( $host )無法連接 ! <br/>"; continue; } if(!empty($db_key['password'])) { $pass = $redis->auth($db_key['password']); if(!$pass) { $content .= "redis從數(shù)據(jù)庫( $host )密碼錯誤 ! <br/>"; continue; } } try { $config = $redis->info(); if('up' == $config['master_link_status']) { } else { $content .= "redis從數(shù)據(jù)庫( $host )掛掉了! <br/>"; } } catch(RedisException $e) { $content .= "redis從數(shù)據(jù)庫( $host )報錯:" . $e->getMessage()."<br/>"; } } //若報錯信息不為空,發(fā)送報錯郵件 if(!empty($content)) { $title = '主從redis數(shù)據(jù)庫狀態(tài)檢測報錯 '; $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('ok' != $result) { $message = date("Y-m-d H:i:s",time()).' redisSlave.php 主從redis數(shù)據(jù)庫狀態(tài)檢測報錯 郵件發(fā)送失敗!'."\n"; $content = str_replace("<br/>", "\n", $content); $message .= $content; error_log($message,3,"error.log"); } }
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+redis數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP基本語法入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
【網(wǎng)站聲明】本站除付費(fèi)源碼經(jīng)過測試外,其他素材未做測試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請勿用于商業(yè)用途。如損害你的權(quán)益請聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。