« Back

MySQL for Beginners

BasicsVPSWeb Hosting

MySQL is an open-source relational database management system used to store, organise and retrieve structured data. It is widely used by websites and web applications, including many content management systems.

In this beginner’s guide, we’ll look at some of the most common MySQL tasks, including installing MySQL on a Linux server, creating a database and table, adding and updating information, and deleting data.

Important: The examples below are intended for users who manage their own VPS or server. If you use managed web hosting, you may need to create and manage databases through your hosting control panel instead.

Contents

  1. What is MySQL?
  2. Installing MySQL on Linux
  3. Connecting to MySQL
  4. Creating and deleting a database
  5. Selecting a database
  6. Creating a table
  7. Adding data to a table
  8. Updating data
  9. Adding and removing columns
  10. Deleting rows
  11. Basic MySQL safety tips

What is MySQL?

MySQL is a relational database management system, often shortened to RDBMS.

A database stores information in an organised format. Within a MySQL database, information is usually divided into tables, which contain rows and columns.

For example, a website might use separate tables to store:

  • user accounts;
  • blog posts;
  • orders;
  • products; and
  • website settings.

Applications can then retrieve, add, update or remove this information using SQL, or Structured Query Language.

This guide introduces some basic SQL commands you can use with MySQL.

Installing MySQL on Linux

The installation process depends on your Linux distribution.

Before installing MySQL yourself, check whether it is already installed or whether your hosting provider manages the database server for you.

Installing MySQL on Ubuntu

On a supported Ubuntu server, you can usually install the MySQL server package using APT:

sudo apt update
sudo apt install mysql-server

Once the installation is complete, check that MySQL is running:

sudo systemctl status mysql

The MySQL documentation also recommends securing a new installation. You can launch the security configuration tool with:

sudo mysql_secure_installation

This can help you configure options such as the MySQL root account and remove unnecessary default accounts or databases.

Installing MySQL on RHEL-compatible distributions

On distributions such as Red Hat Enterprise Linux and compatible systems, MySQL can be installed using the official MySQL Yum repository.

Once the appropriate MySQL repository has been configured for your operating system, install the server with:

sudo dnf install mysql-community-server

Then enable and start the service:

sudo systemctl enable --now mysqld

You can check its status with:

sudo systemctl status mysqld

When MySQL Community Server is installed using RPM packages, a temporary password for the root MySQL account may be written to the MySQL log. It can usually be found with:

sudo grep 'temporary password' /var/log/mysqld.log

You should replace this temporary password before using the server normally.

Note: Package names and installation steps can vary between Linux distributions and versions. If you are managing a production server, check the documentation for your operating system and the version of MySQL you intend to install.

Connecting to MySQL

Once MySQL is installed, you can access it from the command line.

A common way to connect using the MySQL root account is:

mysql -u root -p

The -u option specifies the username, while -p tells MySQL to request a password.

Enter your MySQL password when prompted. For security reasons, nothing may appear on screen while you type the password. This is normal.

On some Ubuntu installations, the local MySQL root account uses operating-system authentication instead of a password. In this case, you may be able to connect with:

sudo mysql

If you have been given a separate MySQL user account, use that username instead:

mysql -u database_user -p

Once connected, you should see a prompt similar to:

mysql>

You can leave the MySQL client at any time with:

EXIT;

A few things to remember

Most SQL statements should end with a semicolon:

SHOW DATABASES;

If you press Enter without a semicolon, MySQL may assume that you have not finished entering the command.

SQL keywords such as SELECT, CREATE, UPDATE and DELETE are not case-sensitive. Writing SQL keywords in uppercase is simply a common convention that makes queries easier to read.

Database and table names require more care. Their case sensitivity can depend on the operating system and MySQL configuration. On Linux, they are commonly case-sensitive, so it is good practice to use consistent lowercase names.

Creating and deleting a MySQL database

To see the databases available to your MySQL account, run:

SHOW DATABASES;

You may see several databases that MySQL uses internally. Do not delete system databases unless you know exactly what they are used for.

Create a database

For this tutorial, we’ll create a database called community_event:

CREATE DATABASE community_event CHARACTER SET utf8mb4;

You can confirm that it exists by running:

SHOW DATABASES;

The new community_event database should now appear in the list.

Delete a database

You can remove a database using DROP DATABASE:

DROP DATABASE community_event;

Be careful with this command. Dropping a database deletes the database and its contents.

Always make sure you have selected the correct database and have a working backup before using destructive commands on a live website or application.

Selecting a database

Before you create tables or work with data, tell MySQL which database you want to use.

For our example:

USE community_event;

MySQL should respond with:

Database changed

To display the tables in the selected database, run:

SHOW TABLES;

As this is a new database, there are no tables yet.

Creating a MySQL table

Let’s imagine that we’re organising a community event and want to keep track of people who have signed up and what food they plan to bring.

We can create a table called guests:

CREATE TABLE guests (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    food VARCHAR(100),
    confirmed BOOLEAN NOT NULL DEFAULT FALSE,
    signup_date DATE NOT NULL,
    PRIMARY KEY (id)
);

