mysql bash escaping my single quotes - mysql

I am trying to execute a mysql query from a shell script on Ubuntu 16. Let's call the variable $thing.
Shell script
thing=stuff.whatever
mysql -h host -u superuser -ppassword123 --database bestdbever -e "update table set column1=1 where column2='$thing';"
where gets sent is this....
mysql -h host -u superuser -ppassword123 --database bestdbever -e "update table set column1=1 where column2='\''stuff.whatever'\'';'
When escaping the single quote, it sends
mysql -h host -u superuser -ppassword123 --database bestdbever -e "update table set column1=1 where column2=\'\''stuff.whatever\'\'';'
and then I cry.

Related

Excluding tables from "show tables" in a scripted mysqldump

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.

Bash script with multiple queries and redirection in the same mysql session

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"

Linux Bash, multiple MYSQL commands

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

howto to create mysql database from fabric dynamically

Is it possible to create mysql database from fabric dynamically.
This seems like it gets stuck at the password prompt
run('mysql -u %s -p %s -h %s ' % (env.mysqluser, env.mysqlpassword, env.mysqlhost), pty=True)
run('CREATE DATABASE %s; ' % (dataname), pty=True)
run('exit', pty=True)
There's a better way of doing this using mysqladmin:
run('mysqladmin -u %s -p%s create %s' % (user, password, dbname))
Try instead
run('echo "CREATE DATABASE %s;"|mysql --batch --user=%s --password=%s --host=%s' % (dataname, env.mysqluser, env.mysqlpassword, env.mysqlhost), pty=True)
I use the following one liner via command line
mysql -uroot -prootpassword -e "CREATE DATABASE dbname";
key is the -e switch.
if you like to have bash script with variables in db/user/pass and run it as ./myscript then
#!/bin/bash
DB="mydb"
USER="user1"
PASS="pass_bla"
mysql -uroot -prootpassword -e "CREATE DATABASE $DB CHARACTER SET utf8 COLLATE utf8_general_ci";
mysql -uroot -prootpassword -e "CREATE USER $USER#'127.0.0.1' IDENTIFIED BY '$PASS'";
mysql -uroot -prootpassword -e "GRANT SELECT, INSERT, UPDATE ON $DB.* TO '$USER'#'127.0.0.1'";

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.