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_PASSWORD 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_user'@'localhost' and 'app_user'@'10.0.0.5' 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 app
To 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.