2

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

2 Answers 2

2
 mysql -u $user -p$pass  << EOF
CREATE DATABASE $2;
GRANT ALL PRIVILEGES ON $2.* TO $2@'localhost' IDENTIFIED BY '$3';
 EOF
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.