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.
Related
I am trying to connect to mysql on localhost via a Bash script.(On a Raspberry PI 3B+ Raspbian)
The connection seems to work but instead of showing the query results it displays some kind of mysql help page.
Which look like this:
mysql Ver 15.1 Distrib 10.1.38-MariaDB, for ... using readline 5.2
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Usage: mysql [OPTIONS] [database]
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
The following groups are read: mysql client client-server client-mariadb
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
...
#!/bin/bash
username="user"
passwort="1234"
mysql -u $username -p$passwort -e "show databases;"
How can i change the command to execute the given statement?
Your password has some characters that was interpreted by the shell , you must protect the password by quoting .
Example my password has a ( space )
MyMac:~ e444$ u="root"
MyMac:~ e444$ p="1234 2"
MyMac:~ e444$ mysql -u$u -p$p -h 127.0.0.1 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'#'172.17.0.1' (using password: YES)
received here a access denied , because the password sent was the 1234
MyMac:~ e444$ mysql -u$u -p"$p" -h 127.0.0.1 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
here protect the variable with "
I am using wampserver 64 bit. I started the MySQL console via the wamp tray menu icon and executed this command as seen in the documentation.
mysqldump magento_live > dump.sql;
Result:
ERROR 1064 (42000): 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 'mysqldump magento_live > dump.sql' at line 1
I also tried it like in this answer:
mysqldump -u root -p root -h localhost magento_live > magentoLiveDump.sql;
Result:
ERROR 1064 (42000): 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 'mysqldump -u root -p root -h localhost
magento_live > magentoLiveDump.sql' at line 1
I am using MySQL 5.7
I tried it via CMD by using this command:
C:\wamp64\bin\mysql\mysql5.7.21\bin>C:\wamp64\bin\mysql\mysql5.7.21\bin\mysql.exe mysqldump magento_live > test.sql
A file is created, but this is the content:
C:\wamp64\bin\mysql\mysql5.7.21\bin\mysql.exe Ver 14.14 Distrib
5.7.21, for Win64 (x86_64) Copyright (c) 2000, 2018, 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: C:\wamp64\bin\mysql\mysql5.7.21\bin\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
[...]
The command mysqldump will not run from MySQL CLI, you will have to run it from the windows command prompt (CMD) or from the Powershell.
You have to start a CMD or Powershell and navigate to the bin folder inside the MySQL directory (wamp64\bin\mysql\mysql5.7.21\bin).
Then you have to execute the command like this:
mysqldump -u[USERNAME] -p target_database > target_dump_name.sql
Important: There is no space allowed between the option and the value.
WRONG: mysqldump -u root -p target_database > target_dump_name.sql
CORRECT: mysqldump -uroot -p target_database > target_dump_name.sql
Example
CMD:
mysqldump -uroot -p magento_live > magento_live_dump.sql
Powershell:
.\mysqldump -uroot -p magento_live > magento_live_dump.sql
Note: In Powershell you will have to add .\ before the command.
I want to run MySQL query from bash script to create new user:
mysql -u root -p$dbpass -e "GRANT ALL PRIVILEGES ON appdb_${BUILD}.* TO appuser#localhost IDENTIFIED BY 'somepass'"
Where $BUILD variable is set to a number, for example 15, so final query should look like this:
mysql -u root -p$dbpass -e "GRANT ALL PRIVILEGES ON appdb_15.* TO appuser#localhost IDENTIFIED BY 'somepass'"
But... in directory where script is and is run there is a tar archive with same name as database, so query becomes like this:
mysql -u root -p$dbpass -e "GRANT ALL PRIVILEGES ON appdb_15.tar.gz TO appuser#localhost IDENTIFIED BY 'somepass'"
I guess this happens because there is a match, but changing file name is not an option. Moving script to other directory is not allowed too.
I tried to escape dot with backslashes (appdb_${BUILD}\.*) - nothing changed.
How I can fix this?
There is nothing wrong with your approach and it is working in my case. See below. Can you tell us what is the error that you are getting so that we can help you?
[root#cloud mysql]# ls
appdb_15.tar.gz test.sh
[root#cloud mysql]# cat test.sh
#!/bin/bash
BUILD=15
# First Method
mysql -u root -ptest -e "GRANT ALL PRIVILEGES ON appdb_${BUILD}.* TO appuser#localhost IDENTIFIED BY 'somepass';"
# Second Method
mysql -u root -ptest <<EOF
GRANT ALL PRIVILEGES ON appdb_${BUILD}.* TO appuser1#localhost IDENTIFIED BY 'somepass';
select user, host from mysql.user where user like 'appuser%';
EOF
[root#cloud mysql]# ./test.sh
user host
appuser localhost
appuser1 localhost
EDIT 1:
I forgot to mention that this is definitely not a bash issue.
EDIT 2: Changed the build variable to 15 from 5.
I am trying to access mysql via command line, but when I enter the command to access my DB, I get the mysql --help text as an output. Here is what I am typing in:
mysql -h localhost -u USER -p PASSWORD DB_NAME
I have confirmed all of my credentials are correct, but when I run that, I get the following:
mysql Ver 14.14 Distrib 5.5.32, for Linux (x86_64) using readline 5.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 do...
I have also tried running it using php exec. I see the same output that way also. Anyone have any thoughts on why this is occurring?
Remove the space between your -p flag and your actual password, or use --password=PASSWORD. From the man page:
If you use the short option form (-p), you cannot have a space between the option and the password.
Don't include PASSWORD in the command. The "-p" is all you need. It will prompt you for the password without showing the characters you type.
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