Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mysql adduser/db script

Status
Not open for further replies.

axman505

Technical User
Jun 20, 2001
489
US
I have an adduser script that will add a list of any given users to a redhat 9 box. I am trying it write a script that can be called from within the add user script to add a database and database username for each user.

I cannot figure out how to pass the variables and be able to output the commands to mysql. The mysql commands i want to run are:

CREATE DATABASE test;

GRANT USAGE ON * . * TO test_user@localhost IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON test` . * TO test_user@localhost;

Subbing the proper username and databse name.

So i need a script that you can run by:
bash# ./mysqladd.script username databasename userpassword

Any hints on how to accomplish this?

Thanks,
 
I recommend that you write a program which can provide the username subtitution and which can either create a text file you can pass to the mysql app or pass the queries directly to MySQL.

You're on a Linux box, so you will certainly have perl available. You might even be able to produce the user substitution into a file via a bash script.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Something like
Code:
#!/bin/bash
test $# -ne 3 && echo "Usage: $0 username database password" && exit 1

DBUSER=$1
DBNAME=$2
DBPASS=$3
mysql -uroot -p <<-EOF
CREATE DATABASE $DBNAME;
GRANT USAGE ON *.* TO $DBUSER@localhost IDENTIFIED BY '$DBPASS';
GRANT ALL PRIVILEGES ON $DBNAME.* TO $DBUSER@localhost;
EOF

//Daniel
 
Thanks, is the last EOF needed??

Because it spits out a mysql error.
 
The EOF should be treated by the shell, and not received by the MySQL client at all.

//Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top