Database Clients, Servers and Files
What you will learn
- The difference between an embedded database and a client/server database
- What a client, a server, a host and a port are
- What travels between client and server
- When each style fits
1. Style one: a file used by your program
With SQLite the DBMS is a library inside your program. The database is a file, and your program reads and writes it directly through the library. No other process is needed.
import sqlite3, os, tempfile
d = tempfile.mkdtemp(); path = os.path.join(d, "app.db")
con = sqlite3.connect(path)
con.execute("CREATE TABLE t (x INTEGER)"); con.commit(); con.close()
print(sorted(os.listdir(d)))
print(open(path, "rb").read(15))
['app.db']
b'SQLite format 3'
One file, app.db, and its first bytes say which format it is. The SQLite documentation describes this as "serverless": programs read and write the file directly instead of talking to a separate server process.
2. Style two: a server and clients
PostgreSQL, MySQL and MongoDB normally run as a separate server program that waits for connections. Your program is a client that connects, sends a request, and gets an answer back.
| Term | Meaning |
|---|---|
| Server | the program that owns the data and waits for requests |
| Client | any program that connects and asks (your app, a command-line tool) |
| Host | the computer (name or address) where the server runs |
| Port | a number that tells the computer which program on it should receive the connection |
Each product has a default port: PostgreSQL's documentation says its server listens on 5432 by default, and MongoDB's documentation gives 27017 for mongod. Administrators can change them.
To feel the pattern without installing anything, here is a tiny pretend server and client using Python sockets. The "database" just answers by repeating the request:
import socket, threading
srv = socket.socket(); srv.bind(("127.0.0.1", 0)); srv.listen(1)
port = srv.getsockname()[1]
def serve():
c, _ = srv.accept()
q = c.recv(1024).decode()
c.sendall(("answer to: " + q).encode()); c.close()
threading.Thread(target=serve, daemon=True).start()
cli = socket.socket(); cli.connect(("127.0.0.1", port)); cli.sendall(b"SELECT 1")
print(cli.recv(1024).decode())
answer to: SELECT 1
127.0.0.1 means "this same computer". The client sent the text of a request and received an answer: that is all a real client does, with a far smarter server and a standard protocol.
3. Comparing the styles
| Embedded (SQLite) | Client/server | |
|---|---|---|
| Setup | none | install and run a server |
| Many users over a network | limited | designed for it |
| Access control | whoever can open the file | users and passwords on the server |
| Typical use | apps, small sites, learning, tests | websites, teams, large data |
Worked example: choose the style
- A phone app stores a user's notes on the phone: embedded, because one user and no server to run.
- A shop website with 50 staff and a public store, all using the same data: client/server, because many programs on many computers must share one database safely.
Both are made-up cases.
Common mistakes
- Exposing a database server to the whole internet with a weak password. Servers should be reachable only by the programs that need them (chapter 8).
- Using one SQLite file from many computers over a network share. It is designed for programs on the same machine.
- Confusing the port with the database name. The port picks the server program; the database name picks the data inside it.
Key takeaways
- An embedded database lives in a file used directly by your program.
- A client/server database is a separate program that clients connect to by host and port.
- Clients send requests and receive answers.
- Choose by users, network and setup effort.
Practice
1. What is a port?
Show answer
A number identifying which program on a computer should receive a network connection.
2. Which style needs no separate server program?
Show answer
Embedded, like SQLite.
3. What does 127.0.0.1 mean?
Show answer
The same computer the program is running on.
4. In the demo, what did the client send and receive?
Show answer
It sent SELECT 1 and received answer to: SELECT 1.
5. Why is a client/server database better for 50 staff using one shop database?
Show answer
It is designed for many users over a network, with accounts and permissions managed by the server.
6. Name one risk of making a database server reachable from the whole internet.
Show answer
Anyone could try to guess passwords or exploit weaknesses; servers should only be reachable by programs that need them.