MySQL Quotas


 
Thread Tools Search this Thread
Operating Systems Linux MySQL Quotas
# 1  
Old 01-24-2008
Question MySQL Quotas

How do I limit the maximum usage space for a specific MySQL user ??

Databases can be unlimited but the total sum of data on all tables and databases shouldn't exceed a limit I want. How to do this ??
# 2  
Old 01-25-2008
I found a php script on the net, I'm not exactly sure whether it's working correctly, since I don't know php, but I hope this is what you need.
Code:
#!/usr/bin/php -q
<?PHP

/*
 * MySQL quota script
 * written by Sebastian Marsching
 *
 */

/*
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


/*
 * Create table for quota data with the following statement:
 *
 * CREATE TABLE `Quota` (`Db` CHAR(64) NOT NULL, 
 * `Limit` BIGINT NOT NULL,
 * `Exceeded` ENUM('Y','N') DEFAULT 'N' NOT NULL,
 * PRIMARY KEY (`Db`), UNIQUE (`Db`));
 *
 * The field 'db' stores the information for which database
 * you want to limit the size.
 * The field 'limit' is the size limit in bytes.
 * The field 'exceeded' is only used internally and must be
 * initialized with 'N'.
 */
 
/*
 * Settings
 */
 
$mysql_host  = 'localhost';
$mysql_user  = 'root'; // Do NOT change, root-access is required
$mysql_pass  = '';
$mysql_db    = 'quotadb'; // Not the DB to check, but the db with the quota table
$mysql_table = 'quota';

/*
 * Do NOT change anything below
 */
 
$debug = 0;

// Connect to MySQL Server

if (!mysql_connect($mysql_host, $mysql_user, $mysql_pass))
{
 echo "Connection to MySQL-server failed!";
 exit;
}

// Select database

if (!mysql_select_db($mysql_db))
{
 echo "Selection of database $mysql_db failed!";
 exit;
}

// Check quota for each entry in quota table

$sql = "SELECT * FROM $mysql_table;";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result))
{
 $quota_db = $row['db'];
 $quota_limit = $row['limit'];
 $quota_exceeded = ($row['exceeded']=='Y') ? 1 : 0;
 
 if ($debug)
  echo "Checking quota for '$quota_db'...\n";
 
 $qsql = "SHOW TABLE STATUS FROM $quota_db;";
 $qresult = mysql_query($qsql);
 
 if ($debug)
  echo "SQL-query is \"$qsql\"\n";
 
 $quota_size = 0;
 
 while ($qrow = mysql_fetch_array($qresult))
 {
  if ($debug)
  { echo "Result of query:\n"; var_dump($qrow); }
  $quota_size += $qrow['Data_length'] + $qrow['Index_length'];
 }
 
 if ($debug)
  echo "Size is $quota_size bytes, limit is $quota_limit bytes\n";
 
 if ($debug && $quota_exceeded)
  echo "Quota is marked as exceeded.\n";
 if ($debug && !$quota_exceeded)
  echo "Quota is not marked as exceeded.\n";
 
 if (($quota_size > $quota_limit) && !$quota_exceeded)
 {
  if ($debug)
   echo "Locking database...\n";
  // Save in quota table  
  $usql = "UPDATE $mysql_table SET exceeded='Y' WHERE db='$quota_db';";
  mysql_query($usql);
  if ($debug)
   echo "Querying: $usql\n";
  // Dismiss CREATE and INSERT privilege for database
  mysql_select_db('mysql');
  $usql = "UPDATE db SET Insert_priv='N', Create_priv='N' WHERE Db='$quota_db';";
  mysql_query($usql);
  if ($debug)
   echo "Querying: $usql\n";
  mysql_select_db($mysql_db);
 }
 
 if (($quota_size <= $quota_limit) && $quota_exceeded) 
 {
  if ($debug)
   echo "Unlocking database...\n";
  // Save in quota table
  $usql = "UPDATE $mysql_table SET exceeded='N' WHERE db='$quota_db';";
  mysql_query($usql);
  if ($debug)
   echo "Querying: $usql\n";
  // Grant CREATE and INSERT privilege for database
  mysql_select_db('mysql');
  $usql = "UPDATE db SET Insert_priv='Y', Create_priv='Y' WHERE Db='$quota_db';";
  mysql_query($usql);
  if ($debug)
   echo "Querying: $usql\n";
  mysql_select_db($mysql_db);
 }
}

