MySQL Create Database
After logging into MySQL service, we can usecreatecommand to create a database, the syntax is as follows:
CREATE DATABASE 数据库名;
The following command simply demonstrates the process of creating a database, the database name is EXAMPLE:
[root@host]# mysql -u root -p Enter password:****** # 登录后进入终端 mysql> create DATABASE EXAMPLE;
The basic syntax for creating a database is as follows:
CREATE DATABASE [IF NOT EXISTS] database_name [CHARACTER SET charset_name] [COLLATE collation_name];
If you want to specify some options when creating a database, you can use other parameters of the CREATE DATABASE statement, for example, you can specify the character set and collation:
Example
CHARACTER SET utf8mb4
COLLATE utf8mb4_general_ci;
If the database already exists, executing CREATE DATABASE will cause an error.
To avoid this, you can add the IF NOT EXISTS clause to the CREATE DATABASE statement:
Example
Create a database using mysqladmin
mysqladmin is a command-line tool provided by MySQL for performing administrative tasks.
With mysqladmin, you can perform various database management operations, including creating databases.
The following is the basic syntax for creating a database using mysqladmin:
mysqladmin -u your_username -p create your_database
-uThe parameter is used to specify the MySQL username.-pThe parameter indicates that a password is required.createis the operation to be performed, indicating creating a database.your_databaseis the name of the database to be created.
As a normal user, you may need specific privileges to create or delete MySQL databases.
Here we log in as the root user. The root user has the highest privileges and can use mysqlmysqladmincommand to create a database.
After executing the above command, the system will prompt you to enter a password. After entering the password, press the Enter key.
The following command simply demonstrates the process of creating a database, the database name is EXAMPLE:
[root@host]# mysqladmin -u root -p create EXAMPLE Enter password:******
After the above command is executed successfully, the MySQL database EXAMPLE will be created.
If you want to specify the character set and collation when creating a database, you can use-default-character-setand-default-collationparameter:
mysqladmin -u your_username -p create your_database \ --default-character-set=utf8mb4 \ --default-collation=utf8mb4_general_ci
The above code creates a database using the utf8mb4 character set and utf8mb4_general_ci collation.
Please note that when executing these commands, make sure the MySQL server is running and you have sufficient privileges to perform these operations.
If you want to use mysqladmin to connect to the MySQL server to perform other administrative tasks, such as viewing server status, restarting the server, etc., you can use the following form of command:
mysqladmin -u your_username -p your_command
Here, your_command is the specific administrative command you wish to execute.
For example, to view the status of the MySQL server, you can use:
mysqladmin -u your_username -p status
This will prompt you for a password and display information about the server status.
Create a database using PHP script
PHP uses the mysqli_query function to create or delete MySQL databases.
This function has two parameters and returns TRUE on success, otherwise returns FALSE.
Syntax
mysqli_query(connection,query,resultmode);
| Parameter | Description |
|---|---|
| connection | Required. Specifies the MySQL connection to use. |
| query | Required. Specifies the query string. |
| resultmode |
Optional. A constant. Can be any of the following values:
|
Example
The following example demonstrates using PHP to create a database:
Create Database
After successful execution, the following result is returned:

If the database already exists, after execution, the following result is returned:
