Mysql, Create database and Grant user by Bash script. 9 개월 전
$ rails <project_name>
이렇게 멋지게 커맨드로 프로젝트를 만든다음에 mysql 에 디비만들고 유저추가하는게 말은 안했지만 내심 귀찮거나 두려웠던 분들... 저 역시 mysql shell에 접속해 만든 디비가 2만개에 달하는 지금에야 스크립트를 짰습니다.
#!/bin/bash
if [ -n "$1" ]
then
read -p "Mysql username: " user
if [ -z $user ]; then echo "Username is required.";exit 1; fi
stty -echo
read -p "Password for $user: " pass
stty echo
if [ -z $pass ]; then echo -e "\nPassword is required.";exit 1; fi
printf "\n\n\033[0;32;32m>> \033[0;mdatabase: \033[0;32;32m$1\033[0m, username: \033[0;32;32m$user\033[0m\n\n"
read -p "Continue? [y/n]" ans
if [[ $ans =~ ^[yY]$ ]]
then
echo "Require mysql root password."
mysql -uroot -p -e "create database $1;grant all privileges on $1.* to '$user'@'localhost' identified by '$pass'"
else
echo "Bye."
fi
else
echo "Database name is required."
exit 1
fi
if [ -n "$1" ]
then
read -p "Mysql username: " user
if [ -z $user ]; then echo "Username is required.";exit 1; fi
stty -echo
read -p "Password for $user: " pass
stty echo
if [ -z $pass ]; then echo -e "\nPassword is required.";exit 1; fi
printf "\n\n\033[0;32;32m>> \033[0;mdatabase: \033[0;32;32m$1\033[0m, username: \033[0;32;32m$user\033[0m\n\n"
read -p "Continue? [y/n]" ans
if [[ $ans =~ ^[yY]$ ]]
then
echo "Require mysql root password."
mysql -uroot -p -e "create database $1;grant all privileges on $1.* to '$user'@'localhost' identified by '$pass'"
else
echo "Bye."
fi
else
echo "Database name is required."
exit 1
fi
차암 쉽죠잉?
가까운 글
- Install Sabayon linux (Gentoo based) on Macbook (non-pro 1 Gen)
- Mysql, Create database and Grant user by Bash script.
- Chrome 을 본받거라!

원래는 정말로 쉬웠는데 넘겨받은 값도 확인하고 색상을 좀 넣었더니....