Linux Bash, multiple MYSQL commands - mysql

I am trying to figure out how to format a multiple variable request to mysql in a bash script. I have 5 variables to be set in my bash script. Each variable is retrived from a remote DB. I currently have each variable on a separate line with its own separate login.
chatTo=$(mysql -D DB -u user -p'password' -h "$Control" -P 3309 -se "SELECT value FROM configuration WHERE label='chatTo'")
chatFrom=$(mysql -D DB -u user -p'password' -h "$Control" -P 3309 -se "SELECT value FROM configuration WHERE label='chatFrom'")
I am quite sure that there is a more efficient way to do this. I am trying:
mysql -D DB -u user -p'password' -h "$Control" -P 3309 -se << END
chatFrom=$(SELECT value FROM configuration WHERE label='chatFrom');
chatTo=$(SELECT value FROM configuration WHERE label='chatTo');
END
This is not working. I imagine it is a formating issue. Or can I even assign multiple variables like this? Seems to me that limiting the login logout processes is more secure.

You should use EOF instead of END
Example:
mysql -uroot -proot SOMEDATABASE << EOF
insert into TABLENAME (name,lastname,address,telephone) values $_name , '$_lastname', '$_address' , '$_tel';
EOF
As for your code, just use ; in the end of each line:
mysql -uroot -proot SOMEDATABASE << EOF
SELECT value FROM configuration WHERE label='chatTo';
SELECT value FROM configuration WHERE label='chatFrom';
EOF

Related

Expand selected variables within here-document while invoking Bash through SSH

I don't have remote access to a MySQL server, so I am trying to do it via an SSH session.
It partly works, but not correctly.
sshpass -p $password ssh user#$IP /bin/bash << EOF
mysql -N -uroot -ppassword test -e "select id from client where user ='$user'"
EOF
This will show the result of the select statement, but I'd like to be able to use that result in another echo statement.
eg:
The user ID is: xxxxx
I tried to assign the output to a variable using:
sshpass -p $password ssh user#$IP /bin/bash << EOF
res=$(mysql -N -uroot -ppassword test -e "select id from client where user ='$user'")
echo $res
EOF
But that results in:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysql/mysql.sock' (2)
If I quote EOF like 'EOF' then I can get the result into res but I lose the ability to use $user
Is there anyway to do this so I can use my variable in the heredoc and get the result of the MySQL query to a new variable ?
If I quote EOF like 'EOF' then I can get the result in to res but I lose the ability to use $user
You can pass $user to bash as a positional parameter and still have the quoted EOF and its advantages. E.g:
sshpass -p "$password" ssh "user#$IP" /bin/bash -s "$user" << 'EOF'
res=$(mysql -N -uroot -ppassword test -e "select id from client where user ='$1'")
echo $res
EOF
Bash manual describes the -s option as follows.
If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell or when reading input through a pipe.

Redirecting stdout to mysql in bash

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

avoid asking second time mysql root password (bash)

In a simple script like this one:
set -x
# Check if db exists, if not we make it, make user, give privileges
if ! mysql -u root -p -e "use $db" 2>/dev/null; then
c1="CREATE DATABASE $db"
c2="GRANT ALL PRIVILEGES ON ${db}.* to '$username'#'localhost' IDENTIFIED BY '$password'"
c3="FLUSH PRIVILEGES"
mysql -u root -p -e "$c1; $c2; $c3"
else
echo 'DATABASE ExISTS, ABORTING'; exit $DB_EXISTS
fi
I am asked each time, bash sees mysql command, for my root credentials.
Is there a way to avoid that, so that once entered the root password, all
additional mysql commands execute seamlessly?
Try looking into adding password to ~/.my.cnf
[client]
user = root
password = XXXXXXXX
Check out :
How to execute a MySQL command from a shell script?
Specifying the --password argument
mysql -u root --password=my_mysql_pass db_name
Safer using a bash variable
mysql -u root --password=$MYSQL_PASS db_name

unable to connect the mysql database using shell script

Am facing problem to connect the MySQL DB from shell script. Please find the below snippet i have written for connecting the MySQL data base. please suggest on this.
My shell Script:
#!bin/bash
Query="select * from Main"
MySQL -u root -p '!!root!!' -e kpi << EOF
$Query;
EOF
Please check the above code and suggest me how to connect the DB.
I think it should be
-pThePassword
So you should delete the space between -p and the pass. Also you should not use an apostrophe (except it is part of the pass itself. Use a backslash to escape special characters.
Second: *nix systems are case sensitive, please try mysql instead of MySQL
Update
You could also try to type your password into a file and read it with your script
mysql -u root -p`cat /tmp/pass` -e "SHOW DATABASES"
The file /tmp/pass should contain your password without any newline char at the end.
Update 2
Your Script is wrong.
You can either use mysql ... -e SELECT * FROM TABLE or mysql ... << EOF (without -e). You should not mix them.
Don't forget to pass the databasename as a parameter (or with use databasename;) in the sql
Don't forget to add a ; after every sql command, if you have multiple statements
Method One:
mysql -u root -ppassword databasename -e "SELECT * FROM main"
Method Two:
mysql -u root -ppassword databasename << EOF
SELECT * FROM main
EOF
Method Three:
mysql -u root -ppassword << EOF
USE databasename;
SELECT * FROM main;
EOF
mysql --user=root --password=xxxxxx -e "source dbscript.sql"
This should work for Windows and Linux.
If the password content contains a ! (Exclamation mark) you should add a \ (backslash) in front of it.

Shell Script to update multiple databases

This is what I currently have:
#!/bin/bash
# Shell script to backup MySql database
MyUSER="root"
MyPASS="password123"
MYSQL="$mysql"
MYSQLDUMP="$mysqldump"
# Store list of databases
DBS=""
# Get all database list first
DBS="$($MYSQL -u $MyUSER -h -p$MyPASS -Bse 'show databases')"
for db in $DBS
do
The problem i have is the 'do' bit,
I need to write this into the shell.
After getting all the DB names do the following:
updated user set password="passowrd" where id = 999;
Can anyone assist?
try
for db in $DBS
do
$MYSQL -u $MyUSER -h -p$MyPASS -Bse "update $db.password='password' whereid =999;'
end
as you can easily access a table by databasename.tablename in mysql.