Chapter 1
Chapter 1 — Installing MySQL on Ubuntu / Debian
MySQL is the database engine that runs behind the overwhelming majority of the web — WordPress, Joomla, phpBB, custom PHP applications — all store their data in MySQL tables. Before you can create databases, run queries, or connect web applications, you need a running MySQL server. This chapter gets it installed, started, and verified on Ubuntu and Debian systems.
What MySQL Is — and Where It Fits
A relational database management system (RDBMS) stores data in tables — rows and columns, like a spreadsheet, but with strict structure and the ability to link tables together. MySQL is the world's most widely deployed open-source RDBMS. It handles everything from a personal blog's twenty posts to a billion-row e-commerce catalogue.
MySQL runs as a background service (mysqld). Your PHP code, Python scripts, or admin tools connect to it over a socket or TCP connection, send SQL commands, and receive results. MySQL itself never touches your web files — it only speaks SQL.
MySQL vs MariaDB — Know Before You Install
MariaDB was created in 2009 as a community fork of MySQL after Oracle acquired MySQL AB. The two share the same SQL syntax, the same client tools, and are largely compatible — but they are different software with diverging internals. On Debian, the default package default-mysql-server installs MariaDB, not MySQL. On Ubuntu, mysql-server installs MySQL.
mysql-server on Ubuntu, or the MySQL APT repository on Debian.Used throughout this course. Newer features: JSON columns, window functions (8.0+), InnoDB improvements.
Check you have it:
mysqld --version should say MySQL Community Server.
default-mysql-server or mariadb-server.Default on Debian. Uses
mysqld binary and mysql client — the commands look identical, which causes confusion.Check you have it:
mysqld --version says mariadb or MariaDB.
The SQL you'll write in this course runs on both. The administration details (service names, config file locations, some system variables) can differ slightly. The instructions in this chapter install MySQL specifically on both distros, so you'll be on the same version regardless of which OS you're running.
Installing MySQL
Ubuntu (20.04 / 22.04 / 24.04)
Ubuntu's official repositories ship MySQL Community Server — apt install mysql-server gets you the real thing with no extra steps.
systemctl start or systemctl enable — it's already running and set to start on boot.
Debian (11 Bullseye / 12 Bookworm)
Debian's default repositories don't include MySQL — default-mysql-server installs MariaDB instead. To get MySQL specifically, you need to add Oracle's official MySQL APT repository first.
apt upgrade — you don't need to manually download new versions in the future.Managing the MySQL Service
systemctl stop mysql before maintenance — not kill. Killing the mysqld process abruptly risks leaving data in an inconsistent state. Always use systemctl stop, which triggers a clean shutdown that flushes the InnoDB buffer pool and closes all open transactions.
Your First Login — Unix Socket Authentication
On a fresh Ubuntu install, MySQL's root account uses Unix socket authentication instead of a password. This means you log in as the MySQL root user by being the Linux root user (or using sudo) — no password required over the terminal. This is intentional and secure: root database access is only possible from the server itself, by a user who already has sudo.
mysql -u root -p — you'll be prompted for that password. If you need to connect from the terminal without a password on Debian, use sudo mysql — this also bypasses the password via the Unix socket.
What Got Installed — Files and Directories
/var/lib/mysql/ — that's where MySQL stores its binary data files. Touching them outside MySQL will corrupt your databases. All data manipulation goes through SQL or the mysqldump backup tool.
The Four Default Databases
Every fresh MySQL install comes with four databases. You'll see them in SHOW DATABASES — they're not yours to use for application data, but understanding what they are saves confusion later.
user table inside it controls who can connect and what they can do. You'll interact with this indirectly — using CREATE USER and GRANT commands (Chapter 3) rather than editing the table directly.SELECT * FROM information_schema.TABLES.performance_schema human-readable. Example: SELECT * FROM sys.processlist — shows active queries in plain English rather than raw performance counters.Essential MySQL Client Commands
SELECT, select, and Select all work. Convention is to write SQL keywords in UPPERCASE and table/column names in lowercase — it makes queries easier to read at a glance. All examples in this course follow that convention.
;. If you press Enter without one, you get a continuation prompt (->). This is a feature — you can split a long statement across multiple lines. Type ; on its own line to run what you've written so far.
Troubleshooting
apt install mysql-server includes the client. If you installed only mysql-client and need the server, run sudo apt install mysql-server. On Debian, if you only added the APT repo but didn't install, run sudo apt update && sudo apt install mysql-server. Confirm: which mysql — should return /usr/bin/mysql.
sudo systemctl status mysql — look at the Active line. If it says failed, check the error log: sudo tail -30 /var/log/mysql/error.log. Common causes: another process is using port 3306 (sudo ss -tlnp | grep 3306), or the data directory has wrong permissions (ls -la /var/lib/mysql — should be owned by mysql:mysql). Fix permissions: sudo chown -R mysql:mysql /var/lib/mysql.
sudo mysql (with sudo), not mysql -u root -p. Without sudo, MySQL sees you as your regular Linux user, not root, and rejects the connection. On Debian where you set a root password during install, use mysql -u root -p and enter that password — without the sudo prefix.
mysql-server is a virtual package that resolves to default-mysql-server → MariaDB. You must add the MySQL APT repository first (see the Debian walkthrough above). After adding the repo and running apt update, running apt install mysql-server will find the real MySQL package from Oracle's repo and install that instead of MariaDB. Confirm after install: mysqld --version should say MySQL Community Server, not MariaDB.
SHOW DATABASES shows nothing or gives access deniedsudo mysql (Ubuntu) or mysql -u root -p (Debian). If you're already root and see only information_schema, this is correct — a root user with socket auth has access to all databases, but non-root users only see databases they have privileges on. Run SHOW GRANTS; inside MySQL to see what the current user can access.
Quick Reference — Chapter 1
| Command | Purpose |
|---|---|
| sudo apt install mysql-server -y | Install MySQL on Ubuntu (includes client tools) |
| sudo systemctl start mysql | Start the MySQL service |
| sudo systemctl enable mysql | Enable MySQL to start automatically on boot |
| sudo systemctl status mysql | Check whether MySQL is running (and see recent log lines) |
| mysql --version | Print the MySQL client version |
| mysqld --version | Print the server version — use this to confirm MySQL vs MariaDB |
| sudo mysql | Connect as root via Unix socket (Ubuntu — no password needed) |
| mysql -u root -p | Connect as root with a password prompt (Debian) |
| SHOW DATABASES; | List all databases the current user can see |
| SELECT VERSION(); | Show the MySQL server version from inside the client |
| STATUS; | Show connection info, server version, and uptime |
| EXIT; / QUIT; | Leave the MySQL client |
| sudo tail -f /var/log/mysql/error.log | Watch the MySQL error log — first place to check if something's wrong |