Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

Create MySQL DB User / Database

I want to set up a php program that will create a MySQL database & a MySql database user for that database.

I know how to do this in phpMyadmin; I have done it a hundred times.

For reasons I don't need to explain here, I want a php script, like create_db_user.php that will create a database & a user with a password. Of course I want that user to have privileges too, I'll cross that bridge later.

Everything I find on the web that shows how to create a database assumes the existence of a User and a password, but I can find NO WAY to create a user & a password without a database. So it's like the chicken & the egg.

See attached. I ran this, it did nothing and output this: "Notice: Array to string conversion in C:\xampp\htdocs\seba\create_db_user.php on line 8
Array"

I have NO CLUE what that means.

I would like CLEAR & CONCISE steps on how to do this from within a php program.

Thanks
create-db-user.php
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

phpMyAdmin uses either 'root' or a highly privileged MySQL user to do admin activities.  You can NOT create anything without a valid MySQL user.  And to use 'root', you normally have to be running on the server where the MySQL server is running.  'root' is not normally allowed from remote access.

Your code does not have a connection function along with proper username and password to access the MySQL server.  Note also that if you create a user with 'localhost', they will not be able to connect remotely to the MySQL server.
Avatar of Richard Korts

ASKER

Dave,

I am running XAMPP, which is a Apache / MySQL / php environment for Windows.

In this instance, I do NOT want to connect to a remote host, just localhost.

So can I use user 'root' to do this? How do I know what to use for a password?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dave,

I accidentally found out how to change the root password, which I did.

Then I ran the attached script (in the script attached I removed the "real" root password).

It created the database & the user. Yea!!

But on the user, note the attached jpg users_overview, it assigned host % rather than localhost???

What does % mean?

Thanks
create-db-user-np.php
users-overview.jpg
Dave,

I think I know why the user was setup as it was, I was granting privileges to a user without first creating that user.

So I think I see it all now.

Thanks!
As usual, it did what you told it to.  You should get rid of the 'Any' users.  They don't have a password and 'Any'one can connect by using them.