To give a MySQL user access to one database, create the account with CREATE USER, then grant privileges on database_name.* with GRANT. The syntax in the example above, with IDENTIFIED BY inside GRANT, was removed in MySQL 8.0, so this page shows the current two-step method, how to check and revoke access, and how to fix the errors people run into most.
To grant a user access to a specific database in MySQL, you can use the following GRANT statement:
GRANT privileges ON database_name.* TO 'user_name'@'host' [IDENTIFIED BY 'password'];
Code language: JavaScript (javascript)
Where:
privileges: the privileges you want to grant to the user. This can be a combination of SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, and other privileges.database_name: the name of the database you want to grant the user access to.user_name: the name of the user you want to grant the privileges to.host: the host from which the user is connecting. This can be specified as a wildcard (e.g.,'%') to allow access from any host.password: the password for the user (optional).
For example, to grant a user with the username 'john' and password 'password' the ability to select data from the database 'mydatabase', the following command can be used:
GRANT SELECT ON mydatabase.* TO 'john'@'%' IDENTIFIED BY 'password';
Code language: JavaScript (javascript)
After executing the GRANT statement, you need to run the FLUSH PRIVILEGES command to reload the privileges and apply the changes:
FLUSH PRIVILEGES;
Corrected syntax for MySQL 8.0 and later
In MySQL 5.7 and earlier, GRANT ... IDENTIFIED BY created the user and set its password in one statement. MySQL 5.7 deprecated this and MySQL 8.0 removed it. On current servers the statement in the example above fails with a syntax error (ERROR 1064). You now create the account first and grant privileges as a separate step:
-- 1. Create the account (once)
CREATE USER 'john'@'%' IDENTIFIED BY 'S7rong!Passw0rd#2026';
-- 2. Grant privileges on every table in one database
GRANT SELECT ON mydatabase.* TO 'john'@'%';
-- 3. Confirm
SHOW GRANTS FOR 'john'@'%';Code language: JavaScript (javascript)
Expected output of SHOW GRANTS:
+--------------------------------------------------+
| Grants for john@% |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO `john`@`%` |
| GRANT SELECT ON `mydatabase`.* TO `john`@`%` |
+--------------------------------------------------+Code language: JavaScript (javascript)
USAGE means "no global privileges". The account can log in, but it can only touch what it was explicitly granted.
Note: FLUSH PRIVILEGES is not needed here. CREATE USER, GRANT, REVOKE and ALTER USER update the in-memory grant tables immediately. You only need FLUSH PRIVILEGES if you edit the mysql.user or mysql.db tables directly with INSERT, UPDATE or DELETE, and you should avoid doing that.
MariaDB still accepts GRANT ... IDENTIFIED BY and can create the user in the same statement unless the NO_AUTO_CREATE_USER SQL mode is on. Using CREATE USER followed by GRANT works on both MySQL and MariaDB, so it is the safer habit.
Step-by-step: give an application its own database user
- Connect as an administrative account:
mysql -u root -p(on Ubuntu or Debian with socket authentication, usesudo mysql). - Create the database if it does not exist yet:
CREATE DATABASE IF NOT EXISTS appdb; - Create the user, limited to the host it will connect from:
CREATE USER 'appuser'@'10.0.1.%' IDENTIFIED BY 'S7rong!Passw0rd#2026'; - Grant only what the application needs:
GRANT SELECT, INSERT, UPDATE, DELETE ON appdb.* TO 'appuser'@'10.0.1.%'; - Check the result:
SHOW GRANTS FOR 'appuser'@'10.0.1.%'; - Test from the client machine:
mysql -h db.example.internal -u appuser -p appdb -e "SELECT DATABASE(), CURRENT_USER();"
CURRENT_USER() returns the account MySQL actually matched, for example appuser@10.0.1.%. This helps when several accounts share a username but have different host parts.
Choosing privileges and scope
| Use case | Statement |
|---|---|
| Read-only reporting | GRANT SELECT ON appdb.* TO 'report'@'%'; |
| Typical application (CRUD) | GRANT SELECT, INSERT, UPDATE, DELETE ON appdb.* TO 'appuser'@'10.0.1.%'; |
| App that runs its own schema migrations | Add CREATE, ALTER, DROP, INDEX, REFERENCES to the CRUD list |
| Full control of one database only | GRANT ALL PRIVILEGES ON appdb.* TO 'dbowner'@'localhost'; |
| Access to a single table | GRANT SELECT ON appdb.orders TO 'analyst'@'%'; |
| Access to specific columns | GRANT SELECT (id, status) ON appdb.orders TO 'analyst'@'%'; |
ON appdb.* is a database-level grant and also covers tables created later. ON *.* is a global grant on every database, including the mysql system schema, so do not use it for application accounts. Add WITH GRANT OPTION only when the user really needs to pass its own privileges on to other accounts.
Note: In database-level grants, _ and % are wildcards. GRANT ... ON my_db.* also matches a database called myxdb. If you need an exact match, escape the underscore: `GRANT SELECT ON my\_db.* TO ...`. Names that contain hyphens must be quoted with backticks.
How the host part works
A MySQL account is the pair 'user'@'host', so 'john'@'localhost' and 'john'@'%' are two separate accounts with separate passwords and grants.
'localhost': connections over the Unix socket, or to the host namelocalhost.'127.0.0.1': TCP connections over loopback, for examplemysql -h 127.0.0.1.'10.0.1.%'or'10.0.1.0/255.255.255.0': any client in that subnet. Newer MySQL 8.0 releases also accept CIDR notation such as'10.0.1.0/24'.'%': any host. This is convenient, but it exposes the account to every network that can reach port 3306.
For remote clients to connect at all, the server must also listen on a reachable address. Many Linux packages set bind-address = 127.0.0.1 in the server config (for example /etc/mysql/mysql.conf.d/mysqld.cnf). Change it to the server's private IP or 0.0.0.0, restart MySQL, and allow port 3306 only from trusted networks in your firewall or security group.
Using roles to manage access (MySQL 8.0+)
When several users need the same access, grant privileges to a role once and then assign the role to users:
CREATE ROLE 'app_read', 'app_write';
GRANT SELECT ON appdb.* TO 'app_read';
GRANT INSERT, UPDATE, DELETE ON appdb.* TO 'app_write';
GRANT 'app_read', 'app_write' TO 'appuser'@'10.0.1.%';
SET DEFAULT ROLE ALL TO 'appuser'@'10.0.1.%';Code language: PHP (php)
Without SET DEFAULT ROLE, roles are granted but not active when the user logs in, and queries fail with access-denied errors. The user would have to run SET ROLE ALL; in every session.
Revoking access and removing users
-- Remove one privilege
REVOKE INSERT ON appdb.* FROM 'appuser'@'10.0.1.%';
-- Remove everything the account was granted
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'appuser'@'10.0.1.%';
-- Change a password
ALTER USER 'appuser'@'10.0.1.%' IDENTIFIED BY 'N3w!Passw0rd#2026';
-- Delete the account
DROP USER 'appuser'@'10.0.1.%';
-- List all accounts
SELECT user, host FROM mysql.user ORDER BY user;Code language: PHP (php)
A revoked privilege applies to the user's next statement for table and column privileges, and from the next USE for database-level privileges. Sessions that are already open can keep their current default database until they reconnect, so kill those sessions if access must end immediately.
Common errors and fixes
| Error | Cause | Fix |
|---|---|---|
ERROR 1064 ... near 'IDENTIFIED BY' | GRANT ... IDENTIFIED BY on MySQL 8.0+ | Run CREATE USER first, then GRANT without a password |
ERROR 1410: You are not allowed to create a user with GRANT | The 'user'@'host' in GRANT does not exist | Create that exact account, or fix the host part |
ERROR 1396: Operation CREATE USER failed | The account already exists | Use CREATE USER IF NOT EXISTS or ALTER USER |
ERROR 1045: Access denied ... (using password: YES) | Wrong password, or the client matched a different host entry | Check SELECT user, host FROM mysql.user and reset with ALTER USER |
ERROR 1044: Access denied for user ... to database | Login worked but there is no grant on that database | GRANT on the right database and check SHOW GRANTS |
ERROR 1130: Host ... is not allowed to connect | No account matches the client's host | Create the user for that host or subnet |
Authentication plugin 'caching_sha2_password' cannot be loaded | Old client or driver | Upgrade the client or connector, which is the preferred fix |
Security best practices
- Give each application and each person a separate account. Never let an application connect as
root. - Grant the minimum privileges at database or table level. Avoid
ALL PRIVILEGES ON *.*andWITH GRANT OPTION. - Restrict the host part to a subnet or specific address instead of
'%', and firewall port 3306. - Use strong, generated passwords stored in a secrets manager. The
validate_passwordcomponent can enforce password policy on the server. - Require encrypted connections for remote users:
ALTER USER 'appuser'@'10.0.1.%' REQUIRE SSL; - Review accounts regularly with
SHOW GRANTSand drop users you no longer need.
Frequently asked questions
Do I need to run FLUSH PRIVILEGES after GRANT in MySQL?
No. GRANT, REVOKE, CREATE USER and ALTER USER take effect immediately. FLUSH PRIVILEGES is only needed after editing the grant tables directly with DML statements.
Why does GRANT … IDENTIFIED BY give a syntax error?
MySQL 8.0 removed the ability to create users or set passwords through GRANT. Create the account with CREATE USER 'u'@'host' IDENTIFIED BY '...' and then run GRANT without the password clause.
How do I grant a user access to all tables in one database?
Use database_name.* as the target, for example GRANT SELECT, INSERT, UPDATE, DELETE ON appdb.* TO 'appuser'@'%';. The grant also covers tables created in that database later.
How can I see what privileges a MySQL user has?
Run SHOW GRANTS FOR 'user'@'host';. To see your own privileges, run SHOW GRANTS; or SHOW GRANTS FOR CURRENT_USER();.
Why can a user connect locally but not remotely?
Either the account only exists for localhost, or the server is listening on 127.0.0.1 only. Create the user for the remote host or subnet, set bind-address correctly, and open port 3306 to trusted clients.
Related tutorials
- More Databases tutorials
- Senior Database Administrator: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path
- Database Platform Engineer: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals