立即注册PayPal并开始接受信用卡付款。

PHP RRDTool tutorial

On a number of occasions I’ve used RRDTool to graph network traffic and the like. A few years ago when I started using cacti I started wondering how to make the graphs myself. Creating the graphs on the command line isn’t that hard once you know how to set things up and it turns out doing the same in PHP is just as easy.

For this tutorial I’m going to assume you understand how to get RRDTool installed and working from the command line. If not you should take a look at this tutorial (if you are in a hurry look at the “A Real World Example” section) or any of the tutorials on this page.

Setup and introduction

You need to have PHP compiled with RRDTool support to run the following PHP examples. If you compile PHP by hand then see: how to build the php rrdtool extension by hand. If you are using a distribution’s pre-compiled PHP binary you should be able to install a second package with RRDTool support. You can verify that your PHP install is ready to go by running this:


phpinfo(INFO_MODULES);
?>

Then search for “rrdtool” in the output and make sure that “rrdtool support” is enabled.

While going through each of the following steps you will notice that each call takes a couple of parameters and then one parameter that is just a string of options. The string of options is exactly how it is for generating/updating/graphing RRDs from the command line. This makes for a consistent interface for the different languages that have RRDTool support.

Creating a RRD

We first need to create a database for our data. For these examples I will be creating a database that could be used for generating network graphs. This database is created for updates every 5 minutes (300 seconds), has input and output counters that store data for the average and max counts and stores enough samples for hourly, daily, monthly and yearly graphs of both average and max.

 

  $fname = “net.rrd”;

  $opts = array( “–step”, “300″, “–start”, 0,
           “DS:input:COUNTER:600:U:U”,
           “DS:output:COUNTER:600:U:U”,
           “RRA:AVERAGE:0.5:1:600″,
           “RRA:AVERAGE:0.5:6:700″,
           “RRA:AVERAGE:0.5:24:775″,
           “RRA:AVERAGE:0.5:288:797″,
           “RRA:MAX:0.5:1:600″,
           “RRA:MAX:0.5:6:700″,
           “RRA:MAX:0.5:24:775″,
           “RRA:MAX:0.5:288:797″
        );

  $ret = rrd_create($fname, $opts, count($opts));

  if( $ret == 0 )
  {
    $err = rrd_error();
    echo “Create error: $err\n;
  }
?>

After running you will have a file called net.rrd in the current directory. This is your RRD and will contain all the samples for your graphs.

For more information on the options to rrd_create see: rrdcreate

Updating a RRD

The next step is to update your RRD on the frequency you set when you created it. In the case above the frequency was set to 5 minutes (300 seconds). The following script generates random input and output values as input to the update function, sleeps for 300 seconds and then loops.

 

  $fname = “net.rrd”;

  $total_input_traffic = 0;
  $total_output_traffic = 0;

  while(true)
  {
    $total_input_traffic += rand(10000, 15000);
    $total_output_traffic += rand(10000, 30000);

    echo time() . “: “ . $total_input_traffic . ” and “ . $total_output_traffic . \n;

    $ret = rrd_update($fname, “N:$total_input_traffic:$total_output_traffic”);

    if( $ret == 0 )
    {
      $err = rrd_error();
      echo “ERROR occurred: $err\n;
    }

    sleep(300);
  }
?>

Your input and output values could be pulled from anywhere here, I just wanted to have a source that would work for anyone who wanted to try the script.

After letting this script run for a few hours you can run the following shell script to see what type of data you have collected so far:

For more information on the options to rrd_update here: rrdupdate

#!/bin/sh
END=`date +%s`
START=`echo $END-3600|bc` # over the hour
rrdtool fetch net.rrd AVERAGE –start $START –end $END

That should display the average sample values for the last hour.

Displaying RRD data as a graph

