Bash: How to invoke command and store the result in a variable? - mysql

Basically I want to be able to invoke a given command, in this case mysql -uanon -ppwd -db mydb -e "select count(*) from table1". And then take this commands result (the count on that table) and place it in a variable in bash script. What is the simplest way to achieve this?

You most likely want to use batch mode (-B) and disable column names (--disable-column-names) for non-interactive mysql output:
out=$(mysql -B -db mydb -uanon -ppwd --disable-column-names -e "select count(*) from table1";)

$ A=$(mysql -uanon -ppwd -db mydb -e "select count(*) from table1")
$ echo $A
In other words, use the $() syntax.

Related

Why is this bash variable blank when taking output from mysql?

I am trying to take the output from a MySQL query in bash and use it in a bash variable, but it keeps coming up blank when used in the script, but works perfectly from the terminal. What's wrong here?
I've tried changing the way the statement is written and changing the name of the variable just in case it was somehow reserved. I've also done a significant amount of searching but it turns out if you but 'bash', 'blank', and 'variable' in the search it usually comes up with some version of how to test for blank variables which I already know how to do.
tempo=$(mysql -u "$dbuser" -p"$dbpass" -D "$database" -t -s -r -N -B -e "select user from example where user='$temp' > 0;")
printf "the output should be: $tempo" # This is a test statement
The end result should be that the $tempo variable should either contain a user name from the database or be blank if there isn't one.
I think there is some error with your sql statement at user = '$temp' > 0.
But to get the result from MySql you have to redirect the standard error (stderr) to the standard output (stdout), you should use 2>&1.
Most probably you will run into MySql error but try running this on terminal.
tempo=$((mysql -u "$dbuser" -p"$dbpass" -D "$database" -t -s -r -N -B -e "select user from example where user='$temp' > 0;") 2>&1)
The solution was to echo the result of the sql query like this:
tempo=$(echo $(mysql -u "$dbuser" -p"$dbpass" -D "$database" -s -N -B -e "select user from example where user='$username' > 0;"))
Now I'm left with logic issues but I think I can handle that.

Exporting views from Mysql using Docker commmand

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!

connect to mysql db and execute query and export result to variable - bash script

I want to connect to mysql databse and execute some queries and export its result to a varibale, and do all of these need to be done entirely by bash script
I have a snippet code but does not work.
#!/bin/bash
BASEDIR=$(dirname $0)
cd $BASEDIR
mysqlUser=n_userdb
mysqlPass=d2FVR0NA3
mysqlDb=n_datadb
result=$(mysql -u $mysqlUser -p$mysqlPass -D $mysqlDb -e "select * from confs limit 1")
echo "${result}" >> a.txt
whats the problem ?
The issue was resolved in the chat by using the correct password.
If you further want to get only the data, use mysql with -NB (or --skip-column-names and --batch).
Also, the script needs to quote the variable expansions, or there will be issues with usernames/passwords containing characters that are special to the shell. Additionally, uppercase variable names are usually reserved for system variables.
#!/bin/sh
basedir=$(dirname "$0")
mysqlUser='n_userdb'
mysqlPass='d2FVR0NA3'
mysqlDb='n_datadb'
cd "$basedir" &&
mysql -NB -u "$mysqlUser" -p"$mysqlPass" -D "$mysqlDb" \
-e 'select * from confs limit 1' >a.txt 2>a-err.txt
Ideally though, you'd use a my.cnf file to configure the username and password.
See e.g.
MySQL Utilities - ~/.my.cnf option file
mysql .my.cnf not reading credentials properly?
Do this:
result=$(mysql -u $mysqlUser -p$mysqlPass -D $mysqlDb -e "select * from confs limit 1" | grep '^\|' | tail -1)
The $() statement of Bash has trouble handling variables which contain multiple lines so the above hack greps only the interesting part: the data

