Discuz!官方免费开源建站系统

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

终于做好了.欢迎大家评一评!<mysql数据库大小的限制>

[复制链接]
BENDY 发表于 2004-1-8 19:07:20 | 显示全部楼层 |阅读模式
使用PHP实现的程序.花了一个下午去完成...
只是做出来让大家参考一下...献丑了....

程序思路:
一\与MYSQL数据库结合.先在MYSQL数据库另起一个库.记录数据库的库名,对应的用户名,限制的大小.等.....
二\系统检测数据库大小,然后对比记录着的资料.对比是否超过流量.如果超过流量就使用MYSQL的ROOT权.限制用户对该数据库的权限...(删除UPDATE\INST..等)
三\如果达到80%.就向管理员\用户各发送一个EMAIL通知..
四\前台程序控制数据库资料的整理...


系统分二个部份.

第一部份.是系统定时检测数据库大小,再根据检测结果与数据库资料.判断数据库是否超大....该部份操作需要有MYSQL高权限用户去完成(建议ROOT).用该文件需要定时运行.,但该文件可以放在网站访问不到的保密地方...


所有文件.打包下载.
http://www.xingkong.biz/mysql_limit.zip


  1. <?php
  2. //设置部分
  3. $id=mysql_connect('localhost','user','password');   //最好是使用root,或者高权限用户

  4. //FUN部份
  5. function PMA_backquote($a_name, $do_it = TRUE)   // 取自phpmyadmin,用来格式化数据库名
  6. {
  7.         if ($do_it
  8.             && !empty($a_name) && $a_name != '*') {

  9.             if (is_array($a_name)) {
  10.                  $result = array();
  11.                  reset($a_name);
  12.                  while(list($key, $val) = each($a_name)) {
  13.                      $result[$key] = '`' . $val . '`';
  14.                  }
  15.                  return $result;
  16.             } else {
  17.                 return '`' . $a_name . '`';
  18.             }
  19.         } else {
  20.             return $a_name;
  21.         }
  22. } // end of the 'PMA_backquote()' function


  23. function limit($user,$db)   //达到限制限制用户权限后运行的程序
  24. {
  25.         $query='REVOKE INSERT ,UPDATE ,CREATE ON "'.$db.'".* FROM "'.$user.'@localhost';//将insert update create的权限移走。01/17修正大BUG
  26.         $result      = @mysql_query($query);//changed! only 1 query....
  27.         //相应的权限代码可以再更改.
  28.         //echo 'lim.debug';exit;
  29. }

  30. function warning($name,$email)   //超过80%时通知用户
  31. {
  32.         $admin_email='admin@admin.com';     //管理员EMAIL地址。请更改
  33.         $message='MYSQL用户:'.$name.'的数据库已超过系统允许的大小的80% /n 请及时整理数据';   //通知的内容可以更改
  34.         @mail($admin_email,'MYSQL报告',$message,'from:mysql@admin.com');
  35.         @mail($email,'MYSQL大小警告',$message,'from:mysql@admin.com');
  36.         //echo 'warning.debug';exit;
  37. }


  38. //以下一段内容来自phpMyAdmin的查询每个数据库大小的程序
  39. $dbs     = mysql_list_dbs() ;
  40. while ($a_db = mysql_fetch_object($dbs)) {        //查询数据名,以后一段代码来自phpmyadmin
  41.         $dblist[] = $a_db->Database;
  42. } // end while
  43. mysql_free_result($dbs);
  44. $num_dbs = count($dblist);
  45.     $total_array[0] = 0;        // number of tables
  46.     $total_array[1] = 0;        // total data size
  47.     $total_array[2] = 0;        // total index size
  48.     $total_array[3] = 0;        // big total size

  49.     // Gets the tables stats per database
  50.     for ($i = 0; $i < $num_dbs; $i++) {
  51.         $db         = $dblist[$i];
  52.         $tables     = mysql_list_tables($db);

  53.         // Number of tables
  54.         if ($tables) {
  55.             $dbs_array[$db][0] = mysql_numrows($tables);
  56.             mysql_free_result($tables);
  57.         } else {
  58.             $dbs_array[$db][0] = 0;
  59.         }
  60.         $total_array[0]    += $dbs_array[$db][0];

  61.         // Size of data and indexes
  62.         $dbs_array[$db][1] = 0; // data size column
  63.         $dbs_array[$db][2] = 0; // index size column
  64.         $dbs_array[$db][3] = 0; // full size column

  65.         $local_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
  66.         //echo  $db;
  67.         $result      = @mysql_query($local_query);
  68.             // needs the "@" below otherwise, warnings in case of special DB names
  69.             if ($result ) {
  70.                     while ($row = mysql_fetch_array($result)) {
  71.                     $dbs_array[$db][1] += $row['Data_length'];
  72.                     $dbs_array[$db][2] += $row['Index_length'];
  73.                 }
  74.                 $dbs_array[$db][3]     = $dbs_array[$db][1] + $dbs_array[$db][2];
  75.                 $total_array[1]        += $dbs_array[$db][1];
  76.                 $total_array[2]        += $dbs_array[$db][2];
  77.                 $total_array[3]        += $dbs_array[$db][3];
  78.           //      echo $dbs_array[$db][3]."<br>";
  79.                 mysql_free_result($result);
  80.          } // end if
  81. } // end for
  82. //查询数据库完毕。
  83.    
  84.    
  85. //以下代码用来判断数据库大小是否达到限制等。。。。
  86. mysql_select_db('db_limit',$id);   //db_limit数据库名可自由更改。但需要与其它程序保持一致

  87. for ($i = 0; $i < $num_dbs; $i++) {
  88.         $db=$dblist[$i];
  89.         $query='select * from dbs where db_name="'.$db.'"';
  90.         $result=mysql_query($query);
  91.         if ($result)
  92.         {
  93.                 $dbs_limit=mysql_fetch_array($result);
  94.                 $limit_size=$dbs_limit['db_limit_size'];
  95.                 $user=$dbs_limit['db_user'];
  96.                 $limited=$dbs_limit['db_has_limit'];
  97.                 $used_size=$dbs_limit['db_size_used'];
  98.                 $email=$dbs_limit['db_email'];
  99.                 $warning_size=$limit_size*0.8;         //0.8=80%,你可以更改为其它比例
  100.                 if ($user!=''){                        //这
  101.                         if ($dbs_array[$db][3] > $limit_size) {limit($user,$db);$limited='1';}
  102.                         if ($dbs_array[$db][3] > $warning_size) warning($user,$email);
  103.                         $query='update dbs set db_has_limit="'.$limited.'" , db_size_used="'.$dbs_array[$db][3].'" where db_name="'.$db.'"';
  104.                         $result=mysql_query($query);         //更新数据库大小至数据资料库
  105.                 }
  106.                 //echo $query;exit;
  107.         }
  108. }
  109. mysql_close($id);
  110. //欢迎大家加以修改。但改完后。请到https://discuz.dismall.com/forumdisplay.php?fid=34发表修改结果
  111. ?>
复制代码

[ Last edited by BENDY on 2004-1-17 at 11:19 AM ]
BENDY 发表于 2004-1-8 19:10:30 | 显示全部楼层
第二部份为前台管理系统.
这只是最简单的作品.请大家尽快做一个更好的前台出来..

main.php

  1. <?
  2. //Bendy 的mysql限额前台程序
  3. $admin='';session_start();
  4. require ("./config.php");
  5. if ($adpass==$adminpass)
  6. {
  7.         $admin='ok';
  8.         session_register ("admin");$admin='ok';
  9. }       
  10. if ($admin=='')
  11. {
  12.         ?>
  13. <html>
  14. <head>
  15. <title>管理</title>
  16. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  17. </head>
  18. <body>
  19.         <form  method="post" action="main.php">
  20.         请先登陆,管理密码是:<input type='text' name="adpass">
  21.         <input type="submit" name="Submit" value="进入">
  22.         </form></body></html>
  23.         <?
  24.         exit;
  25. }
  26. ?>
  27. <frameset rows="15%,*" frameborder="NO" border="0" framespacing="0">
  28.   <frame src="./menu.html" name="topFrame" >
  29.   <frame src="./list.php" name="main">
  30. </frameset>
复制代码


add.php

  1. <?
  2. //Bendy 的mysql限额前台程序
  3. $admin='';session_start();
  4. require ("./config.php");
  5. if ($admin=='')
  6. {
  7.         echo '<a href="main.php">login first</a>';
  8.         exit;
  9. }
  10. if ($db_name!='')
  11. {
  12.         $id=mysql_connect("localhost",$user,$pass);
  13.         mysql_select_db('db_limit',$id);
  14.         $query="insert into dbs values ('$db_name','$db_user','$email','$db_limit','','')";
  15.         $result=mysql_query($query);
  16.         echo "insert success ";
  17.         mysql_close($id);
  18. }       
  19. ?>
  20. <html>
  21. <head>
  22. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  23. <title>添加用户到数据库</title>
  24. </head>
  25. <body>
  26. <p>添加用户到数据库</p>
  27. <form  method="post" action="add.php">
  28. 数据库名:<input type="text" name="db_name"><br>
  29. 用户名:<input type='text' name="db_user"><br>
  30. 限制的大小:<input type='text' name="db_limit">字节<BR>
  31. 通知的EMAIL:<input type='text' name="email"><BR>
  32. <input type="submit" name="Submit" value="添加">
  33. </form>
  34. </body>
  35. </html>
复制代码


config.php

  1. <?
  2. //Bendy 的mysql限额前台程序
  3. $user="root";       //该用户只需要对系统的db_limit数据库有访问权.不需要ROOT权
  4. $pass="123654";     //密码
  5. $adminpass="bendy"; //进入前台的管理密码
  6. ?>
复制代码
回复

使用道具 举报

BENDY 发表于 2004-1-8 19:12:09 | 显示全部楼层
list.php

  1. <?
  2. //Bendy 的mysql限额前台程序
  3. $admin='';session_start();
  4. require ("./config.php");
  5. if ($admin=='')
  6. {
  7.         echo '<a href="main.php">login first</a>';
  8.         exit;
  9. }
  10. $id=mysql_connect("localhost",$user,$pass);
  11. mysql_select_db('db_limit',$id);
  12. ?>
  13. <html>
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  16. <title>list</title>
  17. </head>
  18. <body>
  19. <table width="100%" border="2" >
  20.   <tr>
  21.     <td width="15%" height="21">数据库名</td>
  22.     <td width="15%">用户名</td>
  23.     <td width="15%">限额</td>
  24.     <td width="15%">已用</td>
  25.     <td width="10%">EMAIL</td>
  26.     <td width="10%">限制</td>
  27.     <td width="10%">修改</td>
  28.     <td width="10%">删除</td>
  29.   </tr>
  30. <?
  31. $query="select * from dbs";
  32. $result=mysql_query($query);
  33. while ($list=mysql_fetch_array($result))
  34. {
  35.         if ($list['db_has_limit']=='1')
  36.         {$limit='<a href=./fix.php?dbname='.$list[db_name].'><font color=red>是|重开</font></a>';} //change for fix.php
  37.         else
  38.         {$limit='否';}
  39.         echo "<tr><td>$list[db_name]</td><td>$list[db_user]</td><td>$list[db_limit_size]</td><td>$list[db_size_used]</td><td>$list[db_email]</td><td>$limit</td><td><a href=edit.php?dbname=$list[db_name]>修改</a></td><td><a href=del.php?dbname=$list[db_name]>删除</a></td><tr>";
  40. }
  41. ?>
  42. </table>
  43. </body>
  44. </html>
复制代码


edit.php

  1. <?
  2. //Bendy 的mysql限额前台程序
  3. $admin='';session_start();
  4. require ("./config.php");
  5. if ($admin=='')
  6. {
  7.         echo '<a href="main.php">login first</a>';
  8.         exit;
  9. }
  10. $id=mysql_connect("localhost",$user,$pass);
  11. mysql_select_db('db_limit',$id);
  12. if ($dbname=='')
  13. {
  14.         echo "edit what? goto <a href=list.php> list </a>";
  15.         exit;
  16. }
  17. if ($submit!='')
  18. {
  19.         $query="update dbs set db_user='$dbuser',db_limit_size='$limit',db_email='$email' ,db_has_limit='0' where db_name='$dbname'";
  20.         $result=mysql_query($query);
  21.         echo "edit success? goto <a href=list.php> list </a>";
  22.         exit;
  23. }
  24.        
  25. $query="select * from dbs where db_name='$dbname'";
  26. $result=mysql_query($query);
  27. $edit=mysql_fetch_array($result);
  28. ?>

  29. <form  method="post" action="edit.php">
  30. 数据库名:<?=$dbname?><br>
  31. 用户名:<input type='text' name="dbuser" value="<?=$edit[db_user]?> "><br>
  32. 限制的大小:<input type='text' name="limit" value="<?=$edit[db_limit_size]?>">字节<BR>
  33. 通知的EMAIL:<input type='text' name="email" value="<?=$edit[db_email]?> " ><BR>
  34. <input type='hidden' name="dbname" value="<?=$dbname?>">
  35. <input type="submit" name="submit" value="更改">
  36. </form>
  37. </body></html>
复制代码


del.php

  1. <?
  2. $admin='';session_start();
  3. require ("./config.php");
  4. if ($admin=='')
  5. {
  6.         echo '<a href="main.php">login first</a>';
  7.         exit;
  8. }
  9. $id=mysql_connect("localhost",$user,$pass);
  10. mysql_select_db('db_limit',$id);
  11. if ($dbname=='')
  12. {
  13.         header("location:list.php");
  14.         exit;
  15. }
  16. $query="delete from dbs where db_name='$dbname'";
  17. $result=mysql_query($query);
  18. echo "delete success ,goto <a href=list.php> list </a>";
  19. ?>
复制代码

[ Last edited by BENDY on 2004-1-17 at 11:26 AM ]
回复

使用道具 举报

