Linux系統下MySQL使用者限額的實現

來源:互聯網
上載者:User

原理:
由於Linux系統下MySQL每個使用者的資料庫都是以目錄的形式存在於/var/db/mysql或者其他目錄下。

一個目錄就是一個資料庫。所以可以通過檢測目錄的大小得到資料庫大小,判斷容量是否超過限額,如果超過限額就取消該使用者的insert,create,update,create temp table許可權,允許select,delete等其他許可權。

實現的指令碼如下:
su-2.05b# cat quotamysql.sh
#!/bin/sh
#hmy-2004-8-19 v-0.1

mysqldir=/usr/db/mysql
infofiledir=/root/hmywork
limitfile=limitmysql
userfile=user_mysql
#定義初始變數
while [ 1 ];
do
sleep 10
#每隔10秒檢查一次

for i in `cat ${infofiledir}/${userfile}`
do
now=`du ${mysqldir}/$i |tail -n 1 |awk '{print $1}'`
#取得目前的目錄大小
limit=`grep $i ${infofiledir}/${limitfile}|awk '{print $2}'`
#取得限額大小
if [ "$now" -gt "$limit" ];
#如果大於限額就執行下面的mysql語句
then
mysql --user=MySql@AdminU_ser --password=***********< use mysql
update db set insert_priv='N' ,update_priv='N', create_priv='N',Create_tmp_table_priv='N' where db='$i';
flush privileges ;
EOF
echo $i database full! `date`>;>;fulluser
sed /`echo $i`/d $userfile >;tempfile
fi
done
cp tempfile $userfile

done

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.