MySQL outputting help/options when executing from command line - mysql

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.

Related

Cannot access remote MySQL database in shell [duplicate]

I am trying to connect to a remote MySQL server:
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql.exe -u USERNAME -p PASSWORD -h www.example.net DB_NAME
The result:
mysql.exe Ver 14.14 Distrib 5.7.17, for Win64 (x86_64)
Copyright (c) 2000, 2016, 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.exe [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.
... and so on, the help page is long.
Why does the mysql.exe utility ignore my commands?
The password must have no space before it:
mysql.exe -u USERNAME -pPASSWORD

mysqldump always returns "option '--tables' cannot take an argument"

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

Bash-MySQL / try to insert string to table

I try to insert string to mysql in bash, so I do the next:
message="<a href = http://www."
message="$message ${d}"
message="$message .com"
mysql -u root -pmypass -Bse 'INSERT INTO atTable VALUES (null, "'$message'")'
When I do it, I get the next massage:
mysql Ver 14.14 Distrib 5.1.69, for debian-linux-gnu (i486) using readline 6.1
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.
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.
-B, --batch Don't use history file. Disable interactive behavior.
(Enables --silent.)
--character-sets-dir=name
Directory for character set files.
and other commands. What I do wrong?
Please have a try with this one:
message="<a href = http://www."
message="$message ${d}"
message="$message .com"
mysql -u root -pmypass -Bse "INSERT INTO atTable VALUES (null, '$message')";
At least it worked for me, when I tested it with this:
message="<a href = http://www."
message="$message hello"
message="$message .com"
mysql -u root -pwhatever -Bse "SELECT '$message'";
Try this:
mysql -u root -pmypass -Bse "INSERT INTO atTable VALUES (null, '$message')"
The problem was the spaces in $message were ending the -e option.
Instead of piecing together the message variable like you did, this is easier to read:
message="<a href = http://www. $d .com"
This is equivalent to the example in the original post, though the text itself doesn't look meaningful.
You can pass your query to mysql like this:
mysql -u root -pmypass -Bse "INSERT INTO atTable VALUES (null, '$message')"
If message contains single quotes, you need to escape them, you can do like this:
message=$(echo "$message" | sed -e "s/'/\\\\'/")
Instead of putting your root password on the command line, I recommend to put that information in the .my.cnf file of your home directory, for example:
[client]
database=yourdbname
user=root
password=yourpass
However, before entering the real password, protect the file first like this:
touch .my.cnf
chmod 600 .my.cnf

How do I truncate all data in my MySQL database?

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

calling mysql from a ruby program

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?