Getting Started with MySQL
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
MySQL is an open-source relational database server that stores data in tables, speaks SQL and keeps the
data safe with ACID transactions through its default storage engine, InnoDB. This page explains what you are
installing, which edition and release track to pick, how to start a server in a container, how to talk to it
with the mysql client, and how to load the shop sample database that the rest of this reference uses.
|
Every statement and output on this page was produced by running the code against the official |
What MySQL is
MySQL is a client/server database: an application connects over TCP (port 3306 by default) or a local socket, authenticates, and sends SQL statements. The server parses and optimises them and executes them against one of its pluggable storage engines. For almost every workload that engine is InnoDB, which provides transactions, row-level locking, crash recovery and foreign keys; the layers are described on Architecture.
MySQL is developed by Oracle. This reference documents MySQL itself. MariaDB is a separate fork whose features have diverged from MySQL’s; it is not covered here.
Editions
-
MySQL Community Edition — the open-source (GPL) server; this is what the
mysqlDocker image ships and what every example here uses. -
MySQL Enterprise Edition — the commercial subscription that adds Oracle support and extra components (for example enterprise audit, firewall and backup tooling) on top of the same core server.
-
MySQL HeatWave — Oracle’s managed cloud service built around MySQL with an in-memory analytics engine; it is a hosted product, not something you install.
The release model
MySQL publishes two tracks, described in the manual’s release model page:
-
LTS (Long-Term Support) releases receive only the fixes that are necessary, so behaviour stays stable within the series. They follow Oracle Lifetime Support: five years of premier support and three years of extended support. Features are added or removed only in the first release of an LTS series.
-
Innovation releases carry new features and behaviour changes, can remove deprecated functionality, and are supported until the next Innovation release. They are production-grade but meant for teams that test continuously.
At the time of writing the official Docker image publishes these tags:
| Tag | Server version |
|---|---|
|
26.10.0 (Innovation) |
|
9.7.4 (LTS) |
|
8.4.13 (LTS) |
Use an LTS tag (lts, or a pinned 8.4 / 9.7) for anything long-lived, and latest when you want to see
the newest behaviour. The server used for the examples in this reference is latest; places where an older
LTS behaves differently say so. Upgrading within an LTS series is an in-place operation; moving from an
Innovation release to another one means going forward to the next Innovation release, and downgrading from an
Innovation release requires a logical dump and reload.
Running MySQL in Docker
docker run -d --name mysql-lab -p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=secret \
mysql:latest
The image reads MYSQL_ROOT_PASSWORD on first start to initialise the data directory, so the first boot takes
several seconds. Watch the log and wait for the ready for connections line:
docker logs mysql-lab 2>&1 | grep "ready for connections"
The container keeps its data in an anonymous volume mounted at /var/lib/mysql. To keep the data under a
name of your choice add -v mysql-lab-data:/var/lib/mysql; to run extra .sql or .sh files on first boot
mount them into /docker-entrypoint-initdb.d/. Other useful variables are MYSQL_DATABASE (create a
database), MYSQL_USER / MYSQL_PASSWORD (create an application user) and MYSQL_ALLOW_EMPTY_PASSWORD. See
Docker for container and volume handling in general.
Confirm the server is up and which version it is:
docker exec mysql-lab mysqladmin -uroot -psecret ping
mysqld is alive
The mysql client
The image contains the mysql command-line client, so no local install is needed:
docker exec -it mysql-lab mysql -uroot -psecret
The client has two modes. Interactive gives you a mysql> prompt, where a statement ends with ;.
Batch runs statements from -e or from standard input and exits, which is how scripts load data.
# one statement
docker exec mysql-lab mysql -uroot -psecret -e "SELECT VERSION()"
# a file (note -i, which keeps stdin open)
docker exec -i mysql-lab mysql -uroot -psecret < script.sql
Options that matter in daily use:
| Option | Effect |
|---|---|
|
Draw result tables even in batch mode (batch output is otherwise tab-separated). |
|
Print each row as a block of |
|
Declare the client character set. Inside the container the client defaults to |
|
Enable local client commands (see below). |
|
Keep going after an error in a script instead of stopping. |
|
Connect to a host and port instead of the local socket. |
Local client commands. Commands that are handled by the client rather than the server — \G (vertical
output), \s (status), source, help, pager and similar — are disabled by default in the 26.10
client, which lists --commands with a default of FALSE. With them disabled even \G is rejected:
docker exec mysql-lab mysql -uroot -psecret -e 'SELECT 1 AS a\G'
ERROR at line 1: Unknown command '\G'.
Start the client with --commands (or -E for vertical output) to use them:
docker exec mysql-lab mysql -uroot -psecret --commands -e 'SELECT 1 AS a\G'
*************************** 1. row ***************************
a: 1
Examples elsewhere in this reference that end in \G assume the client was started with --commands. To make
it permanent put commands=ON under [mysql] in ~/.my.cnf.
Connecting
A connection needs a host, port, user, password and (optionally) a default database. Over TCP from outside the
container use the published port, for example mysql -h 127.0.0.1 -P 3306 -uroot -p from a host that has a
client installed. Once connected, ask the server about the session:
SELECT CURRENT_USER(), DATABASE(), CONNECTION_ID();
SHOW VARIABLES LIKE 'version%';
+----------------+------------+
| current_user() | database() |
+----------------+------------+
| root@localhost | NULL |
+----------------+------------+
+-------------------------+------------------------------+
| Variable_name | Value |
+-------------------------+------------------------------+
| version | 26.10.0 |
| version_comment | MySQL Community Server - GPL |
| version_compile_machine | aarch64 |
| version_compile_os | Linux |
| version_compile_zlib | 1.3.2 |
| version_with_update | 26.10.0.0 |
+-------------------------+------------------------------+
The mysql.user system table declares caching_sha2_password as the default authentication plugin for new
accounts, which is why very old client libraries may fail to log in; see
Administration. Connections over TCP are TLS-capable and the server
creates a self-signed certificate on first start (a TCP connection from inside the container reported
SSL/TLS as its connection type).
Loading the shop sample database
Every page in this reference reuses one small online shop, described on the
reference home page. Save the following script as
shop.sql. It drops and recreates a database called shop, so it is safe to re-run.
-- seed.sql -- the relational "shop" running example (MySQL)
DROP DATABASE IF EXISTS shop;
CREATE DATABASE shop;
USE shop;
CREATE TABLE customers (
id varchar(10) PRIMARY KEY,
name varchar(100) NOT NULL,
email varchar(100) NOT NULL UNIQUE,
city varchar(100) NOT NULL,
tier varchar(20) NOT NULL,
created_at datetime NOT NULL,
CONSTRAINT chk_tier CHECK (tier IN ('free', 'pro', 'enterprise'))
);
CREATE TABLE products (
sku varchar(10) PRIMARY KEY,
name varchar(100) NOT NULL,
category varchar(50) NOT NULL,
price decimal(10,2) NOT NULL CHECK (price >= 0),
stock int NOT NULL CHECK (stock >= 0),
tags json NOT NULL,
attrs json NOT NULL
);
CREATE TABLE orders (
id int PRIMARY KEY,
customer_id varchar(10) NOT NULL,
status varchar(20) NOT NULL,
order_date datetime NOT NULL,
CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers(id),
CONSTRAINT chk_status CHECK (status IN ('open', 'shipped', 'cancelled'))
);
CREATE TABLE order_items (
order_id int NOT NULL,
sku varchar(10) NOT NULL,
qty int NOT NULL CHECK (qty > 0),
price decimal(10,2) NOT NULL,
PRIMARY KEY (order_id, sku),
CONSTRAINT fk_items_order FOREIGN KEY (order_id) REFERENCES orders(id),
CONSTRAINT fk_items_product FOREIGN KEY (sku) REFERENCES products(sku)
);
INSERT INTO customers VALUES
('c-1', 'Ada Lovelace', 'ada@example.com', 'London', 'pro', '2025-01-10 09:00:00'),
('c-2', 'Alan Turing', 'alan@example.com', 'Wilmslow', 'free', '2025-02-14 10:30:00'),
('c-3', 'Grace Hopper', 'grace@example.com', 'New York', 'enterprise', '2025-03-01 08:15:00'),
('c-4', 'Linus Torvalds', 'linus@example.com', 'Helsinki', 'free', '2025-04-20 12:00:00');
INSERT INTO products VALUES
('A-1', 'Standing Desk', 'furniture', 349.00, 12, '["office", "ergonomic"]', '{"color": "oak", "width_cm": 160, "adjustable": true}'),
('A-2', 'Office Chair', 'furniture', 189.50, 30, '["office", "ergonomic", "sale"]', '{"color": "black", "mesh": true}'),
('B-7', 'Desk Lamp', 'lighting', 24.99, 150, '["office", "led"]', '{"color": "white", "watts": 9}'),
('C-3', 'USB-C Hub', 'electronics', 39.00, 80, '["usb", "sale"]', '{"ports": 7, "pd_watts": 100}'),
('C-9', 'Monitor 27in', 'electronics', 279.00, 25, '["display", "office"]', '{"diagonal_in": 27, "panel": "IPS", "hdr": false}');
INSERT INTO orders VALUES
(1001, 'c-1', 'shipped', '2026-01-05 10:00:00'),
(1002, 'c-1', 'open', '2026-02-11 15:20:00'),
(1003, 'c-2', 'shipped', '2026-02-12 09:05:00'),
(1004, 'c-3', 'shipped', '2026-02-20 18:45:00'),
(1005, 'c-3', 'cancelled', '2026-03-02 11:11:00'),
(1006, 'c-4', 'open', '2026-03-15 14:00:00'),
(1007, 'c-1', 'shipped', '2026-03-18 08:30:00'),
(1008, 'c-2', 'open', '2026-03-25 19:00:00');
INSERT INTO order_items VALUES
(1001, 'A-1', 1, 349.00), (1001, 'B-7', 2, 24.99),
(1002, 'C-9', 1, 279.00),
(1003, 'C-3', 3, 39.00),
(1004, 'A-2', 4, 189.50), (1004, 'A-1', 2, 349.00),
(1005, 'C-9', 2, 279.00),
(1006, 'B-7', 1, 24.99), (1006, 'C-3', 1, 39.00),
(1007, 'A-2', 1, 189.50),
(1008, 'B-7', 5, 24.99);
Load it into the running container, then check the row counts:
docker exec -i mysql-lab mysql -uroot -psecret --default-character-set=utf8mb4 < shop.sql
docker exec mysql-lab mysql -uroot -psecret -t -e "SELECT (SELECT COUNT(*) FROM shop.customers) AS customers, (SELECT COUNT(*) FROM shop.products) AS products, (SELECT COUNT(*) FROM shop.orders) AS orders, (SELECT COUNT(*) FROM shop.order_items) AS order_items"
+-----------+----------+--------+-------------+
| customers | products | orders | order_items |
+-----------+----------+--------+-------------+
| 4 | 5 | 8 | 11 |
+-----------+----------+--------+-------------+
Notes on the schema choices, since they recur later:
-
Tables use InnoDB, the default; the
ENGINEclause is therefore omitted. -
order_itemshas a composite primary key(order_id, sku), which InnoDB also uses as the physical order of the rows (see Indexing). -
CHECKconstraints are enforced; before MySQL 8.0.16 they were parsed and ignored. -
products.tagsandproducts.attrsuse the nativeJSONtype, covered on JSON.
A first session
Open an interactive client with docker exec -it mysql-lab mysql -uroot -psecret --commands and look around.
The statements below were run in batch mode with -t, which prints the same tables.
USE shop;
SHOW TABLES;
DESCRIBE products;
+----------------+
| Tables_in_shop |
+----------------+
| customers |
| order_items |
| orders |
| products |
+----------------+
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| sku | varchar(10) | NO | PRI | NULL | |
| name | varchar(100) | NO | | NULL | |
| category | varchar(50) | NO | | NULL | |
| price | decimal(10,2) | NO | | NULL | |
| stock | int | NO | | NULL | |
| tags | json | NO | | NULL | |
| attrs | json | NO | | NULL | |
+----------+---------------+------+-----+---------+-------+
A filter and an aggregate over a three-table join (top three orders by value):
SELECT sku, name, price FROM products WHERE category = 'electronics' ORDER BY price DESC;
SELECT c.name, o.id AS order_id, o.status, SUM(i.qty * i.price) AS total
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN order_items i ON i.order_id = o.id
GROUP BY c.name, o.id, o.status
ORDER BY total DESC
LIMIT 3;
+-----+--------------+--------+
| sku | name | price |
+-----+--------------+--------+
| C-9 | Monitor 27in | 279.00 |
| C-3 | USB-C Hub | 39.00 |
+-----+--------------+--------+
+--------------+----------+-----------+---------+
| name | order_id | status | total |
+--------------+----------+-----------+---------+
| Grace Hopper | 1004 | shipped | 1456.00 |
| Grace Hopper | 1005 | cancelled | 558.00 |
| Ada Lovelace | 1001 | shipped | 398.98 |
+--------------+----------+-----------+---------+
Vertical output and a JSON path (\G needs --commands):
SELECT * FROM customers WHERE id = 'c-3'\G
SELECT name, JSON_EXTRACT(attrs, '$.color') AS color FROM products WHERE sku = 'A-1';
*************************** 1. row ***************************
id: c-3
name: Grace Hopper
email: grace@example.com
city: New York
tier: enterprise
created_at: 2025-03-01 08:15:00
+---------------+-------+
| name | color |
+---------------+-------+
| Standing Desk | "oak" |
+---------------+-------+
The same joins, aggregates and window functions you know from standard SQL work unchanged; for the engine-agnostic material see the SQL reference. What is specific to MySQL starts on Data Types & MySQL-specific SQL.
Checking versions
Four places tell you which server you are talking to, and they matter when a feature is documented "since version X":
SELECT VERSION(), @@version_comment, @@innodb_version;
+-----------+------------------------------+------------------+
| VERSION() | @@version_comment | @@innodb_version |
+-----------+------------------------------+------------------+
| 26.10.0 | MySQL Community Server - GPL | 26.10.0 |
+-----------+------------------------------+------------------+
From a shell, mysql --version prints the client version and mysqladmin version the server’s; both
reported 26.10.0 here. The manual is versioned too: pick the matching release at
dev.mysql.com/doc/refman (this reference was checked against the 26.10
manual and, for the release model, the 8.4 manual).
Where to go next
-
Architecture — what happens inside
mysqldand InnoDB. -
Data Types & MySQL-specific SQL — types,
sql_modeand the MySQL-only statements. -
MySQL Reference — the full list of pages and a suggested reading order.