Skip to content

Instantly share code, notes, and snippets.

@yesnault
Created July 23, 2013 19:49
Show Gist options
  • Save yesnault/6065558 to your computer and use it in GitHub Desktop.
Save yesnault/6065558 to your computer and use it in GitHub Desktop.
Usage : createUserAndImportDumpMysql.sh ./dumpToImport.sql
#!/bin/bash
###############
# use this script at your own risk (usefull for developpers only)
# usage : createUserAndImportDumpMysql.sh ./dumpToImport.sql
#
# drop the user IDBASE
# drop the database IDBASE
# create the user IDBASE with password IDBASE and all rights on IDBASE database
# create database IDBASE
# import DUMP
###############
IDBASE=myapplication
LOGIN_MYSQL=root
PASS_MYSQL=password
MYSQL_="/usr/local/mysql/bin/mysql"
CMD="$MYSQL_ -u $LOGIN_MYSQL -p$PASS_MYSQL ";
if [ $# -ne 1 ]
then
echo "Usage $0 dumToImport.sql"
exit 1;
fi
FIC_DUMP=$1
echo "Source file : $FIC_DUMP"
echo "Deleting user $IDBASE if exists";
echo "DROP USER '$IDBASE'@'localhost';" | $CMD 2>&1 > /dev/null
echo "Drop database $IDBASE if exists";
echo "DROP DATABASE IF EXISTS $IDBASE ;" | $CMD
echo "Create USER $IDBASE";
echo "CREATE USER '$IDBASE'@'localhost' IDENTIFIED BY '$IDBASE'; " | $CMD
if [ $? -ne 0 ]
then
echo "Error while create user $IDBASE";
exit 1;
fi
echo "GRANT USAGE ON * . * TO '$IDBASE'@'localhost' IDENTIFIED BY '$IDBASE' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;" | $CMD;
echo "CREATE DATABASE IF NOT EXISTS $IDBASE " | $CMD;
echo "GRANT ALL PRIVILEGES ON $IDBASE . * TO '$IDBASE'@'localhost';" | $CMD;
echo "Import dump... "
$MYSQL_ -u $IDBASE -p$IDBASE $IDBASE < $FIC_DUMP
if [ $? -ne 0 ]
then
echo "Error while import"
exit 1;
fi
echo "End Import"
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment