Sub topic 1
How a Database Server Works
Prerequisites
This chapter assumes you can use the Linux command line, edit files with sudo, and run basic SQL. You should also know that an IP address identifies a host and a port identifies a network service. The examples use appdb as a database name and PostgreSQL terminology where the engines differ.
The client/server model
A database server is a program that accepts database requests, executes SQL, and returns results. A client is a program that sends those requests. The client might be an application, a command-line tool such as psql or mysql, or an administration tool.
The server normally runs continuously as a system service. It listens on a port, such as PostgreSQL's default port 5432 or MySQL's default port 3306. A client connects using a host name or IP address, a port, a database name, and authentication credentials.
The server does more than read files. It parses SQL, checks whether the authenticated user has permission, chooses an execution plan, manages transactions, and coordinates concurrent clients. A transaction groups changes into an atomic unit. With a committed transaction, the server makes the changes durable according to its storage settings. With a rolled-back transaction, it discards those changes.
Processes, connections, and data
The database server uses operating-system processes or threads, depending on the engine and configuration. PostgreSQL commonly uses a main server process and separate backend processes for client sessions. MySQL commonly uses a server process with threads for client work. These implementation details affect memory use, connection limits, and troubleshooting.
The data directory contains files that hold tables, indexes, transaction information, and internal metadata. It is different from the directory containing configuration files. Never edit data-directory files manually. Use SQL or supported administrative commands.
| Component | Responsibility |
|---|---|
| Server process | Accepts connections and coordinates database work |
| Client session | Sends requests on behalf of one user or application connection |
| Data directory | Stores durable database contents and internal state |
| Configuration | Controls ports, memory, authentication, and logging |
A local client may connect through a Unix-domain socket instead of TCP. A remote client normally connects through TCP. In both cases, the server still authenticates the user and checks privileges.
What happens during a request
A typical request follows this path:
- The client opens a connection and identifies the database and user.
- The server authenticates the user.
- The server checks permissions for the requested object.
- The parser and planner turn SQL into an execution plan.
- The storage layer reads or changes data while enforcing transaction rules.
- The server returns rows, a success status, or an error.
PostgreSQL and MySQL terminology
Both engines provide databases, tables, indexes, transactions, users, and replication features. PostgreSQL uses roles, and a role can own objects or log in. MySQL uses accounts identified by a user and host combination, such as 'appuser'@'10.0.0.%'. The exact commands differ, but the operating model is similar.
Exercise
An application can connect to the server but receives “permission denied” when selecting from a table. Which stage succeeded, and which stage failed?
Show answer
Authentication succeeded, but authorization failed. The account needs a suitable privilege on the table or its schema.