LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   create user and database in the same time script (https://www.linuxquestions.org/questions/linux-general-1/create-user-and-database-in-the-same-time-script-801611/)

asdasdqwe 04-12-2010 03:09 PM

create user and database in the same time script
 
Hello, i have made the following script:

Code:

#!/bin/bash
clear

######################
# Create User        #
######################

echo -e "Username: \c"
read username

useradd -m $username;
passwd $username;

chmod 751 /home/$username;

clear
echo "User $username added..."

exit

but i want in the same time to create a database and a user on mysql with the same username and password. can someone help me?

rweaver 04-12-2010 03:22 PM

You could do something like this... although I don't recommend this, it's insecure to store the password, etc.etc.etc.

Code:

echo -e "Username: \c"
read username
echo -e "Password: \c"
read password

enc_p=$(perl -e 'print crypt($ARGV[0], "arr")' $password)
useradd -m $username -p $enc_p
chown -R user:group /home/$username
chmod 751 /home/$username

mysql -uroot --password=$(cat /root/.mysqlrc) -e "CREATE USER \'$username\'\@\'localhost\' IDENTIFIED BY \'$password\'\;"
mysql -uroot --password=$(cat /root/.mysqlrc) -e "CREATE DATABASE $username;"
mysql -uroot --password=$(cat /root/.mysqlrc) -e "GRANT ALL PRIVILEGES ON $username.* TO \'$username\'\@\'localhost\' WITH GRANT OPTION\;"

Should about do it, although I've not taken time to actually test it. Alternately you could generate a template to use and import the sql, etc.

asdasdqwe 04-12-2010 04:16 PM

it gives this:

Quote:

ERROR at line 1: Unknown command '\''.
ERROR at line 1: Unknown command '\''.
and can you write me the opposite script please? (to delete)

TB0ne 04-12-2010 04:39 PM

Quote:

Originally Posted by asdasdqwe (Post 3933333)
it gives this:
and can you write me the opposite script please? (to delete)

You do realize that you are going to have to debug/write your own code?? You were given a pretty good head start, but don't even want to debug it, or do any research to complete your task???

Show us what you've written, and how you've tried to do it, and where you're getting stuck, and we can help.

asdasdqwe 04-12-2010 04:46 PM

told you where i got stuck in rweavers script.. what's wrong?

asdasdqwe 04-12-2010 05:03 PM

ok that's working:

Quote:

mysql -uroot --password=$(cat /root/.mysqlrc) -e "CREATE USER '$username'@'localhost' IDENTIFIED BY '$password';"
mysql -uroot --password=$(cat /root/.mysqlrc) -e "CREATE DATABASE $username;"
mysql -uroot --password=$(cat /root/.mysqlrc) -e "GRANT ALL PRIVILEGES ON $username.* TO $username@localhost WITH GRANT OPTION;"


All times are GMT -5. The time now is 10:01 AM.