How can I run a mysql query (just he query part) which is written to a file, eg. accounts.txt
/test/mysql -h testdb.com -P 8995 -p -u testaccount -e
"select distinct
amp.account_id
from
account_marketplace_groups amp;" -D company > /tmp/output.csv
How can I put the actual query in a file and execute the above and still get the output in a csv
Would this be the right way to do it ?
/test/mysql -h testdb.com -P 8995 -p -u testaccount -e /account.txt -D company > /tmp/output.csv
The -e option can't be used with a filename, it expects the parameter to be the query. Just use input redirection, since mysql reads from standard input.
/test/mysql -h testdb.com -P 8995 -p -u testaccount -D company < /account.txt > /tmp/output.csv
+1 to Barmar's answer but here is another solution, which works equally well:
/test/mysql -h testdb.com -P 8995 -p -u testaccount \
-e "source /account.txt" -D company > /tmp/output.csv
That is, if you don't want to use < for input redirection, you can use the -e "source ..." command to read the file from within the command-line.
Related
I would like to update the following bash script, which individually dumps each table in a given schema:
for t in $(mysql -NBA -h db_host -u db_user -pdb_pass db_name -e 'show tables')
do
mysqldump -h db_host -u db_user -pdb_pass db_name $t > db_name.$t.sql
to exclude some tables that do not need to be picked up by this script. This is what I mean:
for t in $(mysql -NBA -h db_host -u db_user -pdb_pass db_name -e 'show tables where `Tables_in_db_name` not like 'table1' and `Tables_in_db_name` not like 'table2'')
do
mysqldump -h db_host -u db_user -pdb_pass db_name $t > db_name.$t.sql
The sql is fine in itself, but I can't get it to run inside the -e command. Obviously in this example, the problem is the -e command's opening and closing apostrophes. I have tried:
Using quotes instead of apostrophes. ie -e "show tables where etc"
Using slashes on the internal apostrophes. ie \'table1\'
With no success. Does anybody know how to accommodate apostrophes and back ticks within these confines?
Thank you.
The SQL is fine in itself, but I can't get it to run inside the -e command. Obviously in this example, the problem is the -e command's opening and closing apostrophes.
Right. I assume it will be necessary to escape the backticks. After a short test it seems that
mysql -NBA -h ${DB_HOST} -u "${DB_USER}" -p" ${DB_PASS}" ${DB_NAME} -e "SHOW TABLES WHERE \`Tables_in_${DB_NAME}\` NOT LIKE 'table%'"
is working.
The code below extracts views separately from the database. However, I'm trying to get this to run in a single docker run or exec command.
Right now when I try, the pipe command and in combination with trying to escape quotes gives me errors.
mysql -u username INFORMATION_SCHEMA
--skip-column-names --batch
-e "select table_name from tables where table_type = 'VIEW'
and table_schema = 'database'"
| xargs mysqldump -u username database
> views.sql
Anyone know how to achieve this within one docker command?
For example:
docker exec -i $(docker-compose ps -q mysqldb) mysql ...
Much love.
You can run both the mysql client command and the mysqldump tool from somewhere that's not "on the database server". In your case, you can run them from the host that has the MySQL server, assuming you launched the database with options like docker run -p 3306:3306. It would look something like
mysql -h 127.0.0.1 -u username INFORMATION_SCHEMA \
--skip-column-names --batch \
-e "select table_name from tables where table_type = 'VIEW' and table_schema = 'database'" \
| xargs mysqldump -h 127.0.0.1 -u username database \
> views.sql
This avoids all of the shell quoting problems trying to feed this into docker exec, and also avoids the requirement to need root-level access on the host to do an administrative task (if you can run any Docker command at all then you can use docker run to add yourself to the host's /etc/sudoers, among other things).
I also agree with #MichaelBoesl's answer, though: this is long enough that trying to make it into a one-liner isn't really worth the trouble that the various quoting and escaping will bring. I'd probably write this into a script and put the SQL query into a file.
#!/bin/sh
: ${MYSQL_HOST:=127.0.0.1}
: ${MYSQL_USER:=username}
: ${MYSQL_DATABASE:=INFORMATION_SCHEMA}
cat >/tmp/dump_views.sql <<SQL
SELECT table_name
FROM tables
WHERE table_type='VIEW' AND table_schema='database';
SQL
mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" --skip-column-names --batch \
"$MYSQL_DATABASE" </tmp/dump_views.sql \
| xargs mysqldump -h "$MYSQL_HOST" -u "$MYSQL_USER" "$MYSQL_DATABASE"
You can put all your commands into a bash script on the container and just execute the script!
I can execute mysql passing in a file as follows.
mysql -u username -p < some_file
In a bash script I have a function which echoes output which I want to pass into the same command in a bash script.
some_function() {
echo "Some SQL"
}
How can I pass the output into mysql using pipes/redirection?
I have tried the following, but it fails with no such file or directory. How can I use the output from the function here instead.
mysql -u username -p < some_function
No need to use a pipe or a redirection in this case, you can use directly -e options to execute some SQL commands:
mysql -u username -p -e "SQL/MySQL commands"
Exemple on a specific database:
mysql -u username -p -e "use database_name; SHOW tables"
mysql -u username -p -e "SHOW tables" database_name
And you can also catch the output of a command or function and passing it as argument like this:
sql_command="$(your_function)"
mysql -u username -p -e "${sql_command}" database_name;
If you really want to use a pipe or a redirection (but I think it make no sense in this case):
$ mysql -u root -p database_name < <(echo "SHOW TABLES") # redirection
$ mysql -u root -p database_name <<< "$(echo "SHOW TABLES")" # another way to use redirection
$ echo "SHOW TABLES"|mysql -u root -p database_name # pipe
My bash script queries a mysql database 3 times and redirects the standard out of each query to a file (3 different files in total with different columns structure ).
I want it to ask for the mysql password as it's important for me not to have the password in the script or on disk.
How can I include all queries and stdout redirection in the same mysql session in order to avoid asking for the password 3 times?
This is what I have now:
#!/bin/bash
mysql -h database.com -u user -p -e "USE database; mysql query1" > file1
mysql -h database.com -u user -p -e "USE database; mysql query2" > file2
mysql -h database.com -u user -p -e "USE database; mysql query3" > file3
You could use tee and notee commands and write a single query file, say queries.sql and invoke it in a single shot:
use database
tee file1
query1
notee
tee file2
query2
notee
tee file3
query3
notee
Then invoke it:
mysql -h database.com -u user -p -e "source queries.sql" > /dev/null
Related:
What is the equivalent of the spool command in mysql
How can I run an SQL script in MySQL?
You could use bash to prompt for the password, and then supply it to each of the mysql commands:
#!/bin/bash
echo "enter the password for MySQL:"
read -s PASSWD
mysql -h database.com -u user -p$PASSWD -e "USE database; mysql query1" > file1
mysql -h database.com -u user -p$PASSWD -e "USE database; mysql query2" > file2
mysql -h database.com -u user -p$PASSWD -e "USE database; mysql query3" > file3
Here is a POSIX-compliant version of silent prompting for non-bash shells:
stty -echo
printf "enter the password for MySQL:"
read PASSWD
stty echo
printf "\n"
$ mysql -h foobar.com -u user -p salesdb -e "SET #arg='date';" < "myscript.sql"
did not work. I wonder if using -e and < "myscript.sql" at the same time is allowed at all?
$ echo "SET #arg='date';" > input_script.sql
$ cat myscript.sql >> input_script.sql
$ mysql -h foobar.com -u user -p salesdb < input_script.sql
probably won't work at the same time, but you don't need that...
(echo "SET #arg='date';"; cat "myscript.sql") |
mysql -h foobar.com -u user -p salesdb
Found on http://www.cyberciti.biz/faq/using-mysql-in-shell-scripts/
#!/bin/bash
mysql dbnane<<EOFMYSQL
SELECT * from table;
EOFMYSQL