متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 60Data · PostgreSQL · MySQL

How to see who is connected to a database

"Too many connections", a database that is suddenly slow, a migration that waits forever — the first question is always: who is connected, and what are they doing? PostgreSQL shows it in pg_stat_activity, MySQL in the process list. This how-to lists connections by user and state, finds long queries and the sessions that block others, and stops one session safely.

  • 5 الدروس
  • 6 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

60.1

The quick answer

PostgreSQL:

psql
SELECT pid, usename, datname, client_addr, state, now() - query_start AS runtime, left(query, 60) AS query
FROM pg_stat_activity WHERE backend_type = 'client backend' ORDER BY runtime DESC NULLS LAST;

MySQL / MariaDB:

mysql
SHOW FULL PROCESSLIST;

60.2

Step by step

  1. PostgreSQL: count by user and state — the overview first:
psql
SELECT usename, state, count(*) FROM pg_stat_activity
WHERE backend_type = 'client backend' GROUP BY 1, 2 ORDER BY 3 DESC;
SHOW max_connections;
StateMeans
activeRunning a query now.
idleConnected, doing nothing (normal for a connection pool).
idle in transactionOpened a transaction and waits — holds locks. Many of these = an app bug.
  1. Who blocks whom (PostgreSQL):
psql
SELECT pid, usename, pg_blocking_pids(pid) AS blocked_by, state, left(query, 60) AS query
FROM pg_stat_activity WHERE cardinality(pg_blocking_pids(pid)) > 0;
  1. Stop a session — first only the query, then (if needed) the whole connection:
psql
SELECT pg_cancel_backend(12345);
SELECT pg_terminate_backend(12345);
  1. MySQL / MariaDB: the same questions:
mysql
SELECT user, host, db, command, time, state, LEFT(info, 60) AS query
FROM information_schema.processlist WHERE command <> 'Sleep' ORDER BY time DESC;
SHOW STATUS LIKE 'Threads_connected';
SHOW VARIABLES LIKE 'max_connections';
KILL QUERY 4711;
KILL 4711;

KILL QUERY stops the statement, KILL closes the connection.

  1. From the operating system — which hosts connect to the port:
ss -tn state established '( sport = :5432 )' | awk 'NR>1 {split($4,a,":"); print a[1]}' | sort | uniq -c | sort -rn
  1. Prevent the problems: a connection pool (PgBouncer for PostgreSQL, ProxySQL for MySQL), per-user limits, and timeouts:
psql
ALTER ROLE shop CONNECTION LIMIT 80;
ALTER DATABASE shop SET idle_in_transaction_session_timeout = '5min';
ALTER ROLE report SET statement_timeout = '60s';

60.3

Check that it worked

After the fix, the counts are below max_connections, there are no long idle in transaction sessions, nothing is blocked, and the app's error rate is normal.

60.4

Common problems

ProblemCauseFix
FATAL: sorry, too many clients alreadyAll connection slots used.Find the user with the most connections; use a pooler; fix the app's pool size.
Many idle in transaction sessionsThe app opens transactions and does not commit.Fix the app; idle_in_transaction_session_timeout.
A migration waits foreverIt needs a lock that a long session holds.pg_blocking_pids; finish or cancel the blocker; set lock_timeout for migrations.
pg_terminate_backend returns false / no rightYou are not superuser or the same role.Run as an admin, or give the role pg_signal_backend.
MySQL Too many connectionsmax_connections reached; sleeping connections from apps.Lower wait_timeout for apps; pool; raise the limit only if RAM allows.
You killed the wrong sessionSimilar queries from many PIDs.Check user, client address and query before you kill.

60.5

Go further

1What does the state idle in transaction mean, and why is it dangerous?

The session opened a transaction and waits; it can hold locks that block other queries.

2What is the difference between pg_cancel_backend and pg_terminate_backend?

Cancel stops the running query; terminate closes the whole connection.

كل الأوامر في هذا الفصل