I am trying to do some simple statistics on data I am pulling from a database, but whenever I execute the mysql command all I get is the mysql usage listing. What is confusing to me is this command works from the command line, but not from inside my ruby script.
Command:
query = 'select * from builds;'
results = `mysql -h <hostname> -u root -D build -e #{query}`
print results
I want to see everything in that table, but what I get is this:
mysql Ver 14.12 Distrib 5.0.37, for
Win32 (ia32) Copyright (C) 2002 MySQL
AB This software comes with ABSOLUTELY
NO WARRANTY. This is free software,
and you are welcome to modify and
redistribute it under the GPL license
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit. -I, --help
Synonym for -? --auto-rehash
Enable automatic rehashing. One
doesn't need to use
'rehash' to get table and field completion, but
startup
and reconnecting may take a longer time. Disable with
--disable-auto-rehash. -A, --no-auto-rehash
No automatic rehashing. One has to use 'rehash' to
get
table and field completion. This gives a quicker start
of
mysql and disables rehashing on reconnect.
WARNING:
options deprecated; use --disable-auto-rehash
instead. -B, --batch Don't
use history file. Disable interactive
behavior.
...etc
any help would be great.
Thanks.
I would highly recommend using the mysql gem for ruby. It allows you to operate natively instead of wrapping the command line.
But in your command I would try wrapping the query in quotes before executing it.
results = `mysql -h <hostname> -u root -D build -e "#{query}"`
Any reason you can't just use the mysql gem?
Related
I'm running the following query from the command line in Raspbian:
mysql -u $NAME -p $PASS Tweets -e "SELECT count(*) FROM raw_tweets;"
And it is outputting the following. I'm sure it's a setting somewhere, but all of my searching has been fruitless. Thanks in advance for the help.
mysql Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (armv7l) using readline 6.2
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
and reconnecting may take a longer time. Disable with
--disable-auto-rehash.
(Defaults to on; use --skip-auto-rehash to disable.)
-A, --no-auto-rehash
No automatic rehashing. One has to use 'rehash' to get
table and field completion. This gives a quicker start of
mysql and disables rehashing on reconnect.
--auto-vertical-output
Automatically switch to vertical output mode if the
result is wider than the terminal width.
-B, --batch Don't use history file. Disable interactive behavior.
(Enables --silent.)
--character-sets-dir=name
Directory for character set files.
--column-type-info Display column type information.
-c, --comments Preserve comments. Send comments to the server. The
default is --skip-comments (discard comments), enable
with --comments.
-C, --compress Use compression in server/client protocol.
-#, --debug[=#] This is a non-debug version. Catch this and exit.
--debug-check Check memory and open file usage at exit.
-T, --debug-info Print some debug info at exit.
....... (Abbreviated, above should give enough of an example)
Your syntax is almost right, you forgot the add the database name parameter. Try this:
mysql -u $NAME -p $PASS -e "SELECT count(*) FROM raw_tweets" yourDBname
If Tweets is your database name, try this:
mysql -u $NAME -p $PASS -e "SELECT count(*) FROM raw_tweets" Tweets
Where $NAME and $PASS is your username and password with grants both for the database and the table.
No need of the semicolon. If you are running just one select command.
Going by the list of flags here, with the line at the top about usage: Usage: mysql [OPTIONS] [database].
I am running Windows 8; my table is "contact"; I want to create a tee file for source C:/myfile.sql with the verbose option on.
I have tried mysql -v contact, -v contact, --verbose contact, --verbose source C:/myfile.sql, and various others.
EDIT: adding screenshot of where I'm trying to run this, in case it helps.
The correct syntax for a verbose interactive session is:
c:\> mysql -u yourUser -p -v yourDatabase
This will launch an interactive session (prompting you for password), and set yourDatabase as the default database for the session.
Once in this interactive session, issue a tee command:
mysql> tee c:/temp/my.out
And now you can source your script:
mysql> source c:/myfile.sql
Of course, you can avoid all this pain by simply putting this in your command prompt:
c:\> mysql -u yourUser -pYourPassword -v yourDatabase < myfile.sql > my.out
This will:
Push the contents of myfile.sql to an "interactive" mysql session... that's essentially a batch process
Redirect all contents of the batch process to my.out
Reference:
MySQL Reference Manual: mysql CLI: mysql options
That should work, be aware of the db name.
mysql -v contact
If you db requires login:
mysql -v -udbuser -p contact
I'm used to having tab-completion for mysql keywords, database names etc in mysql, but in my freshly installed mysql (via 'apt-get install mysql-server') it only works for table names, not database names.
I am supplying the --auto-rehash option when starting the mysql command line client interface. I'm aware that I can set this as a default via my.cnf, but I want to get it working first.
The mysql docs tell me that the auto-rehash feature "requires a MySQL client that is compiled with the readline library."
'aptitude show mysql-client' tells me that I have 5.5.43-0ubuntu0.14.04.1.
For debugging purposes, how can I know if my mysql-client has readline, and if not, how do I get one that does?
Resolved: I wasn't specifying a database name when invoking the mysql command line interface!
Auto-completion works as expected if I go in as:
mysql -u root -p mysql # or
mysql -u root -p mydatabase
as opposed to:
mysql -u root -p
(and --auto-rehash clearly is on by default as per the docs)
I am trying to backup a mysql database using mysqldump command, but the message I get is mysqldump: option '--tables' cannot take an argument.
Here you are:
root#myhost:~# mysqldump mydatabase --user myuser --password mypassword
Warning: Using unique option prefix table instead of tables is deprecated and will be removed in a future release. Please use the full name instead.
mysqldump: option '--tables' cannot take an argument
I have tryed several argument combinations, but I finally discovered that the result is the same if I just try to get the command version or event with no arguments at all:
root#myhost:~# mysqldump --version
Warning: Using unique option prefix table instead of tables is deprecated and will be removed in a future release. Please use the full name instead.
mysqldump: option '--tables' cannot take an argument
root#myhost:~# mysqldump
Warning: Using unique option prefix table instead of tables is deprecated and will be removed in a future release. Please use the full name instead.
mysqldump: option '--tables' cannot take an argument
As you can see in the following lines, it is mysql server 5.5 running on debian 7.
System version:
root#myhost:~# uname -a
Linux myhost 3.2.0-4-amd64 #1 SMP Debian 3.2.41-2+deb7u2 x86_64 GNU/Linux
mysql client version:
root#myhost:~# mysql --version
mysql Ver 14.14 Distrib 5.5.35, for debian-linux-gnu (x86_64) using readline 6.2
mysql server version:
root#myhost:~# mysql -h localhost --user=myuser --password=mypassword mydatabase
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 75
Server version: 5.5.35-0+wheezy1-log (Debian)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> quit
Bye
I have looked for this problem on the web, but I cannot see anyone reporting this precise issue. I am not an expert on mysql but I can say it is a very simple install. Should you need more information, please tell me.
Thanks in advance,
ivan
Following #DCoder indications I inspected /etc/mysql/my.cnf which, among others, contained
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
table = true
After removing table = true line from /etc/mysql/my.cnf, mysqldump command works as expected:
root#myhost:~# mysqldump
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
My conclusion is that table=true option is not suitable for mysqldump command and must be removed from [client] in the options file. [client] section groups option settings applied to all client programs.
Should another command need that option set, it should be placed in another program section, neither in [mysqldump] nor in [client].
Try this
mysqldump --tab = dir_name options db_name tbl_name
--tab writes each dumped file as a tab-delimited text file in the "dir_name" directory.
db_name is the db containing the table to the exported.
tbl_name is the table to be exported.
"options" part may include options such as --host or --user.
e.g.
mysqldump --tab = /tmp office contact
I'm using MySql 5.5 on Mac 10.7.5. From the shell (I'm using bash), I'd like to be able to run a command o truncate data in all tables. Also, I'd like to enter a single command that won't prompt me for a password. I've tried this, that I found on another SO post, but no dice. What is a shell command I can use to truncate all database table data?
mysql -umyuser -p -e 'SET FOREIGN_KEY_CHECKS = 0; show tables' my_db | while read table; do mysql -e -umyuser -p "truncate table $table" my_db; done
Enter password:
mysql Ver 14.14 Distrib 5.5.25, for osx10.6 (i386) using readline 5.1
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
and reconnecting may take a longer time. Disable with
--disable-auto-rehash.
(Defaults to on; use --skip-auto-rehash to disable.)
-A, --no-auto-rehash
No automatic rehashing. One has to use 'rehash' to get
table and field completion. This gives a quicker start of
mysql and disables rehashing on reconnect.
--auto-vertical-output
Automatically switch to vertical output mode if the
result is wider than the terminal width.
-B, --batch Don't use history file. Disable interactive behavior.
(Enables --silent.)
--character-sets-dir=name
Directory for character set files.
--column-type-info Display column type information.
-c, --comments Preserve comments. Send comments to the server. The
default is --skip-comments (discard comments), enable
with --comments.
-C, --compress Use compression in server/client protocol.
-#, --debug[=#] This is a non-debug version. Catch this and exit.
--debug-check Check memory and open file usage at exit.
-T, --debug-info Print some debug info at exit.
-D, --database=name Database to use.
--default-character-set=name
...
You can dump database without data:
mysqldump -u myuser -p --databases --add-drop-database --no-data my_db > my_db.sql
And restore it after that
mysql -u myuser -p < my_db.sql