How can I insert MySQL in a bash script - mysql

I would like to insert MySQL into my bash script.
How can I insert the following lines into bash code?
This is how I would enter MySQL...
mysql -uroot -p
and use this MySQL code...
GRANT SELECT, INSERT, UPDATE, DELETE
ON example.*
TO 'yourUser'#'localhost'
IDENTIFIED BY 'yourUserPassword';
\q

You don't need to use bash to do this, simply use the -e flag:
mysql -uroot -p<password> -e"GRANT SELECT, INSERT, UPDATE, DELETE ON example.* TO 'yourUser'#'localhost' IDENTIFIED BY 'yourUserPassword'"
Notice that you might have to flush the privileges after running the above:
FLUSH PRIVILEGES;
From the documentation:
Reloads the privileges from the grant tables in the mysql database
To do this from a bash script you create a sh file with the following content:
#!/bin/bash
mysql -uroot -p<password> -e"GRANT SELECT, INSERT, UPDATE, DELETE ON example.* TO 'yourUser'#'localhost' IDENTIFIED BY 'yourUserPassword'"
mysql -uroot -p<password> -e"FLUSH PRIVILEGES"
Don't forget that you must set execute permission on the file:
chmod +x <filename>.sh
Then you should be able to execute the script using:
./<filename>.sh

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 can I escape backticks in gnu make makefile

I have this make file:
createuser:
$(MYSQL) -e " grant SELECT, USAGE on `app\_%`.* to 'user.name'#'%' IDENTIFIED BY '$(USER_PASS)'" && \
$(MYSQL) -e "FLUSH PRIVILEGES;"
This command works fine if I execute it directly in a MySQL query window, but I want to execute it from terminal.
You don't need FLUSH PRIVILEGES with GRANT or CREATE USER
statements
For future compatibility - CREATE USER [IF NOT EXISTS]
before doing the GRANT
Within a Makefile you can escape using \
before both backticks.

How to disable dot replacement in bash?

I want to run MySQL query from bash script to create new user:
mysql -u root -p$dbpass -e "GRANT ALL PRIVILEGES ON appdb_${BUILD}.* TO appuser#localhost IDENTIFIED BY 'somepass'"
Where $BUILD variable is set to a number, for example 15, so final query should look like this:
mysql -u root -p$dbpass -e "GRANT ALL PRIVILEGES ON appdb_15.* TO appuser#localhost IDENTIFIED BY 'somepass'"
But... in directory where script is and is run there is a tar archive with same name as database, so query becomes like this:
mysql -u root -p$dbpass -e "GRANT ALL PRIVILEGES ON appdb_15.tar.gz TO appuser#localhost IDENTIFIED BY 'somepass'"
I guess this happens because there is a match, but changing file name is not an option. Moving script to other directory is not allowed too.
I tried to escape dot with backslashes (appdb_${BUILD}\.*) - nothing changed.
How I can fix this?
There is nothing wrong with your approach and it is working in my case. See below. Can you tell us what is the error that you are getting so that we can help you?
[root#cloud mysql]# ls
appdb_15.tar.gz test.sh
[root#cloud mysql]# cat test.sh
#!/bin/bash
BUILD=15
# First Method
mysql -u root -ptest -e "GRANT ALL PRIVILEGES ON appdb_${BUILD}.* TO appuser#localhost IDENTIFIED BY 'somepass';"
# Second Method
mysql -u root -ptest <<EOF
GRANT ALL PRIVILEGES ON appdb_${BUILD}.* TO appuser1#localhost IDENTIFIED BY 'somepass';
select user, host from mysql.user where user like 'appuser%';
EOF
[root#cloud mysql]# ./test.sh
user host
appuser localhost
appuser1 localhost
EDIT 1:
I forgot to mention that this is definitely not a bash issue.
EDIT 2: Changed the build variable to 15 from 5.

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

Create MySQL DB from Command Line

I am trying to create a batch file to create a MySQL Database. So far, none of the information I am finding is working. Just for testing, this is what I am trying...
C:\>mysql -uroot -ppassword < CREATE DATABASE testdb;
C:\>mysql -uroot -ppassword mysql < CREATE DATABASE testdb;
No matter how I put it, I keep getting the error "The system cannot find the file specified". If I just put...
C:\>mysql -uroot -ppassword
It logs into the MySQL prompt fine. What exactly am I doing wrong?
I agree with the other posters, it's much better to put the schema into a file. However, here's how you can do it on the command line:
mysql -uroot -ppassword -e "CREATE DATABASE testdb"
acess as root user :
mysql -u root -p
it asks for password..enter your password
then
run the create command like this:
mysql> create database database_name;
It's better to write your MySQL inside a file and then import that file. That way, when you need to run it again (reinstalling or migrating) you have a record of the MySQL to run. The code I use for a file like this is as follows, which destroys anything that's already there, and then creates the database and assigns a dedicated user.
# uninstall if anything's already there
GRANT ALL PRIVILEGES ON *.* TO 'username'#'%';
DROP USER 'username'#'%';
DROP DATABASE IF EXISTS `tablename`;
# create the user
CREATE USER 'username'#'%' IDENTIFIED BY 'password';
CREATE DATABASE IF NOT EXISTS `tablename`;
GRANT ALL PRIVILEGES ON `tablename` . * TO 'username'#'%';
Try putting your sql into a text file, like 'createDb.sql' and then run:
mysql -uroot -ppassword < createDb.sql;