What does the host part in 'user'@'host' mean in MySQL?

IntermediateConceptMySQL
Answer

Short answer

In MySQL, an account is a username plus the host it may connect from. 'shop_app'@'10.0.%' and 'shop_app'@'localhost' are two different accounts with possibly different passwords and permissions. The host can be an IP, a hostname, a wildcard pattern or % (anywhere).

Examples

Account Can connect from
'app'@'localhost' Only the same machine (via socket)
'app'@'10.0.11.25' One specific IP
'app'@'10.0.%' Any 10.0.x.x address
'app'@'%' Anywhere (needs firewall protection)

The classic error

ERROR 1045 (28000): Access denied for user 'shop_app'@'10.0.12.8' (using password: YES)

The password may be right — but the account exists only as 'shop_app'@'10.0.11.%', and the app moved to a new subnet. Fix: create the account for the new host pattern (or widen it carefully).

Security

Restrict host patterns to the app's subnet. Combine with security groups so the port isn't reachable from elsewhere anyway.

What to say in the interview

"MySQL accounts are user@host; the host limits where the account can connect from, and different hosts are separate accounts. A common access-denied cause is the app connecting from an IP that doesn't match the account's host pattern."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: MySQL basics

More on MySQL