2

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

Jason
  • 79
  • 1
  • 1
  • 6

6 Answers6

7

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.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • 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 05 '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's Ghost Aug 05 '11 at 18:49
  • Cpanel does not allow you to do this – Lan Jan 16 '14 at 23:27
  • @Lan: Why should I care about cPanel? The question didn't say anything about cPanel. – Madara's Ghost Jan 17 '14 at 09:29
  • why two variable ? `($dsn = $dsn)` – Shafizadeh Jul 17 '15 at 04:40
0
<?php
    mysql_connect("localhost", "root", "gm123") or die(mysql_error());
    mysql_query("CREATE DATABASE cat") or die(mysql_error());
    mysql_select_db("cat") or die(mysql_error());
    mysql_query("CREATE TABLE user(username varchar(20), password varchar(20), permission varchar(20))") or die(mysql_error());
    mysql_query("INSERT INTO user(username,password,permission) VALUES('gm','311807','admin')") or die(mysql_error());  
    mysql_query("CREATE TABLE cattbl(id INT NOT NULL PRIMARY KEY ,name VARCHAR(30),roll INT NOT NULL,technology VARCHAR(30),semister VARCHAR(30),shift VARCHAR(30),bdate varchar(30),cell VARCHAR(30),address VARCHAR(30),picture LONGBLOB)") or die(mysql_error());        

        header("Location: index.php");
?>

you may try this for your problem

0

i have just been doing this on my project

the actual answer is you can but it's a little complicated.

Cpanel doesnt allow you to use the normal "CREATE DATABASE 'mydbname';" in a php script to create a new database.

the only way you can do it is via logging on to your cpanel, going through the database wizard and creating a new one. Up until now you have probably been doing this manually each time.

this can been done with a php script but you have to use an api - which kind of goes through those same actions but automatically. You have to give the api your cpanel username and cpanel password.

just follow the answer to this question Create cpanel database through php script

hope that helps

Community
  • 1
  • 1
Lan
  • 1,874
  • 2
  • 20
  • 37
0

require_once 'database.php' add this line of code your index.php

$servername = 'localhost';
$username= 'root';
$password ='';

$conn = new mysqli($servername,$username,$password);

if($conn->connect_error)
{
    die("Connection Failed !" . $conn->connect_error()); 
}
    else
    {
        $sql = "CREATE DATABASE test_employee";

        if($conn->query($sql)== TRUE )
        {
            echo "Database Created Successfully";
        }
    }
0

you cannot create new user without logging in.

You can create database with root account or user with privileges

genesis
  • 50,477
  • 20
  • 96
  • 125
-1

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

Community
  • 1
  • 1
Nathaniel Wendt
  • 1,194
  • 4
  • 23
  • 49
  • 2
    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 http://w3fools.com/ – Madara's Ghost Aug 05 '11 at 18:08
  • 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's Ghost Aug 05 '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. – Nathaniel Wendt Aug 05 '11 at 18:21