The Statements
Log in as an administrator: sudo mysql on Ubuntu, sudo mariadb on MariaDB, or mysql -u root -p where root has a password, typed at the prompt. Then:
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON app.* TO 'app_user'@'localhost';Replace STRONG_ with a long random password, and type it only inside the SQL shell, never on a shell command line, where it stays in the history and shows in the process list. No FLUSH PRIVILEGES is needed after CREATE USER or GRANT; they take effect at once.
The Host Part
An account is a user name plus a host. The host says where the account may connect from:
'localhost': connections from the server itself, the normal case for an application on the same machine.'10.0.0.5': connections from one other address, for example an application server on a private network.'%': any host. Avoid it: the account can then be tried from everywhere the port is reachable.
'app_ and 'app_ are two separate accounts with their own passwords and privileges. Connections from another server need more than an account; see how to allow remote connections to MySQL.
Grant Only What Is Needed
app.* means every table in the database app. An application that reads and writes data but never changes the schema needs less than all privileges:
GRANT SELECT, INSERT, UPDATE, DELETE ON app.* TO 'app_user'@'localhost';A reporting account may get SELECT only. Many applications run their own schema migrations and then need CREATE, ALTER, INDEX, and DROP on their database as well; ALL PRIVILEGES ON app.* covers that and still stops at that one database.
Avoid grants on *.* for application accounts, and do not add WITH GRANT OPTION: it lets the account hand its rights to others.
Check the Result
SHOW GRANTS FOR 'app_user'@'localhost';The first line, GRANT USAGE ON *.*, means "may log in" and nothing more. The lines after it are the real rights. Then test the login from the shell, letting the client ask for the password:
mysql -u app_user -p appTo see every account on the server, see how to list users in MySQL.
Revoke a Privilege
REVOKE DELETE ON app.* FROM 'app_user'@'localhost';
REVOKE ALL PRIVILEGES ON app.* FROM 'app_user'@'localhost';A revoke must name the same level as the grant: a right granted on app.* is removed on app.*, not on *.*. Sessions that are already open keep the database-level rights they had until they reconnect or change database, so restart the application if the change must apply at once.
Change a Password or Lock an Account
ALTER USER 'app_user'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD';
ALTER USER 'app_user'@'localhost' ACCOUNT LOCK;
ALTER USER 'app_user'@'localhost' ACCOUNT UNLOCK;Update the application's configuration in the same step, or it fails at its next connection.
Common Errors
- Error 1396, operation CREATE USER failed: the account already exists. Use
CREATE USER IF NOT EXISTS, orALTER USERto change it. - GRANT creates no user: on MySQL 8.0 and later,
GRANTno longer creates a missing account; runCREATE USERfirst. - Access denied when logging in: compare the host in the message with the host of the account; see MySQL access denied for user.
To remove an account later, follow how to delete a user in MySQL.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations