MySQL Create Database


Create Database using mysqladmin:

You would need special privileges to create or to delete a MySQL database. So assuming you have access to root user, you can create any database using mysql mysqladmin binary.


Here is a simple example to create database called TUTORIALS:

[root@host]# mysqladmin -u root -p create TUTORIALS
Enter password:******

This will create a MySQL database TUTORIALS.

Create Database using PHP Script:

PHP uses mysql_query function to create or delete a MySQL database. This function takes two parameters and returns TRUE on success or FALSE on failure.


bool mysql_query( sql, connection );
Parameter Description
sqlRequired - SQL query to create or delete a MySQL database
connectionOptional - if not specified, then last opened connection by mysql_connect will be used.


Try out the following example to create a database:

<title>Creating MySQL Database</title>
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
  die('Could not connect: ' . mysql_error());
echo 'Connected successfully<br />';
$retval = mysql_query( $sql, $conn );
if(! $retval )
  die('Could not create database: ' . mysql_error());
echo "Database TUTORIALS created successfully\n";