does anyone know if there's a script out there that will create a mysql database automatically instead of having to go into cpanel and create a database, username and password manually?

thanks

share|improve this question

4 Answers

You cannot create a username and password unless you log in yourself with a user with a higher level. That's usually the root. Creating database is a breeze after using the username and password with sufficient privileges.

<?php

    $dsn = $dsn = "mysql:host=localhost";
    $pdo = new PDO($dsn,"root","");

    //Creation of user "user_name"
    $pdo->query("CREATE USER 'user_name'@'%' IDENTIFIED BY 'pass_word';");
    //Creation of database "new_db"
    $pdo->query("CREATE DATABASE `new_db`;");
    //Adding all privileges on our newly created database
    $pdo->query("GRANT ALL PRIVILEGES on `new_db`.* TO 'user_name'@'%';");

?>

In this script, I assumed your root user is called "root" and its password is empty if that's not the case, change line 4 to match.

share|improve this answer
i think I will already be logged into the server. I plan to install the script with ftp to my website and server, then i would like to create an install.php page to make it easy to create the database. I might not be explaining things correctly. – Jason Aug 5 '11 at 18:32
Your script should not create the database (imho), but the user who wishes to install your application should. Take note of all the larger CMSs and applications, they ask you, the user, for the database name, username and password. However, if you already have a PDO object opened and connected at the time of running the script, feel free to skip the first 2 lines. – Madara Uchiha Aug 5 '11 at 18:49

you cannot create new user without logging in.

You can create database with root account or user with privileges

share|improve this answer

When you are connected with the servers root-account (or account with db/user-adding permissions) you can create databases and users.

Adding a user:

<?php
mysql_query("CREATE USER 'USER'@'%' IDENTIFIED BY 'PASSWORD';");
mysql_query("GRANT USAGE ON * . * TO 'USER'@'%' IDENTIFIED BY 'PASSWORD' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;");
?>

Creating a database:

<?php
mysql_query("CREATE DATABASE 'DATABASE';");
?>

Give user permissions on database:

<?php
mysql_query("GRANT ALL PRIVILEGES ON `DATABASE` . * TO 'USER'@'%';");
?>
share|improve this answer
-1 for answering with mysql_* functions. They are deprecated, do not use them, and definitely don't tell others to use them. – Madara Uchiha Aug 5 '11 at 18:07

You definitely can! You can use PHP.

First you need to connect to the database. Then you need to run a SQL query on that database that creates the database. (which is essentially what cpanel does)

EDIT: Updated since my_sql is deprecated

Heres a stackoverflow post on how to do it...

Can I create a database using PDO in PHP

share|improve this answer
1  
Same as the answer above me. Don't use mysql_* functions. And if I could I'd give you another one for using examples from w3schools. see w3fools.com – Madara Uchiha Aug 5 '11 at 18:08
1  
-1 for w3schools :) – vascowhite Aug 5 '11 at 18:13
Well done, but now your answer does not answer the question, it does not tell how to create users nor how to create databases. – Madara Uchiha Aug 5 '11 at 18:19
I was working on it but I'll just link to this one. I couldn't put something together quick enough. – thepristinedesign Aug 5 '11 at 18:21

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.