creating a bash script to create database and user automatically - mysql

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

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 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.

sql statement in a bash script "command not found"

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\`;

bash check mysql connect

I'm writing a bash script to do some operations against a database on my debian squeeze Server.
I have noticed that if I enter a wrong password for root, the prompt will be closed and I won't be asked to try again... that's not very convenient!
So I was trying to create a loop that attempts to connect to MYSQL and save the password for later if successful.
I tried this, but it doesn't work.
Instead, I receive this error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
read -s -p "Enter MYSQL root password: " mysqlRootPassword
while [[ -n `mysql -u root -p$mysqlRootPassword` ]]; do
read -p "Can't connect, please retry: " mysqlRootPassword
done
I am not very experienced in bash scripting, any help would be awesome!
I don't think you need the [[ -n backtic ... ]]; test nested like that. Try:
read -s -p "Enter MYSQL root password: " mysqlRootPassword
while ! mysql -u root -p$mysqlRootPassword -e ";" ; do
read -s -p "Can't connect, please retry: " mysqlRootPassword
done
while evaluates any command group upto a closing ; do and checks the return code of last command executed to determine if the loop should be executed. Because you are looking for a failure, you have to precede the test with a logical NOT (!) OR you can use the syntactic equivalent, i.e.
until mysql -u root -p$mysqlRootPassword -e ";" ; do
read -s -p "Can't connect, please retry: " mysqlRootPassword
done
which you can think of as 'until mysql works correctly, keep trying to get the right password'.
Unfortunately, I don't have access to a mysql installation, so this is untested.
I hope this helps.

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;