Creating a database from the MySQL command line is a fundamental skill for developers and system administrators, providing a fast and scriptable way to initialize your data environment. This method is particularly useful when working on remote servers or automating setup processes, as it avoids the overhead of graphical interfaces. The command line offers precision and control, allowing you to define the database character set and collation directly during creation. Mastering this step ensures a consistent foundation for any application relying on MySQL.
Prerequisites and Access
Before you can create a database, you need command line access to the MySQL server and a user account with sufficient privileges. Typically, this requires the CREATE privilege granted to your specific user identity. You will use the mysql client program, which is installed alongside the MySQL server or can be installed separately as part of the MySQL client package. Ensure your MySQL service is actively running before attempting to establish a connection.
Logging into the MySQL Shell
To begin, open your terminal or command prompt and launch the MySQL client. The standard command requires specifying a username, and you will be prompted for the corresponding password. For administrative tasks, it is common to log in as the root user, although it is recommended to use a less privileged account for routine operations. Once authenticated, you will see the mysql> prompt, indicating you are ready to execute SQL statements.
Basic CREATE DATABASE Syntax
The SQL statement for this action is straightforward and follows a consistent pattern. The core command is CREATE DATABASE , followed by the identifier you wish to assign to your new schema. It is a best practice to check for the existence of the database before creation to prevent errors. You can achieve this by adding the IF NOT EXISTS clause, which makes the command idempotent and safe to run in scripts.
Applying Character Sets and Collation
A robust database configuration considers internationalization from the start. By default, MySQL uses a system character set, but you can explicitly define it to ensure proper handling of multilingual data. The utf8mb4 character set is the modern standard, supporting full Unicode, including emojis. You can link a specific collation, such as utf8mb4_unicode_ci , to determine how strings are compared and sorted within that database.
Example Command with Encoding
To create a database named shop_inventory with full UTF-8 support, you would execute the following command in the MySQL prompt. This ensures that your application can store text in any language without encountering encoding errors. The utf8mb4 set requires MySQL version 4.1.1 or later, which covers nearly all current installations in use today.
Verification and Next Steps
After executing the creation command, you should verify that the database exists and is accessible. The SHOW DATABASES; command lists all available schemas on the server, allowing you to confirm your work. Once the database is created, you can proceed to define the internal structure by creating tables using CREATE TABLE statements. This logical progression moves your project from configuration to actual data modeling.