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
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.
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
the command:
ssh xxx.xxx.xxx.xxx \"mysql -u root -password \\\"grant all privileges on rcf_275d315.* to rfc_user#localhost identified by \'W27j453frxrff23\'\\\"\"
gives me an error:
bash: mysql -u root -p97yf2beiru3trf289 "grant all privileges on rcf_275d315.* to rfc_user#localhost identified by 'W27j453frxrff23'": command not found
when i copy the string returned by bash, end run locally it works. Also works, when pasted on the remote serve.
For some reason it doesn't work over ssh, and returns error:
mysql -u root -p97yf2beiru3trf289 "grant all privileges on rcf_275d315.* to rfc_user#localhost identified by 'W27j453frxrff23'": command not found
UPDATE:
i tried few variations, without success:
ssh xxx.xxx.xxx.xxx \"mysql -u root -pBOY8o7ubio87gubip7 \\\"grant all privileges on rfc_275d315.* to rfc_user identified by \'KUG34dY976fyvc768g\'\\\"\"
result:
bash: mysql -u root -pBOY8o7ubio87gubip7 "grant all privileges on rfc_275d315.* to rfc_user identified by 'KUG34dY976fyvc768g'": command not found
ssh xxx.xxx.xxx.xxx \"mysql -u root -pBOY8o7ubio87gubip7 \\"grant all privileges on rfc_275d315.* to rfc_user identified by \'KUG34dY976fyvc768g\'\\"\"
result:
bash: -c: line 0: unexpected EOF while looking for matching `"'
bash: -c: line 1: syntax error: unexpected end of file
ssh xxx.xxx.xxx.xxx mysql -u root -pBOY8o7ubio87gubip7 \\"grant all privileges on rfc_275d315.* to rfc_user identified by \'KUG34dY976fyvc768g\'\\"
result:
mysql Ver 14.14 Distrib 5.1.66, for redhat-linux-gnu (x86_64) using readline 5.1
Copyright (c) 2000, 2012, 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.
ssh xxx.xxx.xxx.xxx mysql -u root -pBOY8o7ubio87gubip7 \\\"grant all privileges on rfc_275d315.* to rfc_user identified by \'KUG34dY976fyvc768g\'\\\"
result:
mysql Ver 14.14 Distrib 5.1.66, for redhat-linux-gnu (x86_64) using readline 5.1
Copyright (c) 2000, 2012, 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.
still, don't where the problem is. Thanks for any help.
UPDATE:
when echo saved to file:
ssh xxx.xxx.xxx.xxx "echo mysql -u root -piugiu -e \\"grant all privileges on rfc_275d315.* to rfc_user identified by \'in76bn6bgb876n\'\\" > rfc/echo.txt"
i get this in file:
mysql -u root -piugiu -e "grant all privileges on rfc_275d315.* to rfc_user identified by 'in76bn6bgb876n'"
which is proper command, and when copied and pasted to command line on remote server it works, as it should.
when echo is removed:
ssh xxx.xxx.xxx.xxx "mysql -u root -piugiu -e \\"grant all privileges on rfc_275d315.* to rfc_user identified by \'in76bn6bgb876n\'\\" > rfc/echo.txt"
the text file contain info about usage of mysql:
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
ssh xxx.xxx.xxx.xxx 'mysql -u root -pBOY8o7ubio87gubip7 -e "grant all privileges on rfc_275d315.* to rfc_user#localhost identified by '\''KUG34dY976fyvc768g'\''" yourdatabase'
Should be working. You forgot the -e in the MySQL client parameters which allows you to pass a statement
EDIT 1:
fixed quoting
In bash you can differ two ways of quoting (just like in many other languages)
1. Weak quoting: double quotes
e.g. echo "$PATH"
In a weak-quoted string there is no interpretion of
spaces as word-separators
pathname expansion
process substitution
single-quotes to introduce strong-quoting
characters for pattern matching
Otherwise parameter expansion is done:
ls -l "*"
not be interpreted, will pass * literally and will cause an error unless you have a file called *
echo "Your PATH is: $PATH"
Will work as expected. $PATH is interpreted
2. Strong quoting: single quotes
Within single quotes you get no interpretation at all. All characters within single quotes are treated as text.
If you have to use the single quote within a single-quoted text, simply escaping isn't enough. You have to concat like this:
QUERY='SELECT * FROM myTable WHERE col1 = ' \' 'value1' \'
source: http://wiki.bash-hackers.org/syntax/quoting
You don't need to quote the command you send to ssh. Remove your outermost layer of quoting around your mysql command and it should work fine.
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?