?>

# 3  
Old 01-26-2008
I knew this one. Smilie I want system based like Linux has edquota.

I have got the solution to this:

setup system quotas for the group in which the user is, then create a separate databases directory in his home directory with permission mysql:usergroup and permission 0755

Then move the databases to that directory and create symbolic links to the original mysql datadir.

Last edited by Nilesh_lf; 01-26-2008 at 12:31 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Disk Quotas

Hi, I'm practicing new things with Linux/Unix and I need someone to point me at the right direction. Lets say I create a user named user1. After creating it, how would I enable quotas for it. I have already added "userquota" under "/etc/fstab" and rebooted my system, but after I run the command... (1 Reply)
Discussion started by: abhi7514
1 Replies

2. Solaris

quotas on two file system

Hi Guys, I have quota support turned on, on two file systems. However, when I do a repquota -va I get report only for one. What might be the problem? I will really appreciate your help. Thanks Gurus. (0 Replies)
Discussion started by: cjashu
0 Replies

3. Solaris

Quotas on /export/home

Hello all, I am trying to set quotas on /export/home filesystem for some of our users on a Solaris Zone I know that you would be redirecting me to some of the documentation pages, but I have already done that. The /export/home on the Zone is a Veritas FS and I cannot see an entry for... (4 Replies)
Discussion started by: grajp002
4 Replies

4. Ubuntu

Download quotas for users

Hi peeps, I have a teenage lad who needs to learn some discipline with internet access etc. etc. My main problem is that he is downloading so much that he is using more than his fair share. What I'd like to do is set a download quota for him so that he can learn what this all means. Can... (10 Replies)
Discussion started by: greadey
10 Replies

5. Filesystems, Disks and Memory

auto quotas?

Heres a stupid question to all you Linux gods/goddesses. Is there any way to have the system automatically set user quotas when a user account is created or does it have to be done by hand or a script. Every article, thread, anything Ive read so far only shows how to set quotas manually. Any... (3 Replies)
Discussion started by: mcady_02
3 Replies

6. UNIX for Dummies Questions & Answers

AIX 5.3 quotas

Hello, I will be creating several users on a new AIX box. I would like to limit the amount of data they can keep. How can I do that in smit or with a command. Thanks - Brad (0 Replies)
Discussion started by: rondebbs
0 Replies

7. Solaris

Quotas and NIS

Has anyone ever used disk quotas with NIS? I tried to implement it the normal way but since the users only exist in NIS and not in the local passwd file when i try the edquota command it cannot find the user. Thanks (4 Replies)
Discussion started by: meyersp
4 Replies

8. Shell Programming and Scripting

Need help!! script for quotas

here is the file i must use to write my script (from a repquota -g) : Group used soft grace ... group1 -- 270000 0 0 ... group2 -- 1500005 0 0 ... group3 -- 55 0 0 ... ... ... ... ... the script has to... (2 Replies)
Discussion started by: tomapam
2 Replies

9. Filesystems, Disks and Memory

setting quotas

Hi, I'm trying to setup user quotas on my Linux system. It is Suse 7.3 running the standard kernel from that release. When I try to run quotaon, I get the following: # quotaon -v /dev/hdc1 quotaon: using /home/aquota.user on /dev/hdc1: Invalid argument I can't figure out what it means by... (1 Reply)
Discussion started by: cybler
1 Replies

10. UNIX for Advanced & Expert Users

disk quotas in UNIX?

RedHat 7.0 Caldera 2.4 From Windows 2000 server, you can allow ceratin users to use only so much disk space when they login. I'm sure you can do that in Linux/UNIX, but I just don't know how or what you call them here (disk quotas maybe?). Anyway, any kind help is always greatly appreciated.... (1 Reply)
Discussion started by: zorro81
1 Replies
Login or Register to Ask a Question