Domain insights

How to Install MySQL on an Ubuntu VPS and Create Your First Database

A beginner-friendly MySQL tutorial for Ubuntu VPS users: install MySQL, create a database and user, test access, make a backup and troubleshoot common errors.

VPS and Linux
How to Install MySQL on an Ubuntu VPS and Create Your First Database

You have a new VPS.

Your website files are ready.

Your application asks for four things:

Database host
Database name
Database username
Database password

And suddenly the simple project has become a database project.

The good news is that a basic MySQL setup does not require you to become a database administrator.

For a small website or application, you need to understand a short workflow:

  1. install MySQL;
  2. confirm it is running;
  3. create a database;
  4. create a separate application user;
  5. grant that user access only to the database it needs;
  6. test the login;
  7. know how to make a backup;
  8. know where to look when something fails.

This tutorial uses Ubuntu and the MySQL package provided through Ubuntu's package management system.

The exact MySQL version can vary depending on the Ubuntu release and repositories you use. The commands below focus on tasks that remain consistent across normal current Ubuntu server installations.

Choose our VPS plans when you need to install and configure your own database service. If the project primarily needs a conventional website, compare our shared hosting options first; our infrastructure comparison explains the trade-offs.

Before you install anything

Connect to the VPS using SSH.

For example:

ssh [email protected]

You need a user that can run administrative commands with sudo.

Confirm the Ubuntu version:

cat /etc/os-release

Then check available disk space:

df -h

A database should not be installed on a server that is already close to full.

Also check memory:

free -h

MySQL can run on small servers, but available memory matters as your database and traffic grow.

If SSH is still unfamiliar, read SSH for VPS Beginners: Connect Safely, Use Keys, Copy Files, and Fix Common Errors.

Step 1: update the package list

Run:

sudo apt update

This refreshes the local list of packages available from the configured Ubuntu repositories.

You may also want to install normal operating-system updates before adding a new service:

sudo apt upgrade

Read the list of proposed changes before confirming on an important production server.

Package updates are routine, but a production VPS still deserves change control.

Step 2: install MySQL Server

Ubuntu's server documentation uses:

sudo apt install mysql-server

Confirm the installation when prompted.

After the package finishes installing, MySQL should normally start automatically.

Check:

sudo systemctl status mysql

You want to see something similar to:

Active: active (running)

Press:

q

to exit the status screen if it opens in a pager.

You can also check simply:

sudo systemctl is-active mysql

If the response is:

active

the service is running.

Install the database, restrict the application account and test an off-server backup.
Install the database, restrict the application account and test an off-server backup.

Step 3: understand the MySQL root account on Ubuntu

On a typical Ubuntu package installation, MySQL creates a local administrative root account.

Ubuntu documents local root access using socket authentication.

That means you can normally enter MySQL with:

sudo mysql

You should see a prompt similar to:

mysql>

This is not the same as logging into your Linux shell as root.

You are now inside the MySQL command-line client.

To leave later, type:

exit;

Every SQL command in the examples below ends with a semicolon.

If you forget the semicolon, MySQL may simply wait for the rest of the command.

Step 4: create your first database

Suppose your website is called exampleapp.

Create a database:

CREATE DATABASE exampleapp;

Confirm it exists:

SHOW DATABASES;

You should see exampleapp in the list.

For many web applications, using utf8mb4 is appropriate because it supports the full Unicode character range.

As an alternative to the first CREATE DATABASE example, create the database explicitly with the following command. Run only one of the two creation commands for the same database:

CREATE DATABASE exampleapp
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;

Application requirements vary, so use a specific character set or collation requested by your software when applicable.

Step 5: create a separate application user

Do not configure a website to connect as the MySQL root user.

Create a dedicated account.

For example:

CREATE USER 'exampleuser'@'localhost'
IDENTIFIED BY 'Use-A-Long-Random-Password-Here';

Replace the example password with a strong unique password.

Do not use:

password123

Do not reuse your VPS root password.

Do not place the password in a public Git repository.

The @'localhost' part means this account is expected to connect from the same server.

That is appropriate when the website and MySQL run on the same VPS.

Database-scoped ALL PRIVILEGES is useful for installation and schema migrations, but it is still broad within that database. Where the application supports separate accounts, grant the runtime user only the operations it needs and keep migration privileges separate.

Step 6: grant access to only the required database

Give the user privileges on the application database:

GRANT ALL PRIVILEGES ON exampleapp.* TO 'exampleuser'@'localhost';

Then inspect the grants:

SHOW GRANTS FOR 'exampleuser'@'localhost';

You should see access to:

exampleapp.*

The asterisk means all tables inside that database.

It does not mean every database on the MySQL server.

This is much safer than granting global privileges to an application account.

For simple web hosting, the application user usually needs access to its own database and nothing else.

Step 7: test the application account

Exit MySQL:

exit;

Now connect as the new user:

mysql -u exampleuser -p

MySQL asks for the password.

After logging in:

USE exampleapp;

Then:

SELECT DATABASE();

It should return:

exampleapp

This simple test is important.

Do not configure WordPress, a custom application or another platform until you know the database credentials actually work.

Step 8: create a test table

For a basic functional test:

CREATE TABLE test_items (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL
);

Insert a row:

INSERT INTO test_items (name)
VALUES ('My first MySQL row');

Read it:

SELECT * FROM test_items;

You should see your row.

Remove the test table afterward if the application will manage its own schema:

DROP TABLE test_items;

Now you know the user can create, write and read objects inside the intended database.

The four values your application needs

Your application configuration will commonly use:

Database host: localhost
Database name: exampleapp
Database user: exampleuser
Database password: your strong password

Some applications use:

127.0.0.1

instead of:

localhost

Those values can cause different connection behavior because localhost may use a Unix socket while 127.0.0.1 uses TCP.

Follow the application's documentation.

If the website and database are on the same VPS, there is usually no reason to expose MySQL publicly to the internet.

Do not open port 3306 unless you need remote MySQL

MySQL commonly uses TCP port:

3306

A beginner may assume:

My website uses MySQL, so I need to open port 3306 in the firewall.

Not when the website and MySQL are on the same server.

A local application can connect locally.

Publicly exposing MySQL adds unnecessary attack surface.

Remote database access is a different architecture and should use restricted source addresses, appropriate MySQL users, firewall rules, secure networking, and TLS where required.

Do not set MySQL to listen on every interface simply to solve a local application problem.

How to restart MySQL

After a configuration change, you may need:

sudo systemctl restart mysql

Then verify:

sudo systemctl status mysql

If MySQL does not return to active (running), do not keep restarting it.

Read the logs:

sudo journalctl -u mysql --since "15 minutes ago"

This often reveals the configuration or resource problem.

For a broader troubleshooting workflow, read Your Website Is Down: How to Read VPS Logs Before You Restart Everything.

Our backup strategy guide explains retention and restore testing beyond a single SQL file. If imports exhaust memory, read RAM, swap and the OOM killer before choosing a larger VPS or dedicated server.

How to create a backup with mysqldump

A database that exists only on one VPS is not safely backed up.

For a simple logical backup:

mysqldump --single-transaction --no-tablespaces -u exampleuser -p exampleapp > exampleapp.sql

Enter the MySQL password when prompted. This example suits a simple InnoDB database: --single-transaction provides a consistent read of InnoDB tables, while --no-tablespaces avoids requiring the global PROCESS privilege merely to dump tablespace metadata. Avoid schema changes during the dump; nontransactional tables, routines, events and replication/GTID configurations can require different options or additional privileges.

Check the file:

ls -lh exampleapp.sql

Do not assume a zero-byte or unexpectedly tiny file is a valid backup.

To restore into an existing database:

mysql -u exampleuser -p exampleapp < exampleapp.sql

Test your backups.

A backup strategy is incomplete until you have verified that restoration works.

Also remember that storing the only backup on the same VPS does not protect you from a VPS failure, account problem, storage corruption or accidental deletion.

Copy backups to another protected location.

Avoid passwords directly in shell commands

You may see examples like:

mysql -u exampleuser -pMyPassword

Avoid this style.

A password typed directly into a command can become visible in shell history or process information depending on the environment.

Prefer:

mysql -u exampleuser -p

and enter the password when prompted.

Applications should store secrets using the mechanism appropriate for their platform and permissions.

Useful MySQL commands for beginners

Enter MySQL:

sudo mysql

Show databases:

SHOW DATABASES;

Select a database:

USE exampleapp;

Show tables:

SHOW TABLES;

Show users and hosts:

SELECT user, host FROM mysql.user;

Show a user's permissions:

SHOW GRANTS FOR 'exampleuser'@'localhost';

Exit:

exit;

These commands are enough for many basic administration tasks.

Common error: Access denied for user

You may see:

ERROR 1045 (28000): Access denied for user

Check:

  1. username;
  2. password;
  3. host portion of the MySQL account;
  4. whether you are connecting through localhost or another host;
  5. whether the application is using the credentials you expect.

Inside MySQL:

SELECT user, host FROM mysql.user;

Remember that:

'exampleuser'@'localhost'

and:

'exampleuser'@'%'

are not the same account definition.

Do not create a broad % account merely to silence an error.

Understand where the application is connecting from.

Common error: Unknown database

If the application reports an unknown database, confirm:

SHOW DATABASES;

The application configuration may contain a typo.

Database names can also differ by environment.

Check the exact value rather than creating another database with a slightly different spelling.

Common error: Can't connect to local MySQL server

First check:

sudo systemctl status mysql

If MySQL is stopped:

sudo journalctl -u mysql --since "30 minutes ago"

Also check disk space:

df -h

A database service can fail when the server has no usable disk space.

Check memory:

free -h

If the service was killed because the system ran out of memory, restarting MySQL is only a temporary recovery.

Common error: permission denied after the database exists

The database may exist while the application user lacks permissions.

Inspect:

SHOW GRANTS FOR 'exampleuser'@'localhost';

Then compare the database name in the grants with the name configured in your application.

A one-character difference matters.

Should you run mysql_secure_installation?

Many MySQL installations include:

sudo mysql_secure_installation

The utility can help review security-related settings.

Its exact prompts and behavior can vary by MySQL package and authentication configuration.

Do not follow an old tutorial mechanically.

Read every prompt.

On Ubuntu, local MySQL root authentication may use the operating system socket, so password advice written for another distribution or an old MySQL version may not match your server.

The goal is not to press Y as quickly as possible.

The goal is to understand the resulting authentication configuration.

Keep MySQL updated

Normal Ubuntu security and package maintenance matters.

Check available updates:

sudo apt update
apt list --upgradable

Apply updates according to your maintenance process.

Database upgrades deserve backups and a rollback plan.

Do not run major version upgrades casually on the only copy of production data.

For a small VPS, good operations are often simple:

  • keep the OS supported;
  • apply security updates;
  • back up the database;
  • test restores;
  • monitor disk space;
  • monitor memory;
  • review MySQL logs when behavior changes.

A simple production checklist

Before connecting a real website:

  • MySQL is active.
  • The database exists.
  • The application has its own user.
  • The user is restricted to the required database.
  • The password is strong and stored safely.
  • The application can connect.
  • Port 3306 is not unnecessarily public.
  • Backups exist outside the VPS.
  • A restore has been tested.
  • Disk and memory are monitored.
  • You know how to read MySQL service logs.

That is a much better foundation than installing MySQL and hoping the installer handled everything forever.

Frequently asked questions

How do I install MySQL on Ubuntu?

Run: sudo apt update; sudo apt install mysql-server Then verify the service with: sudo systemctl status mysql

How do I open the MySQL command line as the local administrator on Ubuntu?

A normal Ubuntu package installation commonly supports: sudo mysql

Should my website use the MySQL root account?

No. Create a dedicated user and grant it access only to the application's database.

What host should I use for MySQL if the website is on the same VPS?

Usually localhost is appropriate, but follow your application's requirements.

Do I need to open TCP port 3306?

Not when the application and database are on the same VPS. Public remote access should be enabled only when required and secured deliberately.

How do I back up one database?

A basic logical backup is: mysqldump -u USER -p DATABASE > backup.sql

How do I see why MySQL failed?

Start with: sudo systemctl status mysql; sudo journalctl -u mysql --since "30 minutes ago"

Where should I store backups?

Keep protected copies outside the VPS. A backup stored only beside the original database does not protect you from loss of the entire server.