BENDY 发表于 2004-1-8 19:14:59 | 显示全部楼层
还有!!!
在MYSQL建立一个
db_limit
的数据库.
导入以下内容

  1. # database : `db_limit`
  2. CREATE TABLE `dbs` (
  3.   `db_name` varchar(64) NOT NULL default '',
  4.   `db_user` varchar(64) NOT NULL default '',
  5.   `db_email` varchar(64) NOT NULL default '',
  6.   `db_limit_size` int(10) NOT NULL default '0',
  7.   `db_has_limit` int(10) NOT NULL default '0',
  8.   `db_size_used` int(10) NOT NULL default '0'
  9. ) TYPE=MyISAM;
复制代码



MENU.html

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  4. <title>menu</title>
  5. </head>

  6. <body>
  7. <table width="100%" border="5">
  8.   <tr>
  9.     <td><div align="center"><a href="./list.php" target="main">列表</a></div></td>
  10.     <td><div align="center"><a href="./add.php" target="main">添加</a></div></td>
  11.   </tr>
  12. </table>
  13. </body>
  14. </html>
复制代码

[ Last edited by zhnag on 2004-1-9 at 05:43 PM ]
回复

使用道具 举报

BENDY 发表于 2004-1-8 19:46:41 | 显示全部楼层
引用自uplinux 的
就算利用 php 来写检测,也仍然可以实现自动运行的。
crobtab

* 0 * * * /usr/lcoal/php4/bin/php  /home/master/mysql.php

将本系统的脚本放在一个安全的地方.(如果可以让网页访问.也可以,这样的好处是用户可以随时更新信息)

每天0点执行这个php脚本,PHP 文件中想怎么写就怎么写了。
回复

使用道具 举报

BENDY 发表于 2004-1-8 19:48:02 | 显示全部楼层
本人是没有必要限制客户的数据库大小.这程序只能用来给大家参考使用.
回复

使用道具 举报

soway 发表于 2004-1-8 20:12:47 | 显示全部楼层
呵呵,一定测试
多谢,看来是厉害啊,不知道效果如何
这个是一个功能,感觉可以这样来提供mysql服务,特别对于一些 虚拟主机提供商可能有用
回复

使用道具 举报

BENDY 发表于 2004-1-8 20:28:01 | 显示全部楼层
加一个程序

init.php快速建立数据库资料系统

  1. <?php
  2. //初始数据库系统,使用本程序。可以快速建立本地用户系统资料库。
  3. //暂时只能快速建立数据库名=用户名的系统。有意者。可以建立数据库名与用户名不同的资料库。。。。。

  4. $id=mysql_connect('localhost','user','password');   //最好是使用root,或者高权限用户
  5. $dbs     = mysql_list_dbs() ;
  6. while ($a_db = mysql_fetch_object($dbs)) {        //查询数据名,以后一段代码来自phpmyadmin
  7.         $dblist[] = $a_db->Database;
  8. } // end while
  9. mysql_free_result($dbs);
  10. $num_dbs = count($dblist);
  11. mysql_select_db('db_limit',$id);
  12. $init_size='100000000';                           //初始限制大小(字节数)
  13. for ($i = 0; $i < $num_dbs; $i++) {
  14.         $query="insert into dbs values ('$dblist[$i]','$dblist[$i]','','$init_size','','')";
  15.         $result=mysql_query($query);       
  16. }
  17. mysql_close($id);
  18. ?>
复制代码



补充一个....恢复用户权限..


  1. <?
  2. $admin='';session_start();
  3. require ("./config.php");
  4. if ($admin=='')
  5. {
  6.         echo '<a href="main.php">login first</a>';
  7.         exit;
  8. }
  9. $id=mysql_connect("localhost",'root','rootpass');   //这里需要ROOT权限!!!
  10. mysql_select_db('db_limit',$id);
  11. if ($dbname=='')
  12. {
  13.         header("location:list.php");
  14.         exit;
  15. }
  16. $query="select * from dbs where db_name='$dbname'";
  17. $result=mysql_query($query);
  18. $fix=mysql_fetch_array($result);
  19. $query='GRANT INSERT ,UPDATE ,CREATE  ON "'.$fix[db_name].'".* TO "'.$fix[db_name].'@localhost';
  20. $result=mysql_query($query);
  21. $query="update dbs set db_has_limit=0 where db_name=$fix[db_name]";
  22. $result=mysql_query($query);
  23. echo "fix success ,but you must change the limit , If not ,system will limit it again! goto <a href=list.php> list </a>";
  24. ?>
复制代码

[ Last edited by BENDY on 2004-1-17 at 11:33 AM ]
回复

使用道具 举报

BENDY 发表于 2004-1-8 20:29:00 | 显示全部楼层
忘记想恢复程序了...嘻...一会再写
回复

使用道具 举报

UP.Linux 发表于 2004-1-8 23:07:31 | 显示全部楼层
哈哈,支持支持支持~~~~

被你抢先了,哈,一定测试~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2024-9-29 19:25 , Processed in 0.152556 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表