如何在 bash 上运行 mysql 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20033648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:27:01  来源:igfitidea点击:

How to run mysql command on bash?

mysqlbashunixterminalspecial-characters

提问by johnatasjmo

The following code works on the command line

以下代码适用于命令行

mysql --user='myusername' --password='mypassword' --database='mydatabase' --execute='DROP DATABASE myusername; 
CREATE DATABASE mydatabase;'

However, it doesnt work on bash file on excecution

但是,它在执行时不适用于 bash 文件

#!/bin/bash
user=myusername
password=mypassword
database=mydatabase

mysql --user='$user' --password='$password' --database='$database' --execute='DROP DATABASE $user; CREATE DATABASE $database;'

I receive the following error:

我收到以下错误:

ERROR 1045 (28000): Access denied for user '$user'@'localhost' (using password: YES)

错误 1045 (28000): 用户 '$user'@'localhost' 访问被拒绝(使用密码:是)

How to make the bash file run as the command line?

如何使bash文件作为命令行运行?

回答by anubhava

Use double quotes while using BASH variables.

使用 BASH 变量时使用双引号。

mysql --user="$user" --password="$password" --database="$database" --execute="DROP DATABASE $user; CREATE DATABASE $database;"

BASH doesn't expand variables in single quotes.

BASH 不会用单引号扩展变量。

回答by johnatasjmo

This one worked, double quotes when $user and $password are outside single quotes. Single quotes when inside a single quote statement.

当 $user 和 $password 在单引号之外时,这个有效,双引号。在单引号语句中使用单引号。

mysql --user="$user" --password="$password" --database="$user" --execute='DROP DATABASE '$user'; CREATE DATABASE '$user';'

回答by flopcoder

I have written a shell script which will read data from properties file and then run mysql script on shell script. sharing this may help to others.

我编写了一个 shell 脚本,它将从属性文件中读取数据,然后在 shell 脚本上运行 mysql 脚本。分享这可能对其他人有所帮助。

#!/bin/bash
    PROPERTY_FILE=filename.properties

    function getProperty {
       PROP_KEY=
       PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
       echo $PROP_VALUE
    }

    echo "# Reading property from $PROPERTY_FILE"
    DB_USER=$(getProperty "db.username")
    DB_PASS=$(getProperty "db.password")
    ROOT_LOC=$(getProperty "root.location")
    echo $DB_USER
    echo $DB_PASS
    echo $ROOT_LOC
    echo "Writing on DB ... "
    mysql -u$DB_USER -p$DB_PASS dbname<<EOFMYSQL

    update tablename set tablename.value_ = "$ROOT_LOC" where tablename.name_="Root directory location";
    EOFMYSQL
    echo "Writing root location($ROOT_LOC) is done ... "
    counter=`mysql -u${DB_USER} -p${DB_PASS} dbname -e "select count(*) from tablename where tablename.name_='Root directory location' and tablename.value_ = '$ROOT_LOC';" | grep -v "count"`;

    if [ "$counter" = "1" ]
    then
    echo "ROOT location updated"
    fi