Quick revision
The short answer
First we learn what each service really does: we tag every service's connections with a program name, and use performance_schema or an audit log to see which tables and statement types each one uses. Then we create one account per service and environment, grouped with MySQL 8 roles, restricted by host, and with migrations on a separate account. We move one service at a time with the old account kept as a quick rollback, and finally lock, monitor and drop the shared account.
What to remember
In this answer, we turned a shared ALL PRIVILEGES account into per-service accounts without an outage. We tagged connections with program names and used performance_schema digests or an audit log to learn real usage, built MySQL 8 roles and host-restricted accounts with separate migration logins, remembered SET DEFAULT ROLE, moved one service at a time with a canary and the old account as rollback, and finally locked, monitored and dropped the shared account.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
First we learn what each service really does: we tag every service's connections with a program name, and use performance_schema or an audit log to see which tables and statement types each one uses. Then we create one account per service and environment, grouped with MySQL 8 roles, restricted by host, and with migrations on a separate account. We move one service at a time with the old account kept as a quick rollback, and finally lock, monitor and drop the shared account.
Why this question is asked
A shared account with ALL PRIVILEGES is one of the most common audit findings, and it is hard to fix precisely because it works. Nobody wants to be the person who removes a grant and breaks checkout at peak time. Interviewers ask this scenario because the answer shows whether we can turn a security principle into a careful migration with measurement, small steps and a rollback path. In this answer, we will see why the shared account is dangerous, how to discover what each service needs, how MySQL 8 roles and host restrictions shape the new accounts, how to roll out without an outage, and how to retire the old account safely.
1Why the Shared Account Is a Problem
ALL PRIVILEGES on *.* in MySQL means the account can read and change every table in every schema, create and drop databases, and often grant privileges to others or read files on the server. Thirty services sharing it means that a bug or leaked password in the least careful of the thirty exposes all of them. It also means the database logs cannot tell us which service ran a statement, because they all appear as the same user.
Least privilege means each account has only the privileges its job needs. The hard part here is the phrase nobody knows which service needs what, so the first job is measurement, not grants.
2Learning What Each Service Actually Uses
Because all services use one user name, we need another way to tell their connections apart. MySQL Connector/J can send connection attributes when it connects, and MySQL stores them in performance_schema. Adding a program name to each service's JDBC URL is a harmless change that we can roll out first.
Statement digests are normalised statement texts with the values replaced by question marks, so thousands of similar queries collapse into one row. The history table is a ring buffer, so in practice we sample it every minute for a week or two into our own table, long enough to include month-end jobs and rare admin endpoints. Where an audit log plugin is available (MySQL Enterprise Audit, or a third-party plugin, check your version), it records every statement with the connection's details and is even better evidence.
We combine this with reading code: the service's repositories, native queries and migration scripts. The result for each service is a short list such as SELECT, INSERT and UPDATE on orders.*, plus SELECT on catalog.products.
3Designing Per-Service Accounts With Roles
MySQL 8 roles are named bundles of privileges that we grant to accounts. They let us describe access once, for example orders_rw, and give it to the production and staging accounts of the same service.
Notice the last line. In MySQL a granted role is not active in a new session unless it is a default role or activate_all_roles_on_login is on. Forgetting SET DEFAULT ROLE is the classic reason a freshly migrated service fails with SELECT command denied even though the grants look right.
The host part of each account, such as '10.20.%', is part of the account's identity in MySQL. Restricting it means a stolen password is useless from a laptop outside the application network. Separating orders_migrate from orders_svc means the running service cannot alter or drop tables, even though its migrations can.
4Rolling Out One Service at a Time
Now the change itself. The safe pattern is to move one service, watch it, and only then move the next, starting with low-risk internal services before the payment path.
- Measure usagea week or two of digests and code review
- Create role and accountgrants, host limit, default role
- Deploy one canarynew credentials from the secret store
- Permission errors?1142, 1143, 1044 in logsYesAdd grant or roll back canaryNoRoll out to all instances
- Next servicerepeat until no traffic uses the old account
5 steps in order. Play them, or step through them one at a time.
For each service we store the new credentials in the secret store, deploy one instance (a canary) with them, and watch for permission errors: MySQL error 1142 (command denied for a table), 1143 (for a column) and 1044 (access denied to a database). If something was missed, such as a nightly job that reads a table nobody remembered, we either add the grant or switch the canary back to the old account, which is still valid. When the canary is clean for a full business cycle, we roll the new account to all instances.
So the shared account stays alive during the whole migration as the rollback path. Its value drops every week, because fewer services use it, and we can see that drop directly in the connection attributes.
5Retiring the Shared Account
When no connection attribute shows the shared account in use for some weeks, we retire it in two stages. First we lock it, which is reversible in one statement: ALTER USER 'shared'@'%' ACCOUNT LOCK. Any forgotten client now fails with a clear account-locked error instead of silently succeeding, and we alert on those attempts through the error log or audit log. After a quiet period we drop it with DROP USER.
We also add a standing alert on any login with the old name or on any new account with global privileges, so the problem cannot quietly return when the next service is created in a hurry.
6Follow-up questions
Why not just revoke privileges from the shared account step by step?
Because all thirty services depend on the same grants, every revoke is a change for all of them at once, with no way to test it on one service. Separate accounts let us change one service at a time and roll back by switching credentials, which is far safer.
What about services that create tables at runtime, such as Quartz or Spring Batch?
Their schema should be created by migrations under the migration account, and the runtime account then only needs DML on those tables. If a library insists on DDL at start-up, we turn that option off and provide its scripts as migrations, so the running service still has no DDL rights.
How do you keep the new grants from drifting back to broad access?
We keep the roles and grants in code, for example SQL files or Terraform, applied by a pipeline, and run a scheduled check that compares SHOW GRANTS for each account with the declared version and alerts on any difference.
7Conclusion
In this answer, we turned a shared ALL PRIVILEGES account into per-service accounts without an outage. We tagged connections with program names and used performance_schema digests or an audit log to learn real usage, built MySQL 8 roles and host-restricted accounts with separate migration logins, remembered SET DEFAULT ROLE, moved one service at a time with a canary and the old account as rollback, and finally locked, monitored and dropped the shared account.
Open with
I would start by measuring, because nobody knows what each service needs. Since they all share one user, I would first add a connectionAttributes program_name to every service's JDBC URL, which is harmless.
Then, if they want more
Then I can join performance_schema statement history to session_connect_attrs, sample it for a week or two, and get each service's tables and statement types, backed up by an audit log plugin if we have one and by reading the code.
Next I create one account per service per environment, with MySQL 8 roles such as orders_rw holding the grants, host restrictions in the account name, and a separate migration account with DDL so the runtime account has none. I make sure to set the default role, because otherwise the role is not active on login.
The rollout is service by service: deploy one canary with the new account, watch for errors like 1142 command denied, add a missed grant or roll back to the old account, and only then move all instances. The shared account stays valid until the end as the rollback path.
When nothing uses it, I lock it first, alert on any attempts, and drop it after a quiet period. Finally the grants go into code with a drift check, so it does not happen again.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "An audit finds that 30 services share one MySQL account with ALL PRIVILEGES, and nobody...": https://scrivio-interview-library.netlify.app/databases/database-security/f997409a46/