-
-
Save pkvip9999/b5baedf124413680b3b734c3cd302d69 to your computer and use it in GitHub Desktop.
Script create db, user and grant permission
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# sudo ./mysql-db-create.sh dbname dbuser password | |
# Functions | |
ok() { echo -e '\e[32m'$1'\e[m'; } # Green | |
EXPECTED_ARGS=3 | |
E_BADARGS=65 | |
MYSQL=`which mysql` | |
Q1="CREATE DATABASE IF NOT EXISTS $1;" | |
Q2="GRANT ALL ON *.* TO '$2'@'localhost' IDENTIFIED BY '$3';" | |
Q3="FLUSH PRIVILEGES;" | |
SQL="${Q1}${Q2}${Q3}" | |
if [ $# -ne $EXPECTED_ARGS ] | |
then | |
echo "Usage: $0 dbname dbuser dbpass" | |
exit $E_BADARGS | |
fi | |
$MYSQL -uroot -p -e "$SQL" | |
ok "Database $1 and user $2 created with a password $3" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Functions | |
ok() { echo -e '\e[32m'$1'\e[m'; } # Green | |
EXPECTED_ARGS=3 | |
E_BADARGS=65 | |
MYSQL=`which mysql` | |
Q1="CREATE DATABASE IF NOT EXISTS $1;" | |
Q2="CREATE USER IF NOT EXISTS '$2'@'localhost' IDENTIFIED WITH mysql_native_password BY '$3';GRANT ALL PRIVILEGES ON $1.* TO '$2'@'localhost' WITH GRANT OPTION;" | |
Q3="FLUSH PRIVILEGES;" | |
SQL="${Q1}${Q2}${Q3}" | |
if [ $# -ne $EXPECTED_ARGS ] | |
then | |
echo "Usage: $0 dbname dbuser dbpass" | |
exit $E_BADARGS | |
fi | |
$MYSQL -uroot -p -e "$SQL" | |
ok "Database $1 and user $2 created with a password $3" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# dump-tables-mysql.sh | |
# Descr: Dump MySQL table data into separate SQL files for a specified database. | |
# Usage: Run without args for usage info. | |
# Author: @Trutane | |
# Ref: http://stackoverflow.com/q/3669121/138325 | |
# Notes: | |
# * Script will prompt for password for db access. | |
# * Output files are compressed and saved in the current working dir, unless DIR is | |
# specified on command-line. | |
[ $# -lt 3 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DB_NAME> [<DIR>]" && exit 1 | |
DB_host=$1 | |
DB_user=$2 | |
DB=$3 | |
DIR=$4 | |
[ -n "$DIR" ] || DIR=. | |
test -d $DIR || mkdir -p $DIR | |
echo -n "DB password: " | |
read -s DB_pass | |
echo | |
echo "Dumping tables into separate SQL command files for database '$DB' into dir=$DIR" | |
tbl_count=0 | |
for t in $(mysql -NBA -h $DB_host -u $DB_user -p$DB_pass -D $DB -e 'show tables') | |
do | |
echo "DUMPING TABLE: $DB.$t" | |
mysqldump -h $DB_host -u $DB_user -p$DB_pass $DB $t | gzip > $DIR/$DB.$t.sql.gz | |
tbl_count=$(( tbl_count + 1 )) | |
done | |
echo "$tbl_count tables dumped from database '$DB' into dir=$DIR" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Import các bảng được export riêng trong 1 thư mục | |
[ $# -lt 3 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DB_NAME> [<DIR>]" && exit 1 | |
DB_host=$1 | |
DB_user=$2 | |
DB=$3 | |
DIR=$4 | |
[ -n "$DIR" ] || DIR=. | |
test -d $DIR || mkdir -p $DIR | |
echo -n "DB password: " | |
read -s DB_pass | |
echo | |
echo "Import tables in dir=$DIR to database '$DB'" | |
tbl_count=0 | |
for FILENAME in $DIR/*.sql.gz; do | |
echo "Importing TABLE: $FILENAME" | |
BASENAME=$(basename -- "$FILENAME") | |
echo $BASENAME | |
IFS='.' read -ra FILENAME_INFO <<< "$BASENAME" | |
TABLENAME=${FILENAME_INFO[1]} | |
if [[ $(mysql -NBA -h $DB_host -u $DB_user -p$DB_pass -D $DB -e "show tables like '$TABLENAME'") ]] | |
then | |
echo "Table existed" | |
else | |
zcat $FILENAME | mysql -u $DB_user -h $DB_host -p$DB_pass $DB | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment