A database in MySQL is created with the CREATE DATABASE command in the mysql console. But a single database isn't enough: a website or application also needs a dedicated user with permissions limited to that one database. Below we'll walk through the whole process on an Ubuntu VPS: install MySQL, harden the server, create a database and a user, and verify that everything works. You'll need SSH access to the server and a user with sudo or root privileges.
What you'll need before you start
Before you begin, make sure you have:
a VPS or dedicated server running Ubuntu LTS (this article uses Ubuntu; package and service names differ on Debian and AlmaLinux);
SSH access. If you haven't connected to your server yet, start with the article "What is SSH";
a user with sudo or root privileges;
5–10 minutes of free time and an open terminal.
If you don't have a server yet, you can order a VPS on the Leneex pricing page.
Step 1. Install MySQL Server (if it isn't installed yet)
Important: always back up your data before making changes, and check whether any other database server is already installed. If MariaDB is already running on the server, install MySQL in a separate Docker container.
Check whether MySQL is already installed on the server:
mysql --versionIf the command returns a version number, skip to step 2. If it says command not found, install the server from Ubuntu's default repository:
Update the package list:
sudo apt updateInstall the package:
sudo apt install mysql-serverCheck that the service is running:
sudo systemctl status mysqlThe output should show active (running). Press q to exit the status view. To exit a container, use exit.
To check the database version inside a Docker container, run:
docker exec -it CONTAINER_NAME mysql --version
Replace CONTAINER_NAME with your actual container name.
Step 2. Basic hardening (mysql_secure_installation)
Right after installation, it's worth running the mysql_secure_installation script. It asks about password validation strength and offers to close typical weak points: remove the test database, disallow remote root login, and apply the new settings immediately.
sudo mysql_secure_installationAnswer the questions thoughtfully and read them carefully: the set and order of questions depend on the version. If the script offers to enable password strength validation, it's best to accept. A medium level is fine for a learning server; for a production server, go with a stricter setting.
Step 3. Log in to the MySQL console
On a fresh Ubuntu installation, the MySQL administrator (root) usually logs in without a password via the system account. Just run:
sudo mysqlIf it worked, the prompt will change to mysql>. This is the console we'll be working in from now on. Remember one rule: every SQL command ends with a semicolon. Until it does, the console assumes you haven't finished typing and waits for more input.
If you're prompted for a password or get an access error, login is configured differently on your server. Try mysql -u root -p and enter the password you set in step 2.
Step 4. Create a database
A MySQL database is a named storage container that holds the tables for your website or application. It's best practice to give each project its own database.
Let's create a database called shop1_db using the utf8mb4 character set:
CREATE DATABASE shop1_db
CHARACTER SET utf8mb4
COLLATE utf8mb4_0900_ai_ci;It's better to specify the character set explicitly rather than rely on the default, since that depends on the server version and configuration. utf8mb4 stores the full Unicode range, including emoji and rare characters, while the older utf8 in MySQL only supports part of it.
If you're not sure whether a database with that name already exists, add IF NOT EXISTS so re-running the command won't throw an error:
CREATE DATABASE IF NOT EXISTS shop1_db;A successful response looks like Query OK, 1 row affected. Now let's confirm the database was created:
SHOW DATABASES;In Docker, first enter the container:
docker exec -it CONTAINER_NAME bashThen run:
mysql -u root -pEnter the password (it won't be displayed on screen — that's normal, just press Enter)

Make sure you see the mysql prompt. Now let's create our database inside the container.
CREATE DATABASE shop1_db
CHARACTER SET utf8mb4
COLLATE utf8mb4_0900_ai_ci;
Step 5. Create a user and grant permissions
An application doesn't need root access. If the site gets hacked, the attacker would gain administrator rights over your entire database server. That's why we create a separate user and grant it permissions only on shop_db.
CREATE USER 'shop_user'@'localhost' IDENTIFIED BY 'REPLACE_WITH_YOUR_PASSWORD';
GRANT ALL PRIVILEGES ON shop_db.* TO 'shop_user'@'localhost';
FLUSH PRIVILEGES;Let's break this down:
'shop_user'@'localhost' — the username and the host it's allowed to connect from. localhost means "only from this same server";
REPLACE_WITH_YOUR_PASSWORD — a placeholder. Come up with a long, unique password and avoid words like password;
shop_db.* — all tables in this database only. Using . would mean every database on the server — don't use that;
FLUSH PRIVILEGES — reloads the privilege tables. After CREATE USER and GRANT this is usually not strictly necessary, but it doesn't hurt.
Principles of least privilege:
one project — one database — one user;
permissions only on the needed database, not the whole server;
keep the host as narrow as possible: localhost if the application runs on the same server;
each user has its own password.
Step 6. Test the connection with the new user
Let's verify that the user works and can only see its own database:
Exit the console:
EXIT;Log in as the new user (you'll be prompted for the password after entering the command):
mysql -u shop_user -pList the databases:
SHOW DATABASES;Select your database:
USE shop_db;Optionally, create a test table to confirm that writing works:
CREATE TABLE test (id INT PRIMARY KEY, note VARCHAR(50));You can delete it with DROP TABLE test;.
Common errors
Error | Cause | What to do |
|---|---|---|
ERROR 1045 (28000): Access denied for user | Wrong password or username, or the user was created for a different host | Check the password and host. List users with: SELECT user, host FROM mysql.user; |
ERROR 1044: Access denied ... to database | The user doesn't have permissions on this database | Log in as the administrator and rerun GRANT for the needed database |
ERROR 1007: Can't create database; database exists | A database with that name already exists | Choose a different name or use CREATE DATABASE IF NOT EXISTS |
ERROR 2002: Can't connect to local MySQL server through socket | The MySQL service isn't running | Check sudo systemctl status mysql and start it: sudo systemctl start mysql |
How to connect the database to a site or application
To connect, an application needs four parameters: host (localhost, in our case), database name (shop_db), username (shop_user), and password. These go into the site's configuration file or the application's environment variables.
By default, MySQL only accepts connections from the server itself, so the database can't be reached from outside. This is intentional: a database exposed to the internet becomes a target for password brute-forcing. If remote access is genuinely needed, it's enabled through a separate setting and restricted by IP address.
If you're installing a ready-made application like WordPress or Ghost, keep in mind that when installing from the Leneex marketplace, the database is often created automatically, so you won't need to set it up by hand.
What if you're using a control panel?
In control panels, databases are created through the interface, without using the console. For ISPmanager, there's a separate article, "How to create a website," that walks through every button click. In cPanel, Plesk, DirectAdmin, FASTPANEL, and HestiaCP the logic is the same: go to the databases section, create a database, create a user, and link the user to the database.
Conclusion
The whole process comes down to a simple pattern: a dedicated database, a dedicated user, and minimal permissions. This keeps your project isolated and reduces the risk if something goes wrong or gets compromised. If you need a server of your own for a website or application with a database, check out Leneex VPS plans.
FAQ
How do I create a MySQL database from the command line?
Log in to the console with sudo mysql and run CREATE DATABASE database_name CHARACTER SET utf8mb4;. A successful response is Query OK.
Should I create a separate user for every database?
Yes, it's recommended. That way, if a password leaks, only one project is affected instead of the entire server.
Which character set should I choose for a new database?
In most cases, utf8mb4: it stores the full Unicode range. The appropriate collation depends on the MySQL version — for 8.0, for example, it's utf8mb4_0900_ai_ci.
How do I view the list of databases in MySQL?
Run SHOW DATABASES; in the console. A regular user will only see the databases they have access to.
How does MySQL differ from MariaDB when creating a database?
The syntax for CREATE DATABASE, CREATE USER, and GRANT is largely the same. The differences show up in the default character sets and collations, as well as authentication methods, so you should cross-check the commands in this article against MariaDB's documentation.