Now that you have data in your RRD you will want to graph it. The following code will graph the average input and output for 1 day as well as the max for that day.

 

  $opts = array( “–start”, “-1d”, “–vertical-label=B/s”,
                 “DEF:inoctets=net.rrd:input:AVERAGE”,
                 “DEF:outoctets=net.rrd:output:AVERAGE”,
                 “AREA:inoctets#00FF00:In traffic”,
                 “LINE1:outoctets#0000FF:Out traffic\\r”,
                 “CDEF:inbits=inoctets,8,*”,
                 “CDEF:outbits=outoctets,8,*”,
                 “COMMENT:\\n”,
                 “GPRINT:inbits:AVERAGE:Avg In traffic\: %6.2lf %Sbps”,
                 “COMMENT:  “,
                 “GPRINT:inbits:MAX:Max In traffic\: %6.2lf %Sbps\\r”,
                 “GPRINT:outbits:AVERAGE:Avg Out traffic\: %6.2lf %Sbps”,
                 “COMMENT: “,
                 “GPRINT:outbits:MAX:Max Out traffic\: %6.2lf %Sbps\\r”
               );

  $ret = rrd_graph(“net_1d.gif”, $opts, count($opts));

  if( !is_array($ret) )
  {
    $err = rrd_error();
    echo “rrd_graph() ERROR: $err\n;
  }
?>

You should end up with a file named net_1d.gif in your working directory that looks something like:

You can also do weekly and monthly graphs by changing the start parameter to -1w or -1m:


For the impatient you can download my net.rrd file and try the graphs for yourself.

See the other tutorials for more information on the options you have for graphing.

For more information on the options to rrd_graph see: rrdgraph

农历

