終于做好了.歡迎大家評一評!<mysql數據庫大小的限制>
使用PHP實現的程序.花了一個下午去完成...
只是做出來讓大家參考一下...獻丑了....
程序思路:
一\與MYSQL數據庫結合.先在MYSQL數據庫另起一個庫.記錄數據庫的庫名,對應的用戶名,限制的大小.等.....
二\系統檢測數據庫大小,然后對比記錄著的資料.對比是否超過流量.如果超過流量就使用MYSQL的ROOT權.限制用戶對該數據庫的權限...(刪除UPDATE\INST..等)
三\如果達到80%.就向管理員\用戶各發送一個EMAIL通知..
四\前臺程序控制數據庫資料的整理...
系統分二個部份.
第一部份.是系統定時檢測數據庫大小,再根據檢測結果與數據庫資料.判斷數據庫是否超大....該部份操作需要有MYSQL高權限用戶去完成(建議ROOT).用該文件需要定時運行.,但該文件可以放在網站訪問不到的保密地方...
所有文件.打包下載.
http://www.xingkong.biz/mysql_limit.zip CODE: [Copy to clipboard] clearcase/" target="_blank" >cc3" border="0">
<?php
//設置部分
$id=mysql_connect('localhost','user','password'); //最好是使用root,或者高權限用戶
//FUN部份
function PMA_backquote($a_name, $do_it = TRUE) // 取自phpmyadmin,用來格式化數據庫名
{
if ($do_it
&& !empty($a_name) && $a_name != '*') {
if (is_array($a_name)) {
$result = array();
reset($a_name);
while(list($key, $val) = each($a_name)) {
$result[$key] = '`' . $val . '`';
}
return $result;
} else {
return '`' . $a_name . '`';
}
} else {
return $a_name;
}
} // end of the 'PMA_backquote()' function
function limit($user,$db) //達到限制限制用戶權限后運行的程序
{
$query='REVOKE INSERT ,UPDATE ,CREATE ON "'.$db.'".* FROM "'.$user.'@localhost';//將insert update create的權限移走。01/17修正大BUG
$result = @mysql_query($query);//changed! only 1 query....
//相應的權限代碼可以再更改.
//echo 'lim.debug';exit;
}
function warning($name,$email) //超過80%時通知用戶
{
$admin_email='admin@admin.com'; //管理員EMAIL地址。請更改
$message='MYSQL用戶:'.$name.'的數據庫已超過系統允許的大小的80% /n 請及時整理數據'; //通知的內容可以更改
@mail($admin_email,'MYSQL報告',$message,'from:mysql@admin.com');
@mail($email,'MYSQL大小警告',$message,'from:mysql@admin.com');
//echo 'warning.debug';exit;
}
//以下一段內容來自phpMyAdmin的查詢每個數據庫大小的程序
$dbs = mysql_list_dbs() ;
while ($a_db = mysql_fetch_object($dbs)) { //查詢數據名,以后一段代碼來自phpmyadmin
$dblist[] = $a_db->Database;
} // end while
mysql_free_result($dbs);
$num_dbs = count($dblist);
$total_array[0] = 0; // number of tables
$total_array[1] = 0; // total data size
$total_array[2] = 0; // total index size
$total_array[3] = 0; // big total size
// Gets the tables stats per database
for ($i = 0; $i < $num_dbs; $i++) {
$db = $dblist[$i];
$tables = mysql_list_tables($db);
// Number of tables
if ($tables) {
$dbs_array[$db][0] = mysql_numrows($tables);
mysql_free_result($tables);
} else {
$dbs_array[$db][0] = 0;
}
$total_array[0] += $dbs_array[$db][0];
// Size of data and indexes
$dbs_array[$db][1] = 0; // data size column
$dbs_array[$db][2] = 0; // index size column
$dbs_array[$db][3] = 0; // full size column
$local_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
//echo $db;
$result = @mysql_query($local_query);
// needs the "@" below otherwise, warnings in case of special DB names
if ($result ) {
while ($row = mysql_fetch_array($result)) {
$dbs_array[$db][1] += $row['Data_length'];
$dbs_array[$db][2] += $row['Index_length'];
}
$dbs_array[$db][3] = $dbs_array[$db][1] + $dbs_array[$db][2];
$total_array[1] += $dbs_array[$db][1];
$total_array[2] += $dbs_array[$db][2];
$total_array[3] += $dbs_array[$db][3];
// echo $dbs_array[$db][3]."<br>";
mysql_free_result($result);
} // end if
} // end for
//查詢數據庫完畢。
//以下代碼用來判斷數據庫大小是否達到限制等。。。。
mysql_select_db('db_limit',$id); //db_limit數據庫名可自由更改。但需要與其它程序保持一致
for ($i = 0; $i < $num_dbs; $i++) {
$db=$dblist[$i];
$query='select * from dbs where db_name="'.$db.'"';
$result=mysql_query($query);
if ($result)
{
$dbs_limit=mysql_fetch_array($result);
$limit_size=$dbs_limit['db_limit_size'];
$user=$dbs_limit['db_user'];
$limited=$dbs_limit['db_has_limit'];
$used_size=$dbs_limit['db_size_used'];
$email=$dbs_limit['db_email'];
$warning_size=$limit_size*0.8; //0.8=80%,你可以更改為其它比例
if ($user!=''){ //這
if ($dbs_array[$db][3] > $limit_size)
if ($dbs_array[$db][3] > $warning_size) warning($user,$email);
$query='update dbs set db_has_limit="'.$limited.'" , db_size_used="'.$dbs_array[$db][3].'" where db_name="'.$db.'"';
$result=mysql_query($query); //更新數據庫大小至數據資料庫
}
//echo $query;exit;
}
}
mysql_close($id);
//歡迎大家加以修改。但改完后。請到http://www.discuz.net/forumdisplay.php?fid=34發表修改結果
?>
這只是最簡單的作品.請大家盡快做一個更好的前臺出來..
main.php CODE: [Copy to clipboard] <?
//Bendy 的mysql限額前臺程序
$admin='';session_start();
require ("./config.php");
if ($adpass==$adminpass)
{
$admin='ok';
session_register ("admin");$admin='ok';
}
if ($admin=='')
{
?>
<html>
<head>
<title>管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form method="post" action="main.php">
請先登陸,管理密碼是:<input type='text' name="adpass">
<input type="submit" name="Submit" value="進入">
</form></body></html>
<?
exit;
}
?>
<frameset rows="15%,*" frameborder="NO" border="0" framespacing="0">
<frame src="./menu.html" name="topFrame" >
<frame src="./list.php" name="main">
</frameset>
add.php CODE: [Copy to clipboard] <?
//Bendy 的mysql限額前臺程序
$admin='';session_start();
require ("./config.php");
if ($admin=='')
{
echo '<a href="main.php">login first</a>';
exit;
}
if ($db_name!='')
{
$id=mysql_connect("localhost",$user,$pass);
mysql_select_db('db_limit',$id);
$query="insert into dbs values ('$db_name','$db_user','$email','$db_limit','','')";
$result=mysql_query($query);
echo "insert success ";
mysql_close($id);
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>添加用戶到數據庫</title>
</head>
<body>
<p>添加用戶到數據庫</p>
<form method="post" action="add.php">
數據庫名:<input type="text" name="db_name"><br>
用戶名:<input type='text' name="db_user"><br>
限制的大小:<input type='text' name="db_limit">字節<BR>
通知的EMAIL:<input type='text' name="email"><BR>
<input type="submit" name="Submit" value="添加">
</form>
</body>
</html>
config.php CODE: [Copy to clipboard] <?
//Bendy 的mysql限額前臺程序
$user="root"; //該用戶只需要對系統的db_limit數據庫有訪問權.不需要ROOT權
$pass="123654"; //密碼
$adminpass="bendy"; //進入前臺的管理密碼
?> CODE: [Copy to clipboard] <?
//Bendy 的mysql限額前臺程序
$admin='';session_start();
require ("./config.php");
if ($admin=='')
{
echo '<a href="main.php">login first</a>';
exit;
}
$id=mysql_connect("localhost",$user,$pass);
mysql_select_db('db_limit',$id);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>list</title>
</head>
<body>
<table width="100%" border="2" >
<tr>
<td width="15%" height="21">數據庫名</td>
<td width="15%">用戶名</td>
<td width="15%">限額</td>
<td width="15%">已用</td>
<td width="10%">EMAIL</td>
<td width="10%">限制</td>
<td width="10%">修改</td>
<td width="10%">刪除</td>
</tr>
<?
$query="select * from dbs";
$result=mysql_query($query);
while ($list=mysql_fetch_array($result))
{
if ($list['db_has_limit']=='1')
{$limit='<a href=./fix.php?dbname='.$list[db_name].'><font color=red>是|重開</font></a>';} //change for fix.php
else
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>";
}
?>
</table>
</body>
</html>
edit.php CODE: [Copy to clipboard] <?
//Bendy 的mysql限額前臺程序
$admin='';session_start();
require ("./config.php");
if ($admin=='')
{
echo '<a href="main.php">login first</a>';
exit;
}
$id=mysql_connect("localhost",$user,$pass);
mysql_select_db('db_limit',$id);
if ($dbname=='')
{
echo "edit what? goto <a href=list.php> list </a>";
exit;
}
if ($submit!='')
{
$query="update dbs set db_user='$dbuser',db_limit_size='$limit',db_email='$email' ,db_has_limit='0' where db_name='$dbname'";
$result=mysql_query($query);
echo "edit success? goto <a href=list.php> list </a>";
exit;
}
$query="select * from dbs where db_name='$dbname'";
$result=mysql_query($query);
$edit=mysql_fetch_array($result);
?>
<form method="post" action="edit.php">
數據庫名:<?=$dbname?><br>
用戶名:<input type='text' name="dbuser" value="<?=$edit[db_user]?> "><br>
限制的大小:<input type='text' name="limit" value="<?=$edit[db_limit_size]?>">字節<BR>
通知的EMAIL:<input type='text' name="email" value="<?=$edit[db_email]?> " ><BR>
<input type='hidden' name="dbname" value="<?=$dbname?>">
<input type="submit" name="submit" value="更改">
</form>
</body></html>
del.php CODE: [Copy to clipboard] <?
$admin='';session_start();
require ("./config.php");
if ($admin=='')
{
echo '<a href="main.php">login first</a>';
exit;
}
$id=mysql_connect("localhost",$user,$pass);
mysql_select_db('db_limit',$id);
if ($dbname=='')
{
header("location:list.php");
exit;
}
$query="delete from dbs where db_name='$dbname'";
$result=mysql_query($query);
echo "delete success ,goto <a href=list.php> list </a>";
?>
在MYSQL建立一個
db_limit
的數據庫.
導入以下內容 CODE: [Copy to clipboard] # database : `db_limit`
CREATE TABLE `dbs` (
`db_name` varchar(64) NOT NULL default '',
`db_user` varchar(64) NOT NULL default '',
`db_email` varchar(64) NOT NULL default '',
`db_limit_size` int(10) NOT NULL default '0',
`db_has_limit` int(10) NOT NULL default '0',
`db_size_used` int(10) NOT NULL default '0'
) TYPE=MyISAM;
MENU.html CODE: [Copy to clipboard] <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>menu</title>
</head>
<body>
<table width="100%" border="5">
<tr>
<td><div align="center"><a href="./list.php" target="main">列表</a></div></td>
<td><div align="center"><a href="./add.php" target="main">添加</a></div></td>
</tr>
</table>
</body>
</html> QUOTE: 引用自uplinux 的
就算利用 php 來寫檢測,也仍然可以實現自動運行的。
crobtab
* 0 * * * /usr/lcoal/php4/bin/php /home/master/mysql.php
將本系統的腳本放在一個安全的地方.(如果可以讓網頁訪問.也可以,這樣的好處是用戶可以隨時更新信息)
每天0點執行這個php腳本,PHP 文件中想怎么寫就怎么寫了。
init.php快速建立數據庫資料系統 CODE: [Copy to clipboard] <?php
//初始數據庫系統,使用本程序??梢钥焖俳⒈镜赜脩粝到y資料庫。
//暫時只能快速建立數據庫名=用戶名的系統。有意者??梢越祿烀c用戶名不同的資料庫。。。。。
$id=mysql_connect('localhost','user','password'); //最好是使用root,或者高權限用戶
$dbs = mysql_list_dbs() ;
while ($a_db = mysql_fetch_object($dbs)) { //查詢數據名,以后一段代碼來自phpmyadmin
$dblist[] = $a_db->Database;
} // end while
mysql_free_result($dbs);
$num_dbs = count($dblist);
mysql_select_db('db_limit',$id);
$init_size='100000000'; //初始限制大?。ㄗ止潝担?br />for ($i = 0; $i < $num_dbs; $i++) {
$query="insert into dbs values ('$dblist[$i]','$dblist[$i]','','$init_size','','')";
$result=mysql_query($query);
}
mysql_close($id);
?>
補充一個....恢復用戶權限.. CODE: [Copy to clipboard] <?
$admin='';session_start();
require ("./config.php");
if ($admin=='')
{
echo '<a href="main.php">login first</a>';
exit;
}
$id=mysql_connect("localhost",'root','rootpass'); //這里需要ROOT權限?。?!
mysql_select_db('db_limit',$id);
if ($dbname=='')
{
header("location:list.php");
exit;
}
$query="select * from dbs where db_name='$dbname'";
$result=mysql_query($query);
$fix=mysql_fetch_array($result);
$query='GRANT INSERT ,UPDATE ,CREATE ON "'.$fix[db_name].'".* TO "'.$fix[db_name].'@localhost';
$result=mysql_query($query);
$query="update dbs set db_has_limit=0 where db_name=$fix[db_name]";
$result=mysql_query($query);
echo "fix success ,but you must change the limit , If not ,system will limit it again! goto <a href=list.php> list </a>";
?>Originally posted by zhnag at 2004-1-9 05:46 PM:
http://www.xingkong.biz/mysql_limit.zip