mysql创建数据库和用户脚本

时间:2009-11-03 11:51:20

标签: mysql database bash scripting centos

问题改写:

HOMEDIR="ftpuser"
REMOTEIP="1.1.1.1"
MYSQLPASS="password"

Q1="DROP USER "$HOMEDIR"_shop;"
Q2="DROP DATABASE "$HOMEDIR"_shop;"
Q3="CREATE DATABASE IF NOT EXISTS "$HOMEDIR"_shop;"
Q4="GRANT ALL ON "$HOMEDIR"_shop TO '"$HOMEDIR"_shop'@'localhost' IDENTIFIED BY '$MYSQLPASS';"
Q5="GRANT ALL ON "$HOMEDIR"_shop TO '"$HOMEDIR"_shop'@'anotherip' IDENTIFIED BY '$MYSQLPASS';"
# Need to grant permissions from another server as well
Q6="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}${Q4}${Q5}${Q6}"

echo $SQL
echo " "
ssh -p 8899 root@$REMOTEIP "mysql -u root -p "$SQL""
然后我跑了:

/root/testing/migratesite.sh

得到:

bash: DROP: command not found
bash: CREATE: command not found
bash: GRANT: command not found
bash: GRANT: command not found
bash: FLUSH: command not found

我错过了什么?

2 个答案:

答案 0 :(得分:3)

您缺少引号和正确的mysql客户端命令行:

ssh -p 8899 root@$REMOTEIP "mysql -u root -p -e \"$SQL\""

你需要转义$ SQL变量周围的引号,以便将它们传递给远程shell,否则它们会被本地shell解释(这就是为什么你得到DROP:命令未找到,半冒号由shell解释) 。)另外,要让mysql客户端执行命令,必须传递-e命令行选项。

答案 1 :(得分:0)

你试过这个:

ssh -p 8899 root@$REMOTEIP "echo \"$SQL\" | mysql -u root --password=$SQL_PASS"