function getNong($year,$month,$day,$type)
{
 
 /*
 $type=day;
 $type=month;
 $type=year;
 $type=month-day;
 $type=year-month-day;
 */
  #农历每月的天数
  $everymonth=array(
                    0=>array(8,0,0,0,0,0,0,0,0,0,0,0,29,30,7,1),
                    1=>array(0,29,30,29,29,30,29,30,29,30,30,30,29,0,8,2),
                    2=>array(0,30,29,30,29,29,30,29,30,29,30,30,30,0,9,3),
                    3=>array(5,29,30,29,30,29,29,30,29,29,30,30,29,30,10,4),
                    4=>array(0,30,30,29,30,29,29,30,29,29,30,30,29,0,1,5),
                    5=>array(0,30,30,29,30,30,29,29,30,29,30,29,30,0,2,6),
                    6=>array(4,29,30,30,29,30,29,30,29,30,29,30,29,30,3,7),
                    7=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,4,8),
                    8=>array(0,30,29,29,30,30,29,30,29,30,30,29,30,0,5,9),
                    9=>array(2,29,30,29,29,30,29,30,29,30,30,30,29,30,6,10),
                    10=>array(0,29,30,29,29,30,29,30,29,30,30,30,29,0,7,11),
                    11=>array(6,30,29,30,29,29,30,29,29,30,30,29,30,30,8,12),
                    12=>array(0,30,29,30,29,29,30,29,29,30,30,29,30,0,9,1),
                    13=>array(0,30,30,29,30,29,29,30,29,29,30,29,30,0,10,2),
                    14=>array(5,30,30,29,30,29,30,29,30,29,30,29,29,30,1,3),
                    15=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,2,4),
                    16=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,3,5),
                    17=>array(2,30,29,29,30,29,30,30,29,30,30,29,30,29,4,6),
                    18=>array(0,30,29,29,30,29,30,29,30,30,29,30,30,0,5,7),
                    19=>array(7,29,30,29,29,30,29,29,30,30,29,30,30,30,6,8),
                    20=>array(0,29,30,29,29,30,29,29,30,30,29,30,30,0,7,9),
                    21=>array(0,30,29,30,29,29,30,29,29,30,29,30,30,0,8,10),
                    22=>array(5,30,29,30,30,29,29,30,29,29,30,29,30,30,9,11),
                    23=>array(0,29,30,30,29,30,29,30,29,29,30,29,30,0,10,12),
                    24=>array(0,29,30,30,29,30,30,29,30,29,30,29,29,0,1,1),
                    25=>array(4,30,29,30,29,30,30,29,30,30,29,30,29,30,2,2),
                    26=>array(0,29,29,30,29,30,29,30,30,29,30,30,29,0,3,3),
                    27=>array(0,30,29,29,30,29,30,29,30,29,30,30,30,0,4,4),
                    28=>array(2,29,30,29,29,30,29,29,30,29,30,30,30,30,5,5),
                    29=>array(0,29,30,29,29,30,29,29,30,29,30,30,30,0,6,6),
                    30=>array(6,29,30,30,29,29,30,29,29,30,29,30,30,29,7,7),
                    31=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,8,8),
                    32=>array(0,30,30,30,29,30,29,30,29,29,30,29,30,0,9,9),
                    33=>array(5,29,30,30,29,30,30,29,30,29,30,29,29,30,10,10),
                    34=>array(0,29,30,29,30,30,29,30,29,30,30,29,30,0,1,11),
                    35=>array(0,29,29,30,29,30,29,30,30,29,30,30,29,0,2,12),
                    36=>array(3,30,29,29,30,29,29,30,30,29,30,30,30,29,3,1),
                    37=>array(0,30,29,29,30,29,29,30,29,30,30,30,29,0,4,2),
                    38=>array(7,30,30,29,29,30,29,29,30,29,30,30,29,30,5,3),
                    39=>array(0,30,30,29,29,30,29,29,30,29,30,29,30,0,6,4),
                    40=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,7,5),
                    41=>array(6,30,30,29,30,30,29,30,29,29,30,29,30,29,8,6),
                    42=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,9,7),
                    43=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,10,8),
                    44=>array(4,30,29,30,29,30,29,30,29,30,30,29,30,30,1,9),
                    45=>array(0,29,29,30,29,29,30,29,30,30,30,29,30,0,2,10),
                    46=>array(0,30,29,29,30,29,29,30,29,30,30,29,30,0,3,11),
                    47=>array(2,30,30,29,29,30,29,29,30,29,30,29,30,30,4,12),
                    48=>array(0,30,29,30,29,30,29,29,30,29,30,29,30,0,5,1),
                    49=>array(7,30,29,30,30,29,30,29,29,30,29,30,29,30,6,2),
                    50=>array(0,29,30,30,29,30,30,29,29,30,29,30,29,0,7,3),
                    51=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,8,4),
                    52=>array(5,29,30,29,30,29,30,29,30,30,29,30,29,30,9,5),
                    53=>array(0,29,30,29,29,30,30,29,30,30,29,30,29,0,10,6),
                    54=>array(0,30,29,30,29,29,30,29,30,30,29,30,30,0,1,7),
                    55=>array(3,29,30,29,30,29,29,30,29,30,29,30,30,30,2,8),
                    56=>array(0,29,30,29,30,29,29,30,29,30,29,30,30,0,3,9),
                    57=>array(8,30,29,30,29,30,29,29,30,29,30,29,30,29,4,10),
                    58=>array(0,30,30,30,29,30,29,29,30,29,30,29,30,0,5,11),
                    59=>array(0,29,30,30,29,30,29,30,29,30,29,30,29,0,6,12),
                    60=>array(6,30,29,30,29,30,30,29,30,29,30,29,30,29,7,1),
                    61=>array(0,30,29,30,29,30,29,30,30,29,30,29,30,0,8,2),
                    62=>array(0,29,30,29,29,30,29,30,30,29,30,30,29,0,9,3),
                    63=>array(4,30,29,30,29,29,30,29,30,29,30,30,30,29,10,4),
                    64=>array(0,30,29,30,29,29,30,29,30,29,30,30,30,0,1,5),
                    65=>array(0,29,30,29,30,29,29,30,29,29,30,30,29,0,2,6),
                    66=>array(3,30,30,30,29,30,29,29,30,29,29,30,30,29,3,7),
                    67=>array(0,30,30,29,30,30,29,29,30,29,30,29,30,0,4,8),
                    68=>array(7,29,30,29,30,30,29,30,29,30,29,30,29,30,5,9),
                    69=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,6,10),
                    70=>array(0,30,29,29,30,29,30,30,29,30,30,29,30,0,7,11),
                    71=>array(5,29,30,29,29,30,29,30,29,30,30,30,29,30,8,12),
                    72=>array(0,29,30,29,29,30,29,30,29,30,30,29,30,0,9,1),
                    73=>array(0,30,29,30,29,29,30,29,29,30,30,29,30,0,10,2),
                    74=>array(4,30,30,29,30,29,29,30,29,29,30,30,29,30,1,3),
                    75=>array(0,30,30,29,30,29,29,30,29,29,30,29,30,0,2,4),
                    76=>array(8,30,30,29,30,29,30,29,30,29,29,30,29,30,3,5),
                    77=>array(0,30,29,30,30,29,30,29,30,29,30,29,29,0,4,6),
                    78=>array(0,30,29,30,30,29,30,30,29,30,29,30,29,0,5,7),
                    79=>array(6,30,29,29,30,29,30,30,29,30,30,29,30,29,6,8),
                    80=>array(0,30,29,29,30,29,30,29,30,30,29,30,30,0,7,9),
                    81=>array(0,29,30,29,29,30,29,29,30,30,29,30,30,0,8,10),
                    82=>array(4,30,29,30,29,29,30,29,29,30,29,30,30,30,9,11),
                    83=>array(0,30,29,30,29,29,30,29,29,30,29,30,30,0,10,12),
                    84=>array(10,30,29,30,30,29,29,30,29,29,30,29,30,30,1,1),
                    85=>array(0,29,30,30,29,30,29,30,29,29,30,29,30,0,2,2),
                    86=>array(0,29,30,30,29,30,30,29,30,29,30,29,29,0,3,3),
                    87=>array(6,30,29,30,29,30,30,29,30,30,29,30,29,29,4,4),
                    88=>array(0,30,29,30,29,30,29,30,30,29,30,30,29,0,5,5),
                    89=>array(0,30,29,29,30,29,29,30,30,29,30,30,30,0,6,6),
                    90=>array(5,29,30,29,29,30,29,29,30,29,30,30,30,30,7,7),
                    91=>array(0,29,30,29,29,30,29,29,30,29,30,30,30,0,8,8),
                    92=>array(0,29,30,30,29,29,30,29,29,30,29,30,30,0,9,9),
                    93=>array(3,29,30,30,29,30,29,30,29,29,30,29,30,29,10,10),
                    94=>array(0,30,30,30,29,30,29,30,29,29,30,29,30,0,1,11),
                    95=>array(8,29,30,30,29,30,29,30,30,29,29,30,29,30,2,12),
                    96=>array(0,29,30,29,30,30,29,30,29,30,30,29,29,0,3,1),
                    97=>array(0,30,29,30,29,30,29,30,30,29,30,30,29,0,4,2),
                    98=>array(5,30,29,29,30,29,29,30,30,29,30,30,29,30,5,3),
                    99=>array(0,30,29,29,30,29,29,30,29,30,30,30,29,0,6,4),
                    100=>array(0,30,30,29,29,30,29,29,30,29,30,30,29,0,7,5),
                    101=>array(4,30,30,29,30,29,30,29,29,30,29,30,29,30,8,6),
                    102=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,9,7),
                    103=>array(0,30,30,29,30,30,29,30,29,29,30,29,30,0,10,8),
                    104=>array(2,29,30,29,30,30,29,30,29,30,29,30,29,30,1,9),
                    105=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,2,10),
                    106=>array(7,30,29,30,29,30,29,30,29,30,30,29,30,30,3,11),
                    107=>array(0,29,29,30,29,29,30,29,30,30,30,29,30,0,4,12),
                    108=>array(0,30,29,29,30,29,29,30,29,30,30,29,30,0,5,1),
                    109=>array(5,30,30,29,29,30,29,29,30,29,30,29,30,30,6,2),
                    110=>array(0,30,29,30,29,30,29,29,30,29,30,29,30,0,7,3),
                    111=>array(0,30,29,30,30,29,30,29,29,30,29,30,29,0,8,4),
                    112=>array(4,30,29,30,30,29,30,29,30,29,30,29,30,29,9,5),
                    113=>array(0,30,29,30,29,30,30,29,30,29,30,29,30,0,10,6),
                    114=>array(9,29,30,29,30,29,30,29,30,30,29,30,29,30,1,7),
                    115=>array(0,29,30,29,29,30,29,30,30,30,29,30,29,0,2,8),
                    116=>array(0,30,29,30,29,29,30,29,30,30,29,30,30,0,3,9),
                    117=>array(6,29,30,29,30,29,29,30,29,30,29,30,30,30,4,10),
                    118=>array(0,29,30,29,30,29,29,30,29,30,29,30,30,0,5,11),
                    119=>array(0,30,29,30,29,30,29,29,30,29,29,30,30,0,6,12),
                    120=>array(4,29,30,30,30,29,30,29,29,30,29,30,29,30,7,1)
                   );
