Creating new MySQL users with PHP?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
johnhaze
Forum Newbie
Posts: 4
Joined: Tue Oct 11, 2005 12:22 am
Location: New York

Creating new MySQL users with PHP?

Post by johnhaze »

I'm familiar with creating new databases with PHP, but can I create new users as well?
I scoped out php.net but couldn't find anything relevant. Google wasn't much help either

I know it's possible because all the popular web hosting backpanels let you do it (cpanel, plesk..). The question is what are they doing it with? Are they using a web language or executing batch files? What would be the simplest way to dynamically create new MySQL user accounts?


Thanks in advance.

- John
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you have access rights to it, the "mysql" database can be loaded, which has a table "user" where you can insert new users. However, if you do not have access to that database, then you very likely will not be able to do it without the root (or admin) level credentials.
johnhaze
Forum Newbie
Posts: 4
Joined: Tue Oct 11, 2005 12:22 am
Location: New York

Post by johnhaze »

Thanks feyd, I do have access to the mysql database because it's my server.

Just to clarify what you suggested, I will be able to add users by inserting new values into a "user" table in the "mysql" database?

edit: I am looking at it now, and the passwords seem to be encrypted. Is that md5 or SHA-1 encryption?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

PASSWORD()
johnhaze
Forum Newbie
Posts: 4
Joined: Tue Oct 11, 2005 12:22 am
Location: New York

Post by johnhaze »

feyd wrote:PASSWORD()
what kind of encryption is that? can I do the same through PHP?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I'm familiar with creating new databases with PHP, but can I create new users as well?
http://dev.mysql.com/doc/mysql/en/grant.html

Code: Select all

mysql_query("GRANT ALL ON *.* TO '" . mysql_real_escape_string($user) . "' IDENTIFIED BY '" . mysql_real_escape_string($password) . "'");
johnhaze
Forum Newbie
Posts: 4
Joined: Tue Oct 11, 2005 12:22 am
Location: New York

Post by johnhaze »

thanks, I got it to work.

but what privileges should I disable, besides GRANT?
Post Reply