Passing a MySQL "SELECT" query to a shell variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 years ago.
Lets say I have a query first that inserts certain values in a table and the next query is to display the maximum value of one of the columns and then store that in a variable. I then need to display that variable such that it shows the max value.
For example:
sudo mysql -u$mysql_user -p$mysql_pwd -h $mysql_host --database $db_name -e "INSERT INTO service_status_batch VALUES ();"
batch_id= sudo mysql -u$mysql_user -p$mysql_pwd -h $mysql_host --database $db_name -e "SELECT MAX(id) as maxid FROM service_status_batch;"
echo "Value of the id is:" $batch_id
This echo command should then show the value of the variable. However it ends up showing me the value of the select query in the form of a table and not the value of the variable.
Is there a particular way to assign the query value to a variable in shell script?
I have attached the select query value that it shows.
Use -s and -N options with mysql command like this.
sudo mysql -u$mysql_user -p$mysql_pwd -h $mysql_host --database $db_name -e "INSERT INTO service_status_batch VALUES ();"
batch_id=`sudo mysql -u$mysql_user -p$mysql_pwd -h $mysql_host --database $db_name -s -N -e "SELECT MAX(id) as maxid FROM service_status_batch;"`
echo "Value of the id is:" $batch_id
Refer the details for -s and -N :
--silent, -s
Silent mode. Produce less output. This option can be given multiple
times to produce less and less output.
This option results in nontabular output format and escaping of
special characters. Escaping may be disabled by using raw mode; see
the description for the --raw option.
--skip-column-names, -N
Do not write column names in results.
EDIT3: Bad explanation - I was trying to show how to get the value considering it could be used as necessary:
sudo echo $(echo "SELECT MAX(id) as maxid FROM service_status_batch" | mysql dbnamehere -uUser -pPassword)
EDIT1: variable version obviously
EDIT2: corrected variable assignment by using shellcheck.net as suggested. thanks.
EDIT3: one last edit to add sudo right before mysql command as it won't work without it for users other than root.
batch_id=$(echo "SELECT MAX(id) as maxid FROM service_status_batch" | sudo mysql dbnamehere -uUser -pPassword)

A better way to execute multiple MySQL commands using shell script

I would like to write a *.sh script to execute multiple MySQL commands.
Currently, what I can do is something like the following
mysql -h$host -u$user -p$password -e "drop database $dbname;"
mysql -h$host -u$user -p$password -e "create database $dbname;"
mysql -h$host -u$user -p$password -e "another MySQL command"
...
Is there a way to avoid typing mysql -h$host -u$user -p$password -e every time I want to execute a MySQL command?
I think you can execute MySQL statements from a text file, for example
here is the cmds.txt file which contains MySQL commands:
select colA from TableA;
select colB from TableB;
select colC from TableC;
To execute them using shell script, type
mysql -h$host -u$user -p$password db_dbname < cmds.txt
This way, you separate your MySQL commands from your shell script.
You may want your script to display progress information to you. For this you can invoke mysql with "--verbose" option.
For more information, see https://dev.mysql.com/doc/refman/5.6/en/mysql-batch-commands.html
You can use a single multiquery:
mysql -h$host -u$user -p$password -e "drop database $dbname;create database $dbname;another MySQL command;"
Simply write all your queries seperated by ;. They will be run one after the other.
Note that you can also use a HERE doc to have the queries within the same script:
mysql -h$host -u$user -p$password db_dbname <<'EOF'
select colA from TableA;
select colB from TableB;
select colC from TableC;
EOF
Note that I've used 'EOF' rather than EOF in the first line in order to prevent the contents of the script to disable parameter substitution (especially the ` can be problematic)
Also note that there should not be any whitespace before the final EOF (except if you use <<- rather than << -- in that case leading tab characters are stripped):
mysql -h$host -u$user -p$password db_dbname <<- 'EOF'
↠select colA from TableA;
↠select colB from TableB;
↠select colC from TableC;
↠EOF
(Replace the ↠ with a tab character).
For more info on the HERE doc syntax, see the bash documentation.
There are several ways, in linux you have:
From the mysql cli:
mysql> source mycmds.sql
Using Pipes:
echo "SELECT ..; INSERT ..;" | mysql ...
Executing commands from a file using pipes or redirection:
cat file.sql | mysql ... OR mysql .. < file.sql
Different from other answers that reduce repetition, but
there are ways to reduce options(user, host ... -u, -p, -h ...) from each line command
2 ways i know.
1. use my.cnf file
you can store your user information in option files (e.g ~/.my.cnf or etc)
[client]
user=your_username
password=your_password
# database=database_name
then you can just run mysql command with one option -e and query
mysql -e "drop database $dbname;"
mysql -e "create database $dbname;"
mysql -e "another MySQL command"
2. use mysql_config_editor
you can save login informations with mysql_config_editor
mysql_config_editor set --login-path=mypath1 --host=localhost --user=root --password
then run command just with login-path option
mysql --login-path=mypath1 -e "drop database $dbname;"
mysql --login-path=mypath1 -e "create database $dbname;"
mysql --login-path=mypath1 -e "another MySQL command"