Lesson 02 beginner

Installing MySQL

Install MySQL 8.4 or 9.7 LTS on Ubuntu, Debian, RHEL and Rocky Linux from the official repositories, on macOS with Homebrew, on Windows with the installer, or run it in Docker; then secure the fresh server, find its configuration and data directory, and create the orbit database and account used in the rest of the course.

In this lesson you get a MySQL server running, lock it down and create the orbit database and account the rest of the course uses. Pick the section for your system; you only need one.

Choose a version

Install an LTS release: 8.4 if your hosting or framework documents it, otherwise 9.7. Both are in Oracle’s official repositories. Distribution repositories can lag behind or ship MariaDB under the mysql name, so the official ones are the safer choice.

Ubuntu and Debian

Oracle publishes an APT configuration package that adds its repository. Download the latest mysql-apt-config .deb from the MySQL APT repository page (dev.mysql.com/downloads/repo/apt/), then:

$ sudo dpkg -i mysql-apt-config_*_all.deb
$ sudo apt update
$ sudo apt install mysql-server

During dpkg -i a menu asks which series to track: choose mysql-8.4-lts or mysql-9.7-lts. The install asks for a root password and starts the service.

$ systemctl status mysql
$ mysql --version
mysql  Ver 9.7.4 for Linux on x86_64 (MySQL Community Server - GPL)

Tip: Ubuntu’s own mysql-server package (without Oracle’s repository) is also real MySQL, usually a slightly older LTS. It is fine for learning; production servers usually prefer the official repository to get fixes sooner.

RHEL, Rocky Linux, AlmaLinux and Oracle Linux

These systems ship a mysql module in AppStream that may be older. Use Oracle’s Yum repository instead:

$ sudo dnf install https://dev.mysql.com/get/mysql84-community-release-el9-1.noarch.rpm
$ sudo dnf module disable mysql
$ sudo dnf install mysql-community-server
$ sudo systemctl enable --now mysqld

The release RPM name contains the series and the EL version; check the Yum repository page for the current file name. On first start the server generates a temporary root password and writes it to the error log:

$ sudo grep 'temporary password' /var/log/mysqld.log
2026-11-11T09:12:44.512031Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: k;9Hs_wP2>rX

You must change it on first login before you can run any other statement.

Warning: In 2026 a repository update made some 8.4 servers jump to 9.7 during a routine dnf update. Check which LTS track is enabled with dnf repolist enabled | grep mysql before updating packages on a production server, and pin the track you want.

macOS

With Homebrew:

$ brew install mysql
$ brew services start mysql
$ mysql -u root

Homebrew’s mysql formula follows the newest release; install mysql@8.4 to stay on that LTS line. A Homebrew install starts with an empty root password for local connections, so run mysql_secure_installation (below) right away. Oracle also offers a .dmg installer with a System Settings pane.

Windows

Download the MySQL Installer or the MSI for your series from dev.mysql.com/downloads. The wizard:

  1. Installs the server and, if you choose, MySQL Shell and Workbench.
  2. Asks for a root password and optional extra accounts.
  3. Registers MySQL as a Windows service that starts with the system.

Afterwards open “MySQL 9.7 Command Line Client” from the Start menu, or add C:\Program Files\MySQL\MySQL Server 9.7\bin to PATH to run mysql in any terminal.

Docker

Containers are the fastest way to get a disposable server for learning:

$ docker run -d --name orbit-mysql \
    -e MYSQL_ROOT_PASSWORD=change-me \
    -p 3306:3306 \
    -v orbit-mysql-data:/var/lib/mysql \
    mysql:9.7
$ docker exec -it orbit-mysql mysql -u root -p
  • mysql:8.4 and mysql:9.7 track the latest patch of each LTS series. Pin a full version such as mysql:9.7.4 in production so containers do not upgrade on their own.
  • The named volume orbit-mysql-data keeps your data when the container is removed.
  • MYSQL_DATABASE, MYSQL_USER and MYSQL_PASSWORD create a database and account on first start, but this lesson does that by hand so you learn the SQL.

A minimal compose.yaml for local development:

services:
  db:
    image: mysql:9.7
    environment:
      MYSQL_ROOT_PASSWORD: change-me
    ports:
      - "3306:3306"
    volumes:
      - db-data:/var/lib/mysql
volumes:
  db-data:

Secure the new server

Package installs come with a helper that tightens the defaults:

$ sudo mysql_secure_installation

It walks you through:

  • Enabling the validate_password component, which rejects weak passwords.
  • Setting or changing the root password.
  • Removing anonymous accounts.
  • Disallowing remote root login.
  • Dropping the test database.

Answer yes to all of them on any server that is not a throwaway container.

Note: On Ubuntu and Debian, root@localhost often uses the auth_socket plugin: sudo mysql logs you in as root without a password because you are root on the operating system. That is safe and convenient for administration; applications should never connect as root anyway.

Where things live