This creates five columns.

id gives each guest a unique number. AUTO_INCREMENT means MySQL automatically generates the next number when a new row is added.

name stores the guest’s name and allows up to 100 characters.

food stores the item they intend to bring.

confirmed records whether the guest has confirmed their attendance. In MySQL, BOOLEAN is treated as a synonym for TINYINT(1), with FALSE represented by 0 and TRUE by 1.

signup_date stores a date. MySQL date values are normally written in YYYY-MM-DD format.

You can now check that the table exists:

SHOW TABLES;

To inspect its structure, use:

DESCRIBE guests;

Adding data to a MySQL table

Now that the table exists, we can add some guests.

The INSERT INTO statement adds new rows:

INSERT INTO guests (name, food, confirmed, signup_date)
VALUES
    ('Alice', 'Pasta salad', TRUE, '2026-11-10'),
    ('Ben', 'Sausage rolls', FALSE, '2026-11-11'),
    ('Chloe', 'Apple crumble', TRUE, '2026-11-12'),
    ('Daniel', 'Sandwiches', TRUE, '2026-11-13');

Notice that we did not provide an id. Because the column uses AUTO_INCREMENT, MySQL assigns one automatically.

To display the contents of the table, run:

SELECT * FROM guests;

You should see something similar to:

+----+--------+----------------+-----------+-------------+
| id | name   | food           | confirmed | signup_date |
+----+--------+----------------+-----------+-------------+
|  1 | Alice  | Pasta salad    |         1 | 2026-11-10 |
|  2 | Ben    | Sausage rolls  |         0 | 2026-11-11 |
|  3 | Chloe  | Apple crumble  |         1 | 2026-11-12 |
|  4 | Daniel | Sandwiches     |         1 | 2026-11-13 |
+----+--------+----------------+-----------+-------------+

A value of 1 in the confirmed column represents TRUE, while 0 represents FALSE.

Updating information in a table

Suppose Ben confirms that he will be attending the event.

First, it is good practice to check that you have identified the correct row:

SELECT * FROM guests WHERE id = 2;

You can then update it:

UPDATE guests
SET confirmed = TRUE
WHERE id = 2;

The WHERE clause is extremely important because it tells MySQL which row to update.

Without it:

UPDATE guests
SET confirmed = TRUE;

MySQL would update the confirmed value for every row in the table.

For this reason, take particular care with UPDATE statements when working with live databases.

Adding and removing columns

Database requirements often change over time.

For example, suppose we decide to store each guest’s email address as well.

We can add a new column using ALTER TABLE:

ALTER TABLE guests
ADD COLUMN email VARCHAR(255) AFTER name;

The AFTER name part tells MySQL to position the new column immediately after the name column. If you leave it out, MySQL normally adds the new column at the end of the table.

Check the new structure with:

DESCRIBE guests;

Removing a column

If the column is no longer required, it can be removed with:

ALTER TABLE guests
DROP COLUMN email;

Warning: Removing a column also removes the data stored in that column.

Make sure you have a backup before changing the structure of an important database.

Deleting a row

You can remove individual records using the DELETE statement.

For example, suppose Ben can no longer attend. First check the row you are about to remove:

SELECT * FROM guests WHERE id = 2;

If it is the correct record, delete it with:

DELETE FROM guests
WHERE id = 2;

You can check the remaining records with:

SELECT * FROM guests;

The remaining IDs may now look like this:

1
3
4

This is normal.

An AUTO_INCREMENT value is intended to provide a unique identifier. MySQL does not automatically renumber all the remaining rows simply because one row has been deleted.

As with UPDATE, be particularly careful with the WHERE clause. Running:

DELETE FROM guests;

would delete every row in the table.

Basic MySQL safety tips

When experimenting with your own test database, mistakes are usually easy to correct. On a live website, however, an incorrect database command can cause data loss or take the website offline.

Follow these basic precautions:

  • Back up important databases before making structural or destructive changes.
  • Check a row with SELECT before running an UPDATE or DELETE statement against it.
  • Pay close attention to WHERE clauses.
  • Do not use the MySQL root account for normal website or application access.
  • Give database users only the permissions they actually need.
  • Use strong, unique passwords for database accounts.
  • Do not expose the MySQL server directly to the public internet unless remote access is genuinely required and properly secured.
  • Test changes on a development or staging environment before applying them to an important production database.

MySQL’s own security guidance recommends limiting account privileges rather than giving users unnecessary access, as well as maintaining appropriate database backups and restricting network access where possible.

For applications, it is generally better to create a dedicated database user than to connect as root. MySQL provides CREATE USER, GRANT and REVOKE for managing accounts and their privileges.

Summary

The commands in this guide cover the basics of working with MySQL from the command line.

You now know how to:

  • install and access MySQL;
  • create and select a database;
  • create tables;
  • insert and view data;
  • update existing records;
  • add and remove columns; and
  • delete individual records.

These are useful building blocks for understanding how MySQL databases work. If you manage a website through a hosting control panel, many of these operations may also be available through graphical tools, so you may not need to work directly from the command line for everyday database management.

Related posts