Nerdegutta's logo

nerdegutta.no

SQL: To remember

18.06.26

Programming

Things I usually forget....
Add a user to mySQL:
This creates a new user:

CREATE USER 'USERNAME'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD';

Give permissions.
GRANT ALL ON 'DATABASENAME'.* TO 'USERNAME'@'localhost';
Here is exactly what each part means:
GRANT ALL PRIVILEGES: Tells MySQL to give the user all standard operational permissions (like SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, and ALTER). Using ALL or ALL PRIVILEGES means the exact same thing.
ON: Specifies the target scope of these permissions.database_name.*: The first part is the database name, and the asterisk (*) is a wildcard meaning "all tables inside this database"
FLUSH PRIVILEGES;
The FLUSH PRIVILEGES command instructs the MySQL server to reload the grant tables (mysql.user, mysql.db, etc.) from disk into the server's in-memory privilege cache. This forces any recent modifications made to user permissions to take effect immediately without needing to restart the database server