Create MySQL database from terminal


Why create a database from terminal?

Creating a database from the terminal gives you more control and flexibility, especially if you don't have access to phpMyAdmin or prefer working from the command line.


Prerequisites

To create a database from terminal you need:

  • ✅ SSH access to your server
  • ✅ User with permissions to create databases (usually root or a user with privileges)
  • ✅ MySQL or MariaDB installed on the server

Step 1: Connect to server via SSH

  • Connect to your server using SSH
  • Log in with a user that has admin permissions
ssh user@23.175.40.20

Step 2: Access MySQL

Once connected, access MySQL:

mysql -u root -p

It will ask for the root password. Enter it and press Enter.

Note: If you use a different user, replace root with your user.


Step 3: Create the database

Once inside MySQL, execute the following command:

CREATE DATABASE database_name;

Example:

CREATE DATABASE my_webapp;

Important: Don't forget the semicolon (;) at the end of the command.

If everything went well, you'll see:

Query OK, 1 row affected (0.00 sec)

Step 4: Create a user and grant permissions (recommended)

It's good practice to create a specific user for each database:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'secure_password';

Example:

CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'MyPassword123!';

Grant full permissions on the database:

GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';

Example:

GRANT ALL PRIVILEGES ON my_webapp.* TO 'app_user'@'localhost';

Apply the changes:

FLUSH PRIVILEGES;

Step 5: Verify database was created

To see all databases:

SHOW DATABASES;

You should see your new database in the list.

To see created users:

SELECT user, host FROM mysql.user;

Option: Create from single command

You can also create the database without entering MySQL, executing from terminal:

mysql -u root -p -e "CREATE DATABASE database_name;"

Example:

mysql -u root -p -e "CREATE DATABASE my_webapp;"

It will ask for the password and create the database directly.


Configure charset and collation

To create a database with specific charset and collation (recommended for UTF-8):

CREATE DATABASE database_name 
CHARACTER SET utf8mb4 
COLLATE utf8mb4_unicode_ci;

Example:

CREATE DATABASE my_webapp 
CHARACTER SET utf8mb4 
COLLATE utf8mb4_unicode_ci;

Advantages of utf8mb4:

  • ✅ Supports emojis and special characters
  • ✅ Compatible with all languages
  • ✅ Recommended for modern applications

Common issues

"Access denied" error

  • Verify that the user has permissions to create databases
  • Use root user or a user with privileges
  • Verify that the password is correct

"database already exists" error

  • The database already exists
  • Verify with SHOW DATABASES;
  • Use a different name or delete the existing one first

Syntax error

  • Verify that the semicolon (;) is not missing at the end
  • Verify there are no typos
  • Verify that names don't have spaces or special characters

Delete a database

If you need to delete a database:

DROP DATABASE database_name;

⚠️ Warning: This will permanently delete the database and all its data. Make sure you have a backup first.


Exit MySQL

Para salir de MySQL:

exit

Or press CTRL + D


Tips

  • ✅ Use descriptive names for your databases
  • ✅ Create a specific user for each database
  • ✅ Use strong passwords for users
  • ✅ Configure utf8mb4 charset for full character support
  • ✅ Backup before deleting databases
  • ✅ Document the users and permissions created

Need help?

If you're having trouble creating databases or need assistance, open a ticket from the billing.baires.host panel or contact us for support.

You can also reach us through our social media:

Was this answer helpful? 0 Users Found This Useful (0 Votes)