Tutorials  /  Databases

How To Create a New User and Grant Permissions in MySQL

Ccentron Redaktion · January 2025 ·4 min read ·Databases, Tutorial

Introduction to To Create a New User and Grant Permissions in MySQL

MySQL is an open-source relational database management system, frequently used as part of the LAMP stack (Linux, Apache, MySQL, PHP). Therefore, it is a go-to choice for developers worldwide due to its efficiency and flexibility. Moreover, this system ensures robust functionality, allowing users to manage data securely and reliably. This guide outlines how to create a new MySQL user and grant permissions to perform various actions. Furthermore, by understanding these steps, you can maintain proper access control and prevent unauthorized changes. Consequently, mastering user and permission management is critical for both small projects and large-scale applications, ensuring database security and integrity.

DB

Matching infrastructure at centron

Databases need care in production: centron can take over updates, backups and monitoring – from a single instance to a full cluster. Explore managed clusters →

Prerequisites To Create a New User and Grant Permissions in MySQL

To follow this guide, ensure you have:

  • Access to a MySQL database.
  • A MySQL database installed, preferably on an Ubuntu 20.04 VPS.
  • Familiarity with terminal commands like chmod, mkdir, and cd.

Creating a New User

To create a new MySQL user, follow these steps:

    1. Access the MySQL prompt as the root user:
Code
sudo mysql
    1. Create a user with the following syntax:
Code
CREATE USER 'username'@'host' IDENTIFIED WITH authentication_plugin BY 'password';
    1. For example:
Code
CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password';

Alternatively, to use the mysql_native_password plugin:

Code
CREATE USER 'sammy'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Granting a User Permissions

Use the GRANT statement to assign privileges:

Code
GRANT PRIVILEGE ON database.table TO 'username'@'host';

For example, to grant global privileges:

Code
GRANT ALL PRIVILEGES ON *.* TO 'sammy'@'localhost' WITH GRANT OPTION;

To reload privileges (optional):

Code
FLUSH PRIVILEGES;

Revoke permissions:

Code
REVOKE type_of_permission ON database_name.table_name FROM 'username'@'host';

To check user permissions:

Code
SHOW GRANTS FOR 'username'@'host';

Deleting a User

To delete a user:

Code
DROP USER 'username'@'localhost';

Logging In as a New User

To log in as the new user:

Code
mysql -u sammy -p

Conclusion to Create a New User and Grant Permissions in MySQL

In this guide, you learned how to create new MySQL users and grant them specific permissions. Additionally, understanding permission settings is essential for managing database security and user access effectively. Experiment with different permission settings or explore advanced MySQL configurations for further learning. Moreover, try combining multiple permissions to create custom access levels for various users. This approach ensures better control over your database operations. As you gain experience, focus on mastering best practices for granting and revoking permissions. These skills are valuable for maintaining a secure and efficient MySQL environment in all projects.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Databases
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren