0
0
MySQLquery~5 mins

Creating users in MySQL

Choose your learning style9 modes available
Introduction

Creating users lets you control who can access your database. It helps keep your data safe and organized.

When you want to give a new person access to your database.
When you need to separate access rights for different team members.
When setting up a new application that needs to connect to the database.
When you want to restrict what certain users can do in the database.
Syntax
MySQL
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
Replace 'username' with the name you want for the user.
'host' is where the user can connect from, often 'localhost' for the same machine.
Examples
This creates a user named 'alice' who can connect only from the local computer.
MySQL
CREATE USER 'alice'@'localhost' IDENTIFIED BY 'mypassword';
This creates a user named 'bob' who can connect from any computer.
MySQL
CREATE USER 'bob'@'%' IDENTIFIED BY 'secure123';
Sample Program

This creates a user 'john' who can connect from the local machine, then shows what permissions he has (none by default).

MySQL
CREATE USER 'john'@'localhost' IDENTIFIED BY 'johnspass';
SHOW GRANTS FOR 'john'@'localhost';
OutputSuccess
Important Notes

After creating a user, you usually need to give them permissions with GRANT.

Passwords should be strong to keep your database secure.

Use SHOW GRANTS FOR 'username'@'host'; to check a user's permissions.

Summary

Creating users controls who can access your database.

Use CREATE USER 'name'@'host' IDENTIFIED BY 'password'; to add a user.

Remember to assign permissions after creating the user.