MySQL statement works in mysql but not from command line - mysql

I am trying to execute a particular mysql command from the command line. When I do so, it fails, but if I execute the exact same command from within mysql interactive, it works.
Here is the command line version which fails:
$ mysql -uscott -p -e "GRANT ALL PRIVILEGES ON `myuser\_%`.* TO 'myuser'#'localhost';"
The error given is:
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 '* TO 'myuser'#'localhost'' at line 1
Copying and pasting just the mysql command into mysql works:
mysql> GRANT ALL PRIVILEGES ON `myuser\_%`.* TO 'myuser'#'localhost';
I have taken this command verbatim from phpmyadmin when creating a user for a database. How can I fix it so it works from the command line?

The * is being expanded by your shell (bash?).
You can fix this by using single quotes around the SQL statement instead of double or escaping the * with a \

Related

How to pass user defined variables to mysql command

How do I pass variables that I would have created in linux shell to the mysql command.
For example:
# carname=tesla-new
# echo $carname
I want to use the $car-name variable when passing arguments to MySQL commands.
This variable will come in as input from loop while iterating through a list of values.
mysql -u wp-supportuser -h xx.xxx.xxx.xxx -p<mypassword>! -e 'DROP DATABASE ${carname};'
I need the hyphon(-) in the variable's value, else my application will accept. We get the below error when the above command is run:
mysql: > [Warning] Using a password on the command line interface can be insecure. 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 '-wp' at line 1
How will I be able to achieve this?
It is a MySQL error, because MySQL does not support the usage of hyphen by itself. Instead, you need to enclose it into
`
characters. You can use it if you wrap your command inside apostrophes, like:
mysql -u wp-supportuser -h xx.xxx.xxx.xxx -p<mypassword> -e 'drop database `$carname`;

Syntax error in mysql statement used in shell script

I am currently working on a shell script.
I want to modify the contains of the column "balance" in "Student" table as "balance=balance-$deduct" in shell script.
I am executing following line in shell
mysql -h "$host" -u "$user" --password="$pass" --database="$db" -e "UPDATE student SET balance=balance-$deduct WHERE pid=$pid";
Here i specified the host, username and password properly and i have one numeric value stored in $deduct
but when i execute the script i am getting following error:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE pid=132054' at line 1
where I am wrong here?
thanks
You don't need --database=
mysql -h "$host" --user="$user" --password="$pass" "$db" -e "UPDATE student SET balance=balance-$deduct WHERE pid=$pid";
You can also do Use $db in you query
Just remove --database=
It should work.

MySQL Set Password Via Command-Line Variable

I need a one-liner to provision users in my databases. I'm running MySQL 5.6. Here is what I'm setting the password as a variable so that I can pass it dynamically (obviously it won't always be 'password').
mysql_password="password"
mysql -u ted -e "SET PASSWORD FOR 'ted'#'localhost' = PASSWORD($mysql_password);"
I'm getting the following error when I run this:
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 'password)' at line 1
What is wrong with the statement? Can it be corrected or is another solution available so I accomplish this via Bash?
You need to surround the variable by single quotes:
mysql -u root -e "SET PASSWORD FOR 'ted'#'localhost' = PASSWORD('$mysql_password');"

How to execute a "INSERT INTO" query to a remote database with linux command line?

I'm tryng to create a code in raspbian system to update a value in my database mySQL stored into my hosting server using a bash ".sh" scrypt.
this line won't to work but I don't know why...
mysql -h eliuslab.com -u user -pPassWord -D mydatabase -e "INSERT INTO 'home-IP' ('IP') VALUES ('192.168.1.2')"
And the output:
'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 ''home-IP' ('IP') VALUES ('192.168.1.2')' at line 1 '
this query works well in a GUI mySQL client, but what I want to do is to launch this action with my ".sh" bash scrypt.
Thank you so much :)
Okay I got the solution. I ran your script in my mysql database and got the same error. It's not the fault of script but it's a parsing error.
The correct command is
mysql -h eliuslab.com -u user -pPassWord -D mydatabase -e "INSERT INTO tablename (column) VALUES ('BLABLASTRING')"
Notice there is no need of using quotation marks around the table name and the column name. The quotation marks treat them as string constant rather than a container.

Unable to create mysql database dump

I am unable to create mysql database dump. I have tried all the commands in the below question
https://stackoverflow.com/questions/24858436/unable-to-create-mysql-dump-in-mysql-server-5-6-19
But every time I get similar error which asks me to check manual
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 'mysql
dump -u root -pmysqlmysql hospital_management -r "c:\hosp.sql"' at line 1
I am trying these commands in Mysql command line and NOT on Windows command prompt. Also I am trying these commands before entering any database in mysql.
mysql> mysqldump -u root -pmysqlmysql hospital_management > hosp.sql
This was the first command I tried, which did not work
mysqldump is an executable, you should not run it in the MySQL command line.
Try the command
mysqldump -uroot -pmysqlmysql hospital_management > "C:\hosp.sql"
By reading the documentation, I assume that when using -r, the file must already exist.