LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 04-12-2010, 03:09 PM   #1
asdasdqwe
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Rep: Reputation: 0
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?
 
Old 04-12-2010, 03:22 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
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.
 
1 members found this post helpful.
Old 04-12-2010, 04:16 PM   #3
asdasdqwe
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0
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)

Last edited by asdasdqwe; 04-12-2010 at 04:17 PM.
 
Old 04-12-2010, 04:39 PM   #4
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,241

Rep: Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475
Quote:
Originally Posted by asdasdqwe View Post
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.
 
1 members found this post helpful.
Old 04-12-2010, 04:46 PM   #5
asdasdqwe
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0
told you where i got stuck in rweavers script.. what's wrong?
 
Old 04-12-2010, 05:03 PM   #6
asdasdqwe
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0
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;"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create backup MYSQL Script to backup my database for every 1hour RMLinux Linux - Newbie 3 11-20-2008 10:13 AM
How to create an executable script that starts execution at a specific date and time? jtag Linux - General 3 08-12-2008 08:36 PM
Create mysql user for backup of database timgerr Linux - Server 2 01-04-2008 10:44 AM
MySQL - command - create user/single database dmedici Linux - Software 3 06-21-2004 03:17 PM
Create user script kelper Linux - Software 1 06-24-2003 09:32 PM


All times are GMT -5. The time now is 08:36 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration