How to run mysql command on bash?

后端 未结 3 1408
南方客
南方客 2021-02-01 00:15

The following code works on the command line

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


        
3条回答
  •  温柔的废话
    2021-02-01 00: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<

提交回复
热议问题