couldn't execute "mysql": no such file or directory, using autoexpect

10,184

Solution 1

Giving a command, without a full path, only really works without a path to the executable if its in your path variables - as per the mysql docs, you need to add PATH=${PATH}:/usr/local/mysql/bin to your path - in .bashrc for interactive shells or .bash_profiles for non interactive ones.

Solution 2

Make sure your mysql binary is in your $PATH variable. You can check this by executing

echo $PATH;

If you do not see the path to your mysql binary there, you'll have to modify the PATH variable and add the path to your mysql binary in the ~/.bash_profile or ~/.bash_rc file to make it available globally on login

You can alternatively use the full path to your mysql binary in your code. For example

spawn /usr/bin/mysql -h $db_host -u $db_user -p $db_pass 'create database $new_db_name'
Share:
10,184

Related videos on Youtube

Andrey Yasinishyn
Author by

Andrey Yasinishyn

Updated on September 18, 2022

Comments

  • Andrey Yasinishyn
    Andrey Yasinishyn over 1 year

    I have the following file: exp.exp

    #!/usr/bin/expect
    
    db_host='localhost'
    db_name='webui_dev'
    db_user='root'
    db_pass='rootpass'
    new_db_name='db_2011'
    
    expect <<EOF
      log_user 0
      spawn mysql -h $db_host -u $db_user -p $db_pass 'create database $new_db_name'
      expect "password:"
      send "$db_pass\r"
      log_user 1
      expect eof
    EOF
    

    I make it executable by sudo chmod +x script/year_changer/create_db.exp, but if I try to execute it I get an error:

    couldn't execute "mysql": no such file or directory
        while executing
    "spawn mysql -h localhost  -u root -p rootpass  'create database db_2011'"
    
    • Joni
      Joni over 10 years
      I have to ask the obvious... Is the mysql client installed?
    • Andrey Yasinishyn
      Andrey Yasinishyn over 10 years
      Yes definitely. sudo /usr/local/mysql/support-files/mysql.server restart Shutting down MySQL .. SUCCESS! Starting MySQL .... SUCCESS!
    • Barmar
      Barmar over 10 years
      Is the mysql command in your $PATH when you run the script?
    • Andrey Yasinishyn
      Andrey Yasinishyn over 10 years
      @Barmar. Don't know really. How can I check it??
    • Barmar
      Barmar over 10 years
      What happens if you just type mysql from the shell instead of running the script?
    • Barmar
      Barmar over 10 years
      echo $PATH and see if it contains the directory where the mysql executable is installed?
    • Thalys
      Thalys over 10 years
      what does "which mysql" say? it might be that mysql is not in your path?
    • Andrey Yasinishyn
      Andrey Yasinishyn over 10 years
      I'm sorry.. It's my mistake mysql -bash: mysql: command not found
    • Barmar
      Barmar over 10 years
      BTW, since you're specifying the password on the command line, it won't prompt for the password, so you shouldn't expect "password:".
    • Andrey Yasinishyn
      Andrey Yasinishyn over 10 years
      @Barmar. May you post you answer as an Answer?
    • Barmar
      Barmar over 10 years
      I didn't give an answer, I just asked questions, what should I post?
    • Andrey Yasinishyn
      Andrey Yasinishyn over 10 years
      @Journeyman Geek It's blank, but you are right, it isn't in my path. By typing mysql I reseive an error mysql -bash: mysql: command not found. So I need to add it to the path.
    • Thalys
      Thalys over 10 years
      hm, which should normally give a full path, IIRC, is it even installed?
    • Andrey Yasinishyn
      Andrey Yasinishyn over 10 years
      Yes it is. I added mysql commans to my path and it works.
    • Andrey Yasinishyn
      Andrey Yasinishyn over 10 years
      @Barmar. But your question was an clue to the solution. Thanks for help.