ItemTypical location (Linux)
Configuration/etc/mysql/my.cnf and /etc/mysql/mysql.conf.d/ (Debian/Ubuntu), /etc/my.cnf and /etc/my.cnf.d/ (RHEL family)
Data directory/var/lib/mysql
Error log/var/log/mysql/error.log or /var/log/mysqld.log
Socket/var/run/mysqld/mysqld.sock or /var/lib/mysql/mysql.sock
Servicemysql (Debian/Ubuntu) or mysqld (RHEL family)

You can always ask the server itself:

SELECT @@datadir, @@port, @@socket, @@log_error;
+-----------------+--------+-----------------------------+--------------------------+
| @@datadir       | @@port | @@socket                    | @@log_error              |
+-----------------+--------+-----------------------------+--------------------------+
| /var/lib/mysql/ |   3306 | /var/run/mysqld/mysqld.sock | /var/log/mysql/error.log |
+-----------------+--------+-----------------------------+--------------------------+
1 row in set (0.00 sec)

The service is managed with systemd on Linux:

$ sudo systemctl stop mysql
$ sudo systemctl start mysql
$ sudo systemctl restart mysql
$ sudo journalctl -u mysql --since today

Create the Orbit database and account

Log in as root and create a database plus an account for the app. Every lesson from now on uses them.

$ sudo mysql
CREATE DATABASE orbit CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
CREATE USER 'orbit'@'localhost' IDENTIFIED BY 'Orbit-dev-2026!';
GRANT ALL PRIVILEGES ON orbit.* TO 'orbit'@'localhost';
Query OK, 1 row affected (0.01 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.01 sec)
  • utf8mb4 stores any Unicode character, including emoji. Lesson 4 explains character sets and collations.
  • A MySQL account is user plus host: 'orbit'@'localhost' may only connect from the same machine. Lesson 18 covers accounts in depth.
  • orbit.* means every object in the orbit database, including tables created later.

Now connect as the new account:

$ mysql -u orbit -p orbit

The last orbit is the default database to use. If you see the mysql> prompt, you are ready for lesson 3.

Quick reference

TaskCommand
Install (Debian/Ubuntu)sudo apt install mysql-server
Install (RHEL family)sudo dnf install mysql-community-server
Start in Dockerdocker run -d -e MYSQL_ROOT_PASSWORD=... mysql:9.7
Secure a new installsudo mysql_secure_installation
Service controlsudo systemctl restart mysql
Find temporary root passwordsudo grep 'temporary password' /var/log/mysqld.log
Check the versionmysql --version

Exercise

You installed MySQL on Rocky Linux from Oracle’s repository. How do you log in as root the first time?

  1. With an empty password
  2. With sudo mysql, which uses auth_socket
  3. With the password “root”
  4. With the temporary password written to the error log, which you must change immediately
Show answer

Answer: D. RHEL-family packages generate a random temporary root password at first start and log it. The account is marked expired, so the first statement you run must change it.

Exercise

What does the volume option in docker run -v orbit-mysql-data:/var/lib/mysql mysql:9.7 achieve?

  1. It makes the server read its configuration from the volume
  2. Data survives when the container is deleted and recreated
  3. It limits the database size to the volume size
  4. It enables replication to another container
Show answer

Answer: B. /var/lib/mysql is the data directory. Mounting a named volume there keeps the database files outside the container’s writable layer.

Exercise

Which account can connect to the server from another machine on the network?

  1. ‘orbit’@’localhost’
  2. ‘orbit’@’127.0.0.1’
  3. ‘orbit’@’%’
  4. Any account, as long as the password is right
Show answer

Answer: C. The host part restricts where a client may connect from. localhost and 127.0.0.1 only match local connections; % matches any host.

Key takeaways

  • Install an LTS release (8.4 or 9.7) from Oracle’s official repository, Homebrew, the Windows installer or the official Docker image.
  • Run mysql_secure_installation on every real server.
  • Know where the configuration, data directory and error log live; SELECT @@datadir, @@log_error tells you.
  • Applications get their own account limited to their own database, never root.

FAQ

I installed MySQL but SELECT VERSION() says MariaDB. What happened?

Some distributions, Debian among them, use MariaDB as their default “MySQL” (for example the default-mysql-server package). Add Oracle’s repository and install mysql-server or mysql-community-server from it to get Oracle MySQL.

Can I run two MySQL versions on one machine?

Yes, as long as each has its own data directory, port and socket. Containers make this trivial: run mysql:8.4 on port 3306 and mysql:9.7 on port 3307.

Do I need MySQL Workbench?

No. Everything in this course uses the mysql command-line client, which is always available on the server. Workbench, DBeaver, TablePlus and similar graphical tools are handy for browsing data and drawing diagrams, and you can use them alongside the terminal.

The server does not start after I edited my.cnf. Where do I look?

In the error log: sudo tail -50 /var/log/mysql/error.log or journalctl -u mysql. A misspelled option name is the most common cause; the log names the exact line MySQL did not understand.

↑ ↓ navigate Enter open Esc close