How to use drush sql-query?

Events happening in the community are now at Drupal community events on www.drupal.org.
tim042849's picture

If I go to the "root" of my site and execute

rush sql-query 'show tables;' -- or some other comand

I get nothing.
Why?
Do I have to "dive into" the file structure?
I've tried cd'ing to sites/default with no different result.
drush help sql-query
doesn't show anything about where to execute
thanks
tim

Comments

Drush requires you to be in

tehbmar's picture

Drush requires you to be in the root of your site to run it (so it can take advantage of drupal's bootstrap system).

For example I would be..

bmar@A-0x001:/var/www/html/wildbonesproject$ drush sqlc

And it seems like you have done that. Some quick checks I would place are..

  • verify 10000% you're in a drupal site directory
    (any folder, including root, in your install)
  • make sure you have the permissions to run drush
  • check for any errors outputted
  • check to see if other drush commands work
  • check to make sure your sites/all/settings.php has the correct credentials for your database

drush sqlc is new to me

tim042849's picture

Hi Brandon:
Thanks for the reply. drush sqlc puts me into the MySQL console, which I am very
familiar with from my CL experience of many years! Good to see it.
So, it looks like all my my connection works, based on credentials and the problem is that for some reason, I am not getting the output. I will get output if I do something like this:

drush sql-query 'show nothing;'
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'nothing' at line 1
I'm a bit baffled but it looks like stderr is being printed but not stdout.

It is late here and we are getting ready for dinner. Thanks very much for the reply. I will pursue this further in the morning and maybe you will have further insights.

If you haven't guessed yet, this is an extension of my investigation into my previous post and I hope that anything I come up with you help will help you.

Cheers
tim

Drupal Noob (since 2012)
Coder since 1987 - Web Coder since 1996
akwebsoft.com - tj49.com

That's really weird, maybe

tehbmar's picture

That's really weird, maybe try this to see if stdout still works
$ echo 'testing stdout'
Also you could always try to reinstall drush (depending on how it was originally installed)

Check https://drupal.org/node/1791676 so see options on different platforms.

Note to self, when puzzled use verbose

tim042849's picture

If I invoke

drush -v sql-query 'show tables;'

The result is (with credentials obfuscated)
Initialized Drupal 7.23 root directory at /Users/http/sites/akwebsoft                                                                                                                                                   [notice]
Initialized Drupal site default at sites/default                                                                                                                                                                        [notice]
Calling system(mysql --database=akwebsoft_dr --host=localhost --user=tim --password=secret  < /private/tmp/drush_6llgEr > '/Users/tim/prj/drush_backup/@DATABASE_@DATE.sql');
Command dispatch complete

/Users/tim/prj/drush_backup/@DATABASE_@DATE.sql is the value for
options['result-file'] in my drushrc. If I look at that file, I see the results.
Now, I need to find out why I originally set that...

Drupal Noob (since 2012)
Coder since 1987 - Web Coder since 1996
akwebsoft.com - tj49.com

Calling it solved

tim042849's picture

I can use (example)
drush -v --result-file=/Users/tim/drush_results.txt sql-query 'show tables;'
to send output to a file at my home directory or
drush -v --result-file=0 sql-query 'show tables;'
Note that the result-file option value is set to 0. Now output goes to the terminal (stdout)

Drupal Noob (since 2012)
Coder since 1987 - Web Coder since 1996
akwebsoft.com - tj49.com

that's weird well I hope that

tehbmar's picture

that's weird well I hope that result file isn't very important and might be worth while to comment out that line, but good find.

tim042849's picture

Find the string and read comments above that line. According to docs this is a "format string" for a naming convention for sql-dump, but sql-query is using it without overwriting the two placeholders (@DATABASE and @DATE). I would be reluctant to disable that option as it is useful for sql-dump.

That was certainly a bit confusing, all right. Works to set result-file to 0 or some arbitrary file via the drush command line. I notice from drush help sql-query that it recommends setting to a file relative to the drupal root. I'm not sure if leaving a file at the drupal root is a good idea. I wouldn't because the result file could end up getting uploaded during the push process and that might not be good, depending on where in the file structure the results file was written.

thanks again Brandon

Drupal Noob (since 2012)
Coder since 1987 - Web Coder since 1996
akwebsoft.com - tj49.com

Hmm that's weird

tehbmar's picture

Hmm that's weird are you actually using a drushrc file?

I ran

bmar@A-0x001:~$ locate drushrc
/usr/share/php/drush/examples/example.aliases.drushrc.php
/usr/share/php/drush/examples/example.drushrc.php

And only had those (and from a few other projects, but none on my normal sites)

also in my example.drushrc.php "$options['dump-dir'] " is commented out.

But I'm glad you were able to figure out what was up. if all else fails write an alias to
alias dsq='drush sql-query --result-file=0'

so you can say dsq 'show tables;'

I am using a .drushrc

tim042849's picture

I've done some tweaking of it for the push and update procedures ...

Aliases are great aren't they? :) We *nixers are lucky.

I'm going to add a function to my python wrapper that will invoke drush, give me the option of writing to stdout or to a file (and then open in vim).

Drupal Noob (since 2012)
Coder since 1987 - Web Coder since 1996
akwebsoft.com - tj49.com

Or to make your script a

tehbmar's picture

Or to make your script a little more exportable if you ever release it you can load up the bash var EDITOR and grab the default system editor

You got it.

tim042849's picture

:)

Drupal Noob (since 2012)
Coder since 1987 - Web Coder since 1996
akwebsoft.com - tj49.com

drush sqlq

DCLoganGuy's picture

I am using mysql 5.6.38. I am using drush sqlq on a multisite. It works great with @sites except that it seems to fail when I try to include a mysql function. For example, the following returns an error. Can someone tell me how to make this work?
drush @sites "sqlq 'SELECT name, FROM users
WHERE DATEDIFF(now(), from_unixtime(LOGIN)) < \"100\"'"

The error is:
Invalid argument supplied for foreach() backend.inc:698

thanks
Milton

I was able to perform this

RAWDESK's picture

I was able to perform this datediff query from drush sqlc

drush -l $drush_profile sqlq "DELETE FROM sessions WHERE datediff(date_time, now()) < -100";

deletes sessions older then 100 days.

Alaska Drupal Users Group

Group categories

Location

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: