How to create a MySQL user and grant privileges
Updated 11/29/2026
For security, every application should use its own MySQL user with access only to its own database. Here is how.
1. Create the user
mysql -u root -p
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'strong_password';
2. Create the database
CREATE DATABASE app_db;
3. Grant privileges
Only on the application's database:
GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
4. Narrower privileges (recommended)
For an app that only reads and writes data, avoid ALL:
GRANT SELECT, INSERT, UPDATE, DELETE ON app_db.* TO 'app_user'@'localhost';
5. Check and revoke
SHOW GRANTS FOR 'app_user'@'localhost';
REVOKE ALL PRIVILEGES ON app_db.* FROM 'app_user'@'localhost';
For remote access, replace localhost with % or a specific IP, but first see the connect to a remote database guide for the security steps.
Put this guide into practice on your own VPS
EU cloud servers, billed hourly, ready in minutes, with one-click installs for dozens of apps.
See pricingRelated tutorials
How to install and secure MySQL on Ubuntu
Install MySQL or MariaDB on your VPS and secure it properly: root password, removing test accounts and controlled access.
Updated 11/29/2026DatabasesHow to create users and databases in PostgreSQL
Create roles, databases and grant privileges in PostgreSQL, step by step.
Updated 11/29/2026DatabasesHow to install and secure PostgreSQL on Ubuntu
Install the most advanced open-source database and configure it safely: password, scram-sha-256 authentication and controlled access.
Updated 11/29/2026