MySQL in One Paragraph
MySQL keeps data in databases. Each database holds tables, each table has named columns of fixed types, and each record is a row. Programs do not open the data files themselves: they connect to the MySQL server, send SQL statements, and get rows back. The server handles many connections at once, checks who may do what, and keeps the data consistent when several programs write at the same time.
Tables and SQL
A short example creates a table, adds a row, and reads it back:
CREATE TABLE customers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO customers (name) VALUES ('Example Ltd');
SELECT id, name FROM customers WHERE name LIKE 'Ex%';
"Relational" means tables can refer to each other: an orders table stores a customer ID, and one query joins both tables to list orders with customer names. SQL is the standard language for this, so most of what you learn carries over to other relational databases.
Server and Client
- The server:
mysqldruns as a service. It listens on a Unix socket for local programs and, if configured, on TCP port 3306. - The client:
mysqlis the command-line client. Applications use a client library for their language instead. - The data: on Linux it lives under
/var/lib/mysql, owned by themysqlsystem user. It is changed only through the server, never by editing files. - Accounts: MySQL has its own users, separate from Linux users, each tied to a host, such as
'app_user'@'localhost'.
Most applications run on the same machine as the database and connect through localhost. See what is localhost for why that address never leaves the server.
InnoDB, the Storage Engine
MySQL hands the storage of each table to a storage engine. The default, and the right choice for almost every table, is InnoDB. It provides:
- Transactions: several changes succeed or fail together.
- Row-level locking: writers to different rows do not block each other.
- Crash recovery: after a power cut, the server replays its log and comes back consistent.
- Foreign keys: the database itself refuses an order for a customer that does not exist.
The memory InnoDB uses for caching, the buffer pool, is the main setting to size on a server.
Where MySQL Is Used
MySQL is the M in the LAMP stack (Linux, Apache, MySQL, PHP) and in LEMP, where Nginx replaces Apache. Content management systems such as WordPress, Drupal, and Joomla store their content in it, as do online stores, forums, and many custom applications in PHP, Python, Node.js, Java, and Go. To set up the PHP side, see how to install PHP on Ubuntu.
MySQL and MariaDB
MariaDB began in 2009 as a fork of MySQL, started by some of MySQL's original developers around the time MySQL changed owners. The two are still largely compatible in SQL, protocol, and tools, but they have diverged in features and version numbers, so check which one a server runs before following version-specific instructions.
Many distributions ship MariaDB under the mysql command name. How to check the MySQL version shows how to tell.
Running MySQL on Your Own Server
- Install it from the distribution: see how to install MySQL on Ubuntu.
- Keep it local: leave the server listening on 127.0.0.1 and port 3306 closed in the firewall.
- One account per application: with rights on its own database only, never root.
- Back up with dumps: copying the data folder of a running server does not give a consistent backup.
When Something Else Fits
For a small program on one machine with few writers, an embedded database such as SQLite needs no server at all; the comparison is in SQLite vs MySQL. PostgreSQL is the other widely used open-source relational database and a common choice when an application already supports it.