##############################
  #农历天干
  $mten=array(”null”,”甲”,”乙”,”丙”,”丁”,”戊”,”己”,”庚”,”辛”,”壬”,”癸”);
  #农历地支
  $mtwelve=array(”null”,”子(鼠)”,”丑(牛)”,”寅(虎)”,”卯(兔)”,”辰(龙)”,
                 “巳(蛇)”,”午(马)”,”未(羊)”,”申(猴)”,”酉(鸡)”,”戌(狗)”,”亥(猪)”);
  #农历月份
  $mmonth=array(”闰”,”正”,”二”,”三”,”四”,”五”,”六”,
                “七”,”八”,”九”,”十”,”十一”,”十二”,”月”);
  #农历日
  $mday=array(”null”,”初一”,”初二”,”初三”,”初四”,”初五”,”初六”,”初七”,”初八”,”初九”,”初十”,
              “十一”,”十二”,”十三”,”十四”,”十五”,”十六”,”十七”,”十八”,”十九”,”二十”,
              “廿一”,”廿二”,”廿三”,”廿四”,”廿五”,”廿六”,”廿七”,”廿八”,”廿九”,”三十”);
##############################
  #星期
  $weekday = array(”星期日”,”星期一”,”星期二”,”星期三”,”星期四”,”星期五”,”星期六”);

  #阳历总天数 至1900年12月21日
  $total=11;
  #阴历总天数
  $mtotal=0;
##############################
  #获得当日日期
  //$today=getdate();
  $today[”year”]=$year;
  $today[”mon”]=$month;
  $today[”mday”]=$day;
  if($today[”year”]<1901 || $today["year"]>2020) die(”年份出错!”);

  $cur_wday=$today[”wday”];

  for($y=1901;$y<$today["year"];$y++) { //计算到所求日期阳历的总天数-自1900年12月21日始,先算年的和
       $total+=365;
       if ($y%4==0) $total++;
  }

  switch($today["mon"]) { //再加当年的几个月
         case 12:
              $total+=30;
         case 11:
              $total+=31;
         case 10:
              $total+=30;
         case 9:
              $total+=31;
         case 8:
              $total+=31;
         case 7:
              $total+=30;
         case 6:
              $total+=31;
         case 5:
              $total+=30;
         case 4:
              $total+=31;
         case 3:
              $total+=28;
         case 2:
              $total+=31;
  }

  if($today["year"]%4 == 0 && $today["mon"]>2) $total++; //如果当年是闰年还要加一天

  $total=$total+$today[”mday”]-1; //加当月的天数

  $flag1=0;  //判断跳出循环的条件
  $j=0;
  while ($j<=120){  //用农历的天数累加来判断是否超过阳历的天数
      $i=1;
      while ($i<=13){
            $mtotal+=$everymonth[$j][$i];
            if ($mtotal>=$total){
                 $flag1=1;
                 break;
            }
            $i++;
      }
      if ($flag1==1) break;
      $j++;
  }

  if($everymonth[$j][0]<>0 and $everymonth[$j][0]<$i){ //原来错在这里,对闰月没有修补
      $mm=$i-1;
  }
  else{
      $mm=$i;
  }

  if($i==$everymonth[$j][0]+1 and $everymonth[$j][0]<>0) {
      $nlmon=$mmonth[0].$mmonth[$mm];#闰月
  }
  else {
      $nlmon=$mmonth[$mm].$mmonth[13];
  }

  #计算所求月份1号的农历日期
  $md=$everymonth[$j][$i]-($mtotal-$total);
  if($md > $everymonth[$j][$i])
      $md-=$everymonth[$j][$i];
  $nlday=$mday[$md];

  $nowday=date(”Y年n月j日 “).$weekday[$cur_wday].”
