sql statement in a bash script "command not found" - mysql

I've got a bash script that runs a series of sql statements:
#!/bin/bash
mysql -u root -p << QUERY_INPUT
CREATE DATABASE dba_first;
CREATE DATABASE dba_second;
CREATE DATABASE dba_third;
GRANT ALL PRIVILEGES ON `dba%`.* TO `dbuser`#`localhost`;
QUERY_INPUT
When I run the script, I get: ./quick.sh: line 20: dba%: command not found
Any suggestions please?

Answered by a nice person on IRC; have to escape the `
GRANT ALL PRIVILEGES ON \`dba%\`.* TO \`dbuser\`#\`localhost\`;

Related

Running a single mysql command with the 'runuser' command

For some strange reason, I can't find a way to make the runuser command work. I know it is possible to achieve this with sudo -u mysql mysql -e "$DB_SETUP but since I want to do this inside a script that already runs with sudo I find this not very pretty.
Here is what I am trying to do:
DB_SETUP="CREATE USER IF NOT EXISTS $DB_USER#$BASEURL IDENTIFIED BY '$DB_PASSWORD';CREATE DATABASE IF NOT EXISTS $DB_NAME;GRANT ALL PRIVILEGES ON $DB_NAME.* TO $DB_USER#$BASEURL IDENTIFIED BY '$DB_PASSWORD';FLUSH PRIVILEGES;"
sudo runuser -u mysql "mysql -e \"$DB_SETUP\"" # does not work
It gives me this error:
runuser: failed to execute mysql -e "CREATE USER IF NOT EXISTS db_user#baseurl IDENTIFIED BY 'db_password';CREATE DATABASE IF NOT EXISTS db_name;GRANT ALL PRIVILEGES ON db_name.* TO db_user#baseurl IDENTIFIED BY 'password';": No such file or directory
As commented above, I got it working with:
sudo runuser -u mysql mysql <<< $DB_SETUP
No quotation marks at all!

How to run custom mysql script for Laravel app in test pipeline in CodeShip Basic?

Relative newbie to PhpUnit and testing in general. We do not use migrations in our project, but have a couple of scripts that I need to run in order to set up the database for testing. How can I run mysql scripts from the project in the test pipeline? I also need to create a new database with a specific name before running those scripts.
Thanks!
The commands that you use on your local machine are the same commands you can run in CodeShip Basic. CodeShip Basic is just a build machine with Ubuntu Bionic and it will run through the setup, test, and deploy commands as if you were entering each line into your CLI. :)
We also have some documentation about mysql: https://documentation.codeship.com/basic/databases/mysql/
OK, after some digging I found out how to do all this. Below is the script I used for testing with a new mysql schema created with specific user from a sql script. Hope this helps someone in the future.
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE USER 'myuser'#'%' IDENTIFIED BY 'testpassword';"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE SCHEMA myschemaname;"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "GRANT ALL PRIVILEGES ON *.* TO 'myuser'#'%' IDENTIFIED BY 'testpassword';"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD myschemaname < ./app/Ship/Migrations/1of2-schema.sql
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD myschemaname < ./app/Ship/Migrations/2of2-seed.sql
php artisan passport:install
./vendor/bin/phpunit
$MYSQL_USER and $MYSQL_PASSWORD are replaced by Codeship - these are the env variables for the user and password for the mysql that exists in the build container.
the -e switch on the mysql call runs the script given and exits. Had to do it this way since I couldn't interact with the mysql client.

Inserting user defined variable in mysql CREATE USER

I would like to create a shell script to run in docker CLI and create a MySQL user with the host IP passed as a command line variable.
So with my script it would be ./create_user.sh 172.17.0.1
I tried starting with inserting variable only in the sql statements part and using something like this:
#!/bin/sh
docker exec -i atb-mariadb bash <<'EOF'
mysql -uroot -pmypass
set #ip='172.17.0.1';
CREATE USER 'exporter'##ip IDENTIFIED BY 'mypass';
GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'exporter'#'172.17.0.1';
GRANT SELECT ON performance_schema.* TO 'exporter'#'172.17.0.1';
SELECT user, host FROM mysql.user;
exit
EOF
This results in syntax error, along with some others i tried :
CREATE USER CONCAT_WS('exporter','#',#ip) IDENTIFIED BY 'mypass';
CREATE USER 'exporter'#CONCAT_WS('',#ip) IDENTIFIED BY 'mypass';
This is of course only the variable within the sql statements part of the script. Using a variable in the overall shell script and passing that into the sql bash is a problem that I have not even been able to come to yet.
Thanks in advance for any help!
UPDATED
I tried Raymond Nijland's solution and it worked for the sql variable part. However, trying to pass the variable value through the command line is still failing for the following script:
#!/bin/sh
echo script received $1
docker exec -e ipa=$1 -i atb-mariadb bash <<'EOF'
echo exec received $ipa
mysql -uroot -pmypass -e "
SET #ip='${ipa}';
SET #createUser = 'CONCAT("CREATE USER exporter#",#ip,"IDENTIFIED BY'mypass'")';
PREPARE smtpCreateUser FROM #createUser;
EXECUTE smtpCreateUser;";
exit
EOF
with the output
$ ./create_user.sh 172.18.0.5
script received 172.18.0.5
exec received 172.18.0.5
mysql Ver 15.1 Distrib 10.2.9-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Usage: mysql [OPTIONS] [database]
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
The following groups are read: mysql client client-server client-mariadb
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
......and so on
I understad the usual approach is to run a separate .sql script file with the -e handle but unfortunately bind-mounting this file into the default mariadb container or creating a custom image are both not possible appraoches given the current requirements.
You should be able to generate dynamic SQL statements with CONCAT, PREPARE and execute them.
Query
SET #ip = '172.17.0.1';
SET #createUser = CONCAT(
"CREATE USER exporter#",#ip, " IDENTIFIED BY 'mypass'"
);
PREPARE smtpCreateUser FROM #createUser;
EXECUTE smtpCreateUser;
You need to grab the variable from the shell before it can be used in the query. I think you are looking for something like (make sure to remove the quotes from EOF):
#!/bin/sh
docker exec -i atb-mariadb bash <<EOF
ip="${1:-127.0.0.1}"
echo mysql -uroot -pmypass
echo CREATE USER 'exporter'#'${ip}' IDENTIFIED BY 'mypass';
GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'exporter'#'172.17.0.1';
GRANT SELECT ON performance_schema.* TO 'exporter'#'172.17.0.1';
SELECT user, host FROM mysql.user;
exit
EOF

creating a bash script to create database and user automatically

Hi I am trying to create a bash script, which take username and database name and password as in arguments and create a database.
mysql -u xxx -pxxxxxx << EOF
CREATE DATABASE '$2';
CREATE USER '$2'#'localhost' IDENTIFIED BY '$3';
GRANT ALL PRIVILEGES ON '$2'.* TO '$2'#'localhost';
EOF
I am getting mysql 1064 error while executing this command.
Any help will be appreciated.
Thanks
mysql -u $user -p$pass << EOF
CREATE DATABASE $2;
GRANT ALL PRIVILEGES ON $2.* TO $2#'localhost' IDENTIFIED BY '$3';
EOF
The answer here did not work for me (got a number of syntax errors, including the same 1064 error as the OP). Posting this in case it helps someone else:
#!/bin/bash
echo "mysql root password:"
read rootpass
if [[ $rootpass ]]; then
# create db
# create user
mysql -uroot -p$rootpass <<EOF
CREATE DATABASE \`${1}\`;
CREATE USER $1#localhost IDENTIFIED BY '$1';
GRANT ALL PRIVILEGES ON \`${1}\`.* TO \`${1}\`#'localhost';
EOF
fi
Seems like some statements required quotes, some escaped, some none at all! :D

Run sql script using batch file from command prompt

D:
cd Tools/MySQL5/bin
mysql -u root mysql
use xyz;
source C:/Users/abc/Desktop/xyz.sql;
\q
When I run the above lines in command prompt it works fine but when I save it as a batch file and run the file it connects to mysql but doesn't perform the sql scripts.
Now what I see is wrong here is that while executing the above commands one by one in your prompt, once you run mysql -u root mysql, you are in the mysql console. So your source command would work there but would not work in your batch since you are not in mysql console while running the batch file.
Solution:
What you can do for this is, instead of using source in mysql you can use
mysql dbname < filename
in your batch file in place of
mysql -u root mysql
use xyz;
source C:/Users/abc/Desktop/xyz.sql;
This link can assist you further if needed
This should work
mysql -u root xyz < C:/Users/abc/Desktop/xyz.sql;
It sources the SQL commands from your file
You could write something like this
mysql -u dbUsername yourDatabase -e "SELECT * FROM table;"
Or to run repeating tasks create a runtasks.bat file, save under the root of your project then write your cmd tasks inside
mysql -u dbUser -e "DROP DATABASE IF EXISTS testDatabase;"
mysql -u dbUser -e "CREATE DATABASE testDatabase;"
php index.php migration latest #runs your migration files
cd application\tests
phpunit
This would work.
mysql.exe -u user_name -p -h _host_ _schema_ -e "select 1 from dual;"
This will also give you output on same command terminal