Skip to main content

Mysql Database User Role

Published 2 min readMarkdown
Part of seriesMySql Database

o create a database and a role in MySQL where the role can view and edit the database but cannot delete any data, follow these steps:

1. Create a New Database

First, log in to MySQL with an admin user (like root) and create the new database.

CREATE DATABASE your_database_name;

2. Create a New User

Next, create a new user (role). This user will have specific permissions later.

CREATE USER 'thirdy'@'localhost' IDENTIFIED BY 'Test@123456';
  • Replace 'your_username' with the desired username.

  • Replace 'your_password' with a strong password.

3. Grant Permissions to the User

Now, give the user permission to view and edit the database, but restrict delete permissions.

GRANT SELECT, CREATE, INSERT, UPDATE, REFERENCES ON your_database_name.* TO 'thirdy'@'localhost';

This grants:

  • SELECT: The ability to view data.

  • INSERT: The ability to add data.

  • UPDATE: The ability to modify existing data.

The user cannot delete any data because the DELETE privilege is not given.

4. Apply Changes

After granting the permissions, run the following command to apply the changes:

FLUSH PRIVILEGES;

5. Show Grants

SHOW GRANTS FOR 'thirdy'@'localhost';

5. Test the User Permissions

You can log in as the new user and verify that they have the appropriate permissions.

mysql -u thirdy -p

Once logged in, try running SELECT, INSERT, and UPDATE queries. The user should not be able to delete records.

Example

SELECT * FROM your_table; -- Should work

INSERT INTO your_table (column1, column2) VALUES ('value1', 'value2'); -- Should work

UPDATE your_table SET column1 = 'new_value' WHERE id = 1; -- Should work

DELETE FROM your_table WHERE id = 1; -- Should NOT work

About the author

  • I am a dedicated and skilled Software Engineer specializing in mobile app development, backend systems, and creating secure APIs. With extensive experience in both SQL and NoSQL databases, I have a proven track record of delivering robust and scalable solutions.

    Key Expertise:

    Mobile App Development: I make high-quality apps for Android and iOS, ensuring they are easy to use and work well.

    Backend Development: Skilled in designing and implementing backend systems using various frameworks and languages to support web and mobile applications.

    Secure API Creation: Expertise in creating secure APIs, ensuring data integrity and protection across platforms.

    Database Management: Experienced with SQL databases such as MySQL, and NoSQL databases like Firebase, managing data effectively and efficiently.

    Technical Skills: Programming Languages: Java, Dart, Python, JavaScript, Kotlin, PHP

    Frameworks: Angular, CodeIgniter, Flutter, Flask, Django

    Database Systems: MySQL, Firebase

    Cloud Platforms: AWS, Google Cloud Console

    I love learning new things and taking on new challenges. I am always eager to work on projects that make a difference.

No comments yet. Be the first to comment.

More from this blog