BASH script not reading input mysql password [duplicate] - mysql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to execute Mysql command from a shell script?
I'm trying to execute a .sql file using a bash script. But I am having a problem connecting to MySQL. Here's what I have so far:
#! /bin/sh
PWD="thepassword"
mysql -p -u theuser < Randomsqlfile.sql
echo $PWD
When the password is prompted, nothing fills out.

Make this:
mysql -utheuser -pthepassword <Randomsqlfile.sql

Related

Bash mysql not being executed [duplicate]

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 1 year ago.
I'm constructing a Bash script to execute a MySQL query. The Bash scrip is very simple, but the query is not being executed correctly. MySQL responds like a do a mysql usage (help of commands). What am I doing wrong?
The bash file is:
COMANDO='mysql -h 148.72.64.68 -p******** -u root db_vias_ue -e "select count(*) from clientes"'
$COMANDO
Bash quoting rules are seriously weird. In what you wrote, the expression within double quotes actually gets separated into multiple arguments.
Try this:
COMANDO='mysql -h 148.72.64.68 -p******** -u root db_vias_ue -e "select count(*) from clientes"'
bash -c "$COMANDO"

unknown option '--print-defaults' from bash script [duplicate]

This question already has answers here:
Reading quoted/escaped arguments correctly from a string
(4 answers)
Closed 3 years ago.
I'm trying to run a query from a bash script:
#!/bin/bash
query="\"show databases\""
command="mysql --defaults-file=/user/.my.cnf -e "
outputfile=" > query_result.txt"
command=$command$query$outputfile
$($command)
the result is this:
# ./query_test
mysql: unknown option '--print-defaults'
what I'm doing wrong?
The command:
mysql --defaults-file=/user/.my.cnf -e "show databases"
works without any issue from the shell
thanks to the comment of #benjamin-w I solved with this:
#!/bin/bash
args=(--defaults-file=/users/.my.cnf)
args+=(-e "show databases")
outputfile="query_result.txt"
mysql "${args[#]}" > "$outputfile"
other examples in this link:
https://mywiki.wooledge.org/BashFAQ/050

How to dump sql file from local to server [duplicate]

This question already has answers here:
mysqldump backup and restore to remote server
(6 answers)
Closed 4 years ago.
I have a SQL file in my local computer size of 9.5Gb. And I want the dump that SQL into the live server through the command line. I want to know the command line to dup the SQL from local to live server
mysql -u root -p db_name < /path of sql file
mysql -u username -p db_name -h hostname < /path/file.sql

Read user input and send it to MySQL [duplicate]

This question already has answers here:
How to feed mysql queries from bash
(7 answers)
Closed 5 years ago.
So I am attempting to make my first Linux script and its just to setup a MySQL user and I'm trying to do that all with a script, but I can not seem to find how I can send the MySQL command from the script. This is what I have right now.
#!/bin/bash
read -p "Please enter your desired MySQL username: " USER
echo $USER
read -p "Please enter your desired MySQL password: " PASS
echo $PASS
mysql CREATE USER $USER#'localhost' IDENTIFIED BY $PASS;
any help would be appreciated
Thanks
-Jamie
You cannot mixup mysql queries in bash script.So, you have to save the mysql queries to a mysql file i.e .sql file and then run the mysql command
To append the create user line to a .sql file use
echo 'mysql CREATE USER $USER#'localhost' IDENTIFIED BY $PASS' >> filename.sql
Now the .sql file will contain the sql queries and you can run
$ mysql -h "server-name" -u "root" "-pXXXXXXXX" "database-name" < "filename.sql"

MySQL prompts for the password when using heredoc [duplicate]

This question already has answers here:
How to provide password to a command that prompts for one in bash?
(8 answers)
Closed 8 years ago.
I am trying to execute a shell script to login to mysql as root and execute some commands, and in order to avoid putting the root's password in the command line, I am using heredoc format as shown below.
However, mysql prompts me for the password despite the fact that I am giving it the right password.
Exactly the same syntax works perfect on some other hosts but not here.
Why?
mysql#myhost:MySQL> mysql -uroot -p -s -P3306 -e 'SELECT NOW();' <<EOF
> MyPassword
> EOF
Enter password: <---- why does it require manual password entry in here?
2014-12-23 14:57:25
Assuming you use a BASH or KSH command line interface:
read -s pwd;mysql -uroot --password=${pwd} -s -P3306 -e 'SELECT NOW();'
For the read command, you should use the -s option because, as it happens with using heredoc, this solution would also display the password as it is being typed, if you wouldn't use said option.
This solution works because the mysql client obfuscates the password so it does not show in the output from process-listing commands, like ps. Run this:
read -s pwd;mysql -uroot --password=${pwd} -s -P3306
Then, if you open an additional command line interface and execute ps, you will see that it does not show the password, but and obfuscated version of it, in the process list.
This was a famous bug of the mysql client, back in the day, but was fixed in 2002.
Using read -s to put the password in a variable has the added benefit of not showing the password in the command-line interface's history.