MySQL is a powerful and widely used open-source relational database management system that empowers developers to efficiently manage and manipulate data. 

What Is MySQL?

MySQL is a powerful and widely used open-source relational database management system that empowers developers to efficiently manage and manipulate data.

One of its essential features is user management, allowing you to control access and permissions for different users. This article will guide you through the process of creating a new user and granting permissions within MySQL.

More About MySQLWhat Is MySQL?

 

Creating a User in MySQL: Prerequisites

Before diving into user creation and permission assignment, ensure you have the following prerequisites in place:

  • A working MySQL server instance installed on your system. 
  • Access to the MySQL root user account or an account with administrative privileges.
  • Basic familiarity with SQL queries and database management concepts.


How to Create a New User in MySQL

Creating a new user involves defining the user’s credentials and privileges. Follow these steps to create a new user in MySQL.

Access MySQL Command Line: Open your terminal or command prompt and log in to the MySQL server using the command: mysql -u root -p

You’ll be prompted to enter the root user’s password.

Create a New User: To create a new user, execute the following SQL command, replacing ‘newusername’ and ‘password’ with your desired username and password: CREATE USER 'newusername'@'localhost' IDENTIFIED BY 'password';

Verify User Creation: You can confirm that the user has been created successfully by running: SELECT user FROM mysql.user;

 

Understanding Privileges in MySQL

Before we proceed to grant permissions, it’s crucial to understand MySQL privileges. In the context of MySQL, privileges play a pivotal role in determining what actions users are allowed to perform within a database system. 

Think of privileges as the keys to various doors in a database, granting or restricting access to specific functionalities and resources. These fine-grained permissions are essential for maintaining data security, integrity and control.

Privileges are categorized into two primary levels: global and object-level privileges.

 

Global Privileges in MySQL

Global privileges pertain to actions that can be executed across the entire MySQL server. These privileges grant administrative capabilities that affect multiple databases. Examples of global privileges include:

  • SUPER: This privilege provides superuser access, allowing the user to perform administrative tasks such as shutting down the server or managing other users' privileges.
  • CREATE USER: Users with this privilege can create new user accounts. It's essential to restrict this privilege to trusted users to prevent unauthorized user creation.

 

Object-Level Privileges in MySQL

Object-level privileges are more granular and apply to specific database objects, such as tables, views and stored procedures. These privileges determine the types of operations a user can perform on these objects. Some common object-level privileges include:

  • SELECT: This privilege allows a user to retrieve data from a specific table or view. Users with this privilege can execute SELECT statements to retrieve data without being able to modify it.
  • INSERT: Users with the INSERT privilege can add new rows to a table. This is useful for applications that involve data entry or data import.
  • UPDATE: With the UPDATE privilege, users can modify existing data within a table, ensuring that data remains accurate and up to date.
  • DELETE: The DELETE privilege empowers users to remove rows from a table, contributing to data maintenance and cleanup.
  • ALTER: Users with the ALTER privilege can modify the structure of a table, such as adding or removing columns. This privilege is vital for database administrators.
  • DROP: The DROP privilege allows users to delete tables or databases entirely. Care must be taken when granting this privilege to prevent accidental data loss.

Privileges can be assigned at different levels — globally, to specific databases, or even down to individual objects like tables and columns. This flexibility allows for precise control over who can perform certain actions on what parts of the database.

To sum it up, privileges are the foundation of access control in MySQL, shaping how users interact with databases. By thoughtfully assigning privileges, database administrators can maintain a secure and organized environment where data remains protected and accessible only to authorized individuals.

 

How to Grant a User Permissions in MySQL

After creating a user, you’ll likely want to grant them specific privileges to perform certain actions. Here’s how:

Granting All Privileges: To grant all privileges to a user on a specific database, use the following command: GRANT ALL PRIVILEGES ON databasename.* TO 'newusername'@'localhost';

Note: Replace ‘databasename’ with the actual database name.

Specific Privileges: If you want to grant specific privileges, use the GRANT statement along with the privileges you wish to grant: GRANT SELECT, INSERT ON databasename.* TO 'newusername'@'localhost';

Reload Privileges: To apply the changes, reload the privileges using: FLUSH PRIVILEGES;

Verify Permissions: To verify the granted permissions, use: SHOW GRANTS FOR 'newusername'@'localhost';

Read More About DatabasesWhat Is Database Normalization?

 

Creating Users in MySQL: Why It’s Important

Creating users and granting permissions in MySQL is an essential skill for anyone working with databases. By understanding how to create users and assign privileges, you can ensure secure and controlled access to your valuable data.

Video tutorial on creating users in MySQL. Credit: Database Star / YouTube

With these steps in mind, you’re well on your way to managing user accounts and permissions effectively within MySQL. Remember that mastering user management contributes to maintaining the integrity and security of your databases.

Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Great Companies Need Great People. That's Where We Come In.

Recruit With Us