How to execute a MySQL command from a shell script?

前端 未结 14 1151
生来不讨喜
生来不讨喜 2020-11-28 01:28

How can I execute an SQL command through a shell script so that I can make it automated?

I want to restore data I have collected in a SQL file using a shell script.

14条回答
  •  北海茫月
    2020-11-28 01:48

    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.

    #!/bin/bash
        PROPERTY_FILE=filename.properties
    
        function getProperty {
           PROP_KEY=$1
           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<

提交回复
热议问题