Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

create mysql user and database from script

# create random password
PASSWDDB="$(openssl rand -base64 12)"

# replace "-" with "_" for database username
MAINDB=${USER_NAME//[^a-zA-Z0-9]/_}

# If /root/.my.cnf exists then it won't ask for root password
if [ -f /root/.my.cnf ]; then

    mysql -e "CREATE DATABASE ${MAINDB} /*!40100 DEFAULT CHARACTER SET utf8 */;"
    mysql -e "CREATE USER ${MAINDB}@localhost IDENTIFIED BY '${PASSWDDB}';"
    mysql -e "GRANT ALL PRIVILEGES ON ${MAINDB}.* TO '${MAINDB}'@'localhost';"
    mysql -e "FLUSH PRIVILEGES;"

# If /root/.my.cnf doesn't exist then it'll ask for root password   
else
    echo "Please enter root user MySQL password!"
    echo "Note: password will be hidden when typing"
    read -sp rootpasswd
    mysql -uroot -p${rootpasswd} -e "CREATE DATABASE ${MAINDB} /*!40100 DEFAULT CHARACTER SET utf8 */;"
    mysql -uroot -p${rootpasswd} -e "CREATE USER ${MAINDB}@localhost IDENTIFIED BY '${PASSWDDB}';"
    mysql -uroot -p${rootpasswd} -e "GRANT ALL PRIVILEGES ON ${MAINDB}.* TO '${MAINDB}'@'localhost';"
    mysql -uroot -p${rootpasswd} -e "FLUSH PRIVILEGES;"
fi
Comment

PREVIOUS NEXT
Code Example
Shell :: github refs/remotes/origin/master do not point to a valid object 
Shell :: angular reduce vendor.js size 
Shell :: how to open file in linux 
Shell :: installer composer windows 10 
Shell :: Rollback a Particular Commit in git command 
Shell :: select my keyboard ubuntu server 
Shell :: how to run a command async in ubuntu 
Shell :: how to scp from remote machine to local machine 
Shell :: uninstall package with yarn 
Shell :: how to use github to setup projects 
Shell :: isntall gnache cli 
Shell :: change default branch on git 
Shell :: grep search 
Shell :: how to set umask in linux 
Shell :: how to compare percentage value in shell script 
Shell :: wget a file from bitbucket 
Shell :: latest package yarn dependencies 
Shell :: Everything up-to-date git push origin main 
Shell :: get last 10 lines of log 
Shell :: update branch with master 
Shell :: terraform version command 
Shell :: git first time 
Shell :: vi search 
Shell :: how to upgrade gradle version in linux 
Shell :: growth ebs disc ec2 running 
Shell :: microk8s enable dns 
Shell :: how to rename a file in linux 
Shell :: create github repo without browser 
Shell :: remove git from local repository 
Shell :: REMOTE HOST IDENTIFICATION HAS CHANGED! how to fix in ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =