Browse Source

Create Separate Database User Per Site

old-stable
Mitesh Shah 11 years ago
parent
commit
c725715a88
  1. 10
      etc/easyengine/ee.conf
  2. 55
      usr/local/sbin/easyengine

10
etc/easyengine/ee.conf

@ -11,19 +11,19 @@ htpasswduser =
htpasswdpass = htpasswdpass =
# WordPress Defaults # WordPress Defaults
# MySQL Default Host: localhost
mysqlhost =
# Custom Database Name # Custom Database Name
customdbname = false customdbname = false
# Custom Database User
# MySQL Default Host: localhost customdbuser = false
mysqlhost =
# WordPress Database Table Prefix Default: wp_ # WordPress Database Table Prefix Default: wp_
wpdbprefix = wpdbprefix =
# WordPress Default User: admin # WordPress Default User: admin
wpadminuser = wpadminuser =
# WordPress Default Password: Random Password # WordPress Default Password: Random Password
wpadminpass = wpadminpass =
# WordPress Default Email: `git config user.email` # WordPress Default Email: `git config user.email`
wpadminemail = wpadminemail =

55
usr/local/sbin/easyengine

@ -525,7 +525,11 @@ MYSQLPASSCHECK()
done done
} }
MYSQLROOT()
{
MYSQLUSER=$(cat ~/.my.cnf | grep user | cut -d'=' -f2)
MYSQLPASS=$(cat ~/.my.cnf | grep pass | cut -d'=' -f2)
}
# GET Information Function # GET Information Function
MYSQLINFO() MYSQLINFO()
{ {
@ -540,8 +544,7 @@ MYSQLINFO()
if [ -f ~/.my.cnf ] if [ -f ~/.my.cnf ]
then then
MYSQLUSER=$(cat ~/.my.cnf | grep user | cut -d'=' -f2) MYSQLROOT
MYSQLPASS=$(cat ~/.my.cnf | grep pass | cut -d'=' -f2)
else else
# Verify MySQL Credentials # Verify MySQL Credentials
MYSQLUSERPASS MYSQLUSERPASS
@ -593,6 +596,32 @@ MYSQLINFO()
# Create Database # Create Database
mysql -h $MYSQLHOST -u $MYSQLUSER -p$MYSQLPASS -e "create database \`$WPDBNAME\`" \ mysql -h $MYSQLHOST -u $MYSQLUSER -p$MYSQLPASS -e "create database \`$WPDBNAME\`" \
|| OwnError "Unable To Create $WPDBNAME Database" || OwnError "Unable To Create $WPDBNAME Database"
# Check Use Default DB User Or Custom DB User
WPDBRANDOMPASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 15 | head -n1)
grep customdbuser /etc/easyengine/ee.conf | grep -i true &>> /dev/null
if [ $? -eq 0 ]
then
read -p "Enter The MySQL Database Username [$REPLACEDOT]: " WPDBUSER
read -p "Enter The MySQL Database Password [$WPDBRANDOMPASS]: " WPDBPASS
fi
if [[ $WPDBUSER = "" ]]
then
WPDBUSER=$REPLACEDOT
fi
if [[ $WPDBPASS = "" ]]
then
WPDBPASS=$WPDBRANDOMPASS
fi
# Create Separate User & Grant Permission
echo $WPDBNAME $WPDBUSER $WPDBPASS
mysql -h $MYSQLHOST -u $MYSQLUSER -p$MYSQLPASS -e "create user '$WPDBUSER'@'$MYSQLHOST' identified by '$WPDBPASS'"
mysql -h $MYSQLHOST -u $MYSQLUSER -p$MYSQLPASS -e "grant all privileges on \`$WPDBNAME\`.* to '$WPDBUSER'@'$MYSQLHOST'"
mysql -h $MYSQLHOST -u $MYSQLUSER -p$MYSQLPASS -e "flush privileges"
} }
PHPUSERINFO() PHPUSERINFO()
@ -814,10 +843,10 @@ EEWPSETUP()
sed -i "s/database_name_here/$WPDBNAME/" \ sed -i "s/database_name_here/$WPDBNAME/" \
/var/www/$DOMAIN/wp-config.php /var/www/$DOMAIN/wp-config.php
sed -i "s/username_here/$MYSQLUSER/" \ sed -i "s/username_here/$WPDBUSER/" \
/var/www/$DOMAIN/wp-config.php /var/www/$DOMAIN/wp-config.php
sed -i "s/password_here/$MYSQLPASS/" \ sed -i "s/password_here/$WPDBPASS/" \
/var/www/$DOMAIN/wp-config.php /var/www/$DOMAIN/wp-config.php
sed -i "s/localhost/$MYSQLHOST/" \ sed -i "s/localhost/$MYSQLHOST/" \
@ -994,10 +1023,12 @@ REMOVEDB()
if [ $? -ne 0 ] if [ $? -ne 0 ]
then then
# MySQL Information # MySQL Information
MYSQLROOT
WPDBNAME=$(grep DB_NAME /var/www/$DOMAIN/wp-config.php | cut -d"'" -f4) WPDBNAME=$(grep DB_NAME /var/www/$DOMAIN/wp-config.php | cut -d"'" -f4)
MYSQLUSER=$(grep DB_USER /var/www/$DOMAIN/wp-config.php | cut -d"'" -f4) WPDBUSER=$(grep DB_USER /var/www/$DOMAIN/wp-config.php | cut -d"'" -f4)
MYSQLPASS=$(grep DB_PASS /var/www/$DOMAIN/wp-config.php | cut -d"'" -f4) WPDBPASS=$(grep DB_PASS /var/www/$DOMAIN/wp-config.php | cut -d"'" -f4)
echo -e " WPDBNAME = $WPDBNAME \n MYSQLUSER = $MYSQLUSER" MYSQLHOST=$(grep DB_HOST /var/www/$DOMAIN/wp-config.php | cut -d"'" -f4)
echo -e " WPDBNAME = $WPDBNAME \n WPDBUSER = $WPDBUSER \n MYSQLHOST = $MYSQLHOST"
# Remove Database # Remove Database
# For Proper read Command Output # For Proper read Command Output
@ -1007,8 +1038,14 @@ REMOVEDB()
if [ "$ANSWER" = "y" ] if [ "$ANSWER" = "y" ]
then then
# Remove Database # Remove Database
mysql -u $MYSQLUSER -p$MYSQLPASS -e "drop database \`$WPDBNAME\`" \ mysql -h $MYSQLHOST -u $MYSQLUSER -p$MYSQLPASS -e "drop database \`$WPDBNAME\`" \
|| OwnError "Unable To Drop $WPDBNAME Database" || OwnError "Unable To Drop $WPDBNAME Database"
# Remove Database User
mysql -h $MYSQLHOST -u $MYSQLUSER -p$MYSQLPASS -e "drop user '$WPDBUSER'@'$MYSQLHOST'" \
|| OwnError "Unable To Drop Database User $WPDBUSER"
mysql -h $MYSQLHOST -u $MYSQLUSER -p$MYSQLPASS -e "flush privileges" \
|| OwnError "Unable To Flush MySQL Privileges"
else else
# User Denied Messages # User Denied Messages
echo -e "\033[31mUser Denied To Drop $WPDBNAME Database.\e[0m" echo -e "\033[31mUser Denied To Drop $WPDBNAME Database.\e[0m"

Loading…
Cancel
Save