1

I am trying to use capistrano to run a mysql command.

In my local termainal if i run

mysql -u <username> -p <password> -h <server ip> <database name>

It connects to mysql and is ready for commands.

If however i run this in a capistrano task....

execute "mysql -u <username> -p <password> -h <server ip> <database name>"

If runs without failing and just hangs there. Does this mean it is connected and waiting for a command? If so how would i provide the command?

9
  • Do you expect to be able to use it interactively, or do you have just one command to execute? The -e 'SELECT * FROM tbl' command line option will run one. Commented Nov 4, 2014 at 14:00
  • @MichaelBerkowski i have several SQL queries that i want to run, these SQL queries will be hard coded in the task file. Do you think the fact it is hanging means it is ready to receive a command? Commented Nov 4, 2014 at 14:02
  • 1
    It looks like it, but I don't know that it is useful. I just established an SSH connection and started a mysql prompt like ssh localhost mysql -ppasswd dbname and got no output back, but my command didn't complete. I then typed SELECT COUNT(*) FROM tbl; and still got no output until I did ctl-d. After that, my query result was displayed and my prompt returned. Commented Nov 4, 2014 at 14:06
  • See also this question. As an alternative to -e you can pipe SQL statements into mysql like echo 'SELECT * FROM tbl' | mysql -ppasswd dbname Commented Nov 4, 2014 at 14:09
  • @MichaelBerkowski Using -e and separating queries by semicolons works - thank you for the answer :). My only question now is why? Is there any way for me to connect to mysql in one command, run a query in the next command and then exit mysql? If i run these run commands separately in terrainal it works fine, but stringing them together in capistrano it falls down. Commented Nov 4, 2014 at 14:15

1 Answer 1

1

The mysql command line client has a few different methods of providing queries on the command line. It can accept them with the --execute/-e flag like:

mysql --execute 'SELECT * FROM tbl' --user=user dbname

Or it can accept them from STDIN by pipe or file redirection:

echo 'SELECT * FROM tbl' | mysql --user=user dbname
# Or:
mysql --user=user dbname < file_containing_queries.sql

For your application in Capistrano, executing them via --execute should work well. You may delimit multiple statements with semicolons, but be sure to properly quote everything. The full set of SQL statements should be one quoted string.

# In a Capistrano task:
execute "mysql -e'SELECT * FROM tbl1; SELECT * FROM tbl2;' -u <username> -p <password> -h <server ip> <database name>"

This may be a good use of Ruby's alternative %q/%Q quoting to avoid issues quoting string literals inside the SQL statements:

execute %q{mysql -e"SELECT * FROM tbl1 WHERE col='value'; SELECT * FROM tbl2 WHERE col='value';" -u user -ppasswd -hhost dbname}

When launching the mysql client via SSH in Capistrano, it appears that the client is loaded on the server and accepting statements, but feeding them interactively would be considerably more difficult. MySQL provides --execute and STDIN options for this reason, so it would be best to use them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.