“.$mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]].”年”.$nlmon.$nlday;
 
 switch($type)
 {
  case “day”:$rv=$nlday;break;
  case “month”:$rv=$nlmon;break;
  case “year”:$rv=$mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]];break;
  case “month-day”:$rv=$nlmon.$nlday;break;
  case “year-month-day”:$rv=$mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]].”年”.$nlmon.$nlday;break;
  default:$rv=$mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]].”年”.$nlmon.$nlday;break;
 }
 return $rv;
  //return $mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]];
 
}

Get IP ADDRESS

function get_ipaddress()
{
 if ( !empty($_SERVER[”HTTP_X_FORWARDED_FOR”]) )
 {   
  $temp_ip = split(”,”, $_SERVER[”HTTP_X_FORWARDED_FOR”]);
  $user_ip = $temp_ip[0];
 }
 else
 {   
  $user_ip = $_SERVER[”REMOTE_ADDR”]; 
 }
 return $user_ip;
}

 

 

function getCurrURI()
{
 return $url_this =  “http://”.$_SERVER [’HTTP_HOST’].$_SERVER[’PHP_SELF’];
}
function getCurrMonthDocument($currpath)
{
 $now = getdate();
 $year=$now[year];
 $month=$now[mon];
 $docuement=$currpath.$year.$month;
 $revalue=$year.$month;
 if(is_dir($docuement))
  return $revalue.”/”;
 else
 {
  mkdir($docuement,0777);
  return $revalue.”/”;
 }
}

 

 

function prev_day($fyear, $fmonth, $fday)
{
  return date (”Y-m-d”, mktime (0,0,0,$fmonth,$fday-1,$fyear));
}

function next_day($fyear, $fmonth, $fday)
{
  return date (”Y-m-d”, mktime (0,0,0,$fmonth,$fday+1,$fyear));
}
 

 
 

save log to file

function savelog($text){
 $now = getdate();
 $nowstr= $now[year].”-”.$now[mon].”-”.$now[mday].” “.$now[hours].”:”.$now[minutes].”:”.$now[seconds];
 $text=$nowstr.”  “.time().”:”.$text.” (”.$_SERVER[QUERY_STRING].”)\n”;
 $handle = fopen(”log_new.php”, ‘a’);
 if($handle){
  fwrite($handle, $text);
  fclose($handle);
 }
}

一、介绍

  这篇文档旨在介绍如何安装配置基于2台服务器的MySQL集群。并且实现任意一台服务器出现问题或宕机时MySQL依然能够继续运行。

  注意!

  虽然这是基于2台服务器的MySQL集群,但也必须有额外的第三台服务器作为管理节点,但这台服务器可以在集群启动完成后关闭。同时需要注意的是并不推荐在集群启动完成后关闭作为管理节点的服务器。尽管理论上可以建立基于只有2台服务器的MySQL集群,但是这样的架构,一旦一台服务器宕机之后集群就无法继续正常工作了,这样也就失去了集群的意义了。出于这个原因,就需要有第三台服务器作为管理节点运行。

  另外,可能很多朋友都没有3台服务器的实际环境,可以考虑在VMWare或其他虚拟机中进行实验。

  下面假设这3台服务的情况:

  Server1: mysql1.vmtest.net 192.168.0.1

  Server2: mysql2.vmtest.net 192.168.0.2

  Server3: mysql3.vmtest.net 192.168.0.3

  Servers1和Server2作为实际配置MySQL集群的服务器。对于作为管理节点的Server3则要求较低,只需对Server3的系统进行很小的调整并且无需安装MySQL,Server3可以使用一台配置较低的计算机并且可以在Server3同时运行其他服务。

  二、在Server1和Server2上安装MySQL

  =================================

  从http://www.mysql.com上下载mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz

  注意:必须是max版本的MySQL,Standard版本不支持集群部署!

  以下步骤需要在Server1和Server2上各做一次

  # mv mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz /usr/local/

  # cd /usr/local/

  # groupadd mysql

  # useradd -g mysql mysql

  # tar -zxvf mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz

  # rm -f mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz

  # mv mysql-max-4.1.9-pc-linux-gnu-i686 mysql

  # cd mysql

  # scripts/mysql_install_db –user=mysql

  # chown -R root

  .

  # chown -R mysql data

  # chgrp -R mysql .

  # cp support-files/mysql.server /etc/rc.d/init.d/mysqld

  # chmod +x /etc/rc.d/init.d/mysqld

  # chkconfig –add mysqld

  此时不要启动MySQL!三、安装并配置管理节点服务器(Server3)

  =====================================

  作为管理节点服务器,Server3需要ndb_mgm和ndb_mgmd两个文件:

  

  从http://www.mysql.com上下载mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz

  # mkdir /usr/src/mysql-mgm

  # cd /usr/src/mysql-mgm

  # tar -zxvf mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz

  # rm mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz

  # cd mysql-max-4.1.9-pc-linux-gnu-i686

  # mv bin/ndb_mgm .

  # mv bin/ndb_mgmd .

  # chmod +x ndb_mg*

  # mv ndb_mg* /usr/bin/

  # cd

  # rm -rf /usr/src/mysql-mgm

  现在开始为这台管理节点服务器建立配置文件:

  # mkdir /var/lib/mysql-cluster

  # cd /var/lib/mysql-cluster

  # vi config.ini

  在config.ini中添加如下内容:

  [NDBD DEFAULT]

  NoOfReplicas=2

  [MYSQLD DEFAULT]

  [NDB_MGMD DEFAULT]

  [TCP DEFAULT]

  # Managment Server

  [NDB_MGMD]HostName=192.168.0.3 #管理节点服务器Server3的IP地址

  # Storage Engines

  [NDBD]

  HostName=192.168.0.1 #MySQL集群Server1的IP地址

  DataDir= /var/lib/mysql-cluster

  [NDBD]

  HostName=192.168.0.2 #MySQL集群Server2的IP地址

  DataDir=/var/lib/mysql-cluster

  # 以下2个[MYSQLD]可以填写Server1和Server2的主机名。

  # 但为了能够更快的更换集群中的服务器,推荐留空,否则更换服务器后必须对这个配置进行更改。

  [MYSQLD]

  [MYSQLD]

  保存退出后,启动管理节点服务器Server3:

  # ndb_mgmd

  启动管理节点后应该注意,这只是管理节点服务,并不是管理终端。因而你看不到任何关于启动后的输出信息。

  四、配置集群服务器并启动MySQL

  =============================

  在Server1和Server2中都需要进行如下改动:

  # vi /etc/my.cnf

  [mysqld]

  ndbclusterndb-conneCTstring=192.168.0.3 #Server3的IP地址

  [mysql_cluster]

  ndb-connectstring=192.168.0.3 #Server3的IP地址

  保存退出后,建立数据目录并启动MySQL:

  # mkdir /var/lib/mysql-cluster

  # cd /var/lib/mysql-cluster

  # /usr/local/mysql/bin/ndbd –initial

  # /etc/rc.d/init.d/mysqld start

  可以把/usr/local/mysql/bin/ndbd加到/etc/rc.local中实现开机启动。

  注意:只有在第一次启动ndbd时或者对Server3的config.ini进行改动后才需要使用–initial参数!

  

  五、检查工作状态

  ================

  回到管理节点服务器Server3上,并启动管理终端:

  # /usr/bin/ndb_mgm

  键入show命令查看当前工作状态:(下面是一个状态输出示例)

  [root@mysql3 root]# /usr/bin/ndb_mgm

  – NDB Cluster — Management Client –

  ndb_mgm> show

  Connected to Management Server at: localhost:1186

  Cluster Configuration

  ———————

  [ndbd(NDB)]

  2 node(s)

  id=2

  @192.168.0.1

  (Version: 4.1.9, Nodegroup: 0, Master)

  id=3

  @192.168.0.2

  (Version: 4.1.9, Nodegroup: 0)

  [ndb_mgmd(MGM)] 1 node(s)

  id=1

  @192.168.0.3

  (Version: 4.1.9)

  [mysqld(API)]

  2 node(s)

  id=4

  (Version: 4.1.9)

  id=5

  (Version: 4.1.9)

  ndb_mgm>如果上面没有问题,现在开始测试MySQL:

  注意,这篇文档对于MySQL并没有设置root密码,推荐你自己设置Server1和Server2的MySQL root密码。

  在Server1中:

  # /usr/local/mysql/bin/mysql -u root -p

  > use test;

  > CREATE TABLE ctest (i INT) ENGINE=NDBCLUSTER;

  > INSERT INTO ctest () VALUES (1);

  > SELECT * FROM ctest;

  应该可以看到1 row returned信息(返回数值1)。

  如果上述正常,则换到Server2上重复上面的测试,观察效果。如果成功,则在Server2中执行INSERT再换回到Server1观察是否工作正常。

  如果都没有问题,那么恭喜成功!

  六、破坏性测试

  ==============

  将Server1或Server2的网线拔掉,观察另外一台集群服务器工作是否正常(可以使用SELECT查询测试)。测试完毕后,重新插入网线即可。

  如果你接触不到物理服务器,也就是说不能拔掉网线,那也可以这样测试:

  在Server1或Server2上:

  # ps aux | grep ndbd

  将会看到所有ndbd进程信息:

  root

  5578

  0.0

  0.3

  6220 1964 ?

  S

  03:14

  0:00 ndbd

  root

  5579

  0.0 20.4 492072 102828 ?

  R

  03:14

  0:04 ndbd

  root

  23532

  0.0

  0.1

  3680

  684 pts/1

  S

  07:59

  0:00 grep ndbd

  然后杀掉一个ndbd进程以达到破坏MySQL集群服务器的目的:

  # kill -9 5578 5579

  之后在另一台集群服务器上使用SELECT查询测试。并且在管理节点服务器的管理终端中执行show命令会看到被破坏的那台服务器的状态。

  

  测试完成后,只需要重新启动被破坏服务器的ndbd进程即可:

  # ndbd

  注意!前面说过了,此时是不用加–inital参数的!

  至此,MySQL集群就配置完成了

 

老外写的图文:http://www.jimdowling.info/ndbinstaller-trac/wiki/DetailedLocalhostInstall




Calender

    August 2008
    M T W T F S S
    « Jun    
     123
    45678910
    11121314151617
    18192021222324
    25262728293031


ok