iDatam

IN AFRICA

ALBANIA

ARGENTINA

AUSTRALIA

AUSTRIA

AZERBAIJAN

B AND H

BANGLADESH

BELGIUM

BRAZIL

BULGARIA

CANADA

CHILE

CHINA

COLOMBIA

COSTA RICA

CROATIA

CYPRUS

CZECH

DENMARK

ECUADOR

EGYPT

EL SALVADOR

ESTONIA

FINLAND

FOR BACKUP AND STORAGE

FOR DATABASE

FOR EMAIL

FOR MEDIA STREAMING

FRANCE

GEORGIA

GERMANY

GREECE

GUATEMALA

HUNGARY

ICELAND

IN ASIA

IN AUSTRALIA

IN EUROPE

IN NORTH AMERICA

IN SOUTH AMERICA

INDIA

INDONESIA

IRELAND

ISRAEL

ITALY

JAPAN

KAZAKHSTAN

KENYA

KOSOVO

LATVIA

LIBYA

LITHUANIA

LUXEMBOURG

MALAYSIA

MALTA

MEXICO

MOLDOVA

MONTENEGRO

MOROCCO

NETHERLANDS

NEW ZEALAND

NIGERIA

NORWAY

PAKISTAN

PANAMA

PARAGUAY

PERU

PHILIPPINES

POLAND

PORTUGAL

QATAR

ROMANIA

RUSSIA

SAUDI ARABIA

SERBIA

SINGAPORE

SLOVAKIA

SLOVENIA

SOUTH AFRICA

SOUTH KOREA

SPAIN

SWEDEN

SWITZERLAND

TAIWAN

THAILAND

TUNISIA

TURKEY

UK

UKRAINE

UNITED ARAB EMIRATES

URUGUAY

USA

UZBEKISTAN

VIETNAM

How To Create a New User and Grant Permissions in MySQL

Learn how to create a new MySQL user and grant permissions with this comprehensive guide. Understand the essential commands, best practices, and troubleshooting tips for effective user and permission management in MySQL.

tutorial_img_name_alt

MySQL, one of the most popular open-source relational database management systems, is widely used in web development and server environments. Managing user accounts and permissions is crucial to maintaining the security and efficiency of your database. This guide will walk you through the step-by-step process of creating a new MySQL user, granting them the appropriate permissions, and managing their access effectively.

What You'll Learn

Prerequisites

Before you start, ensure you have the following:

  • Access to a MySQL database: Installed and configured on your server or local machine.

  • Administrative privileges: Ability to log in as the root user or a user with sufficient permissions.

  • Command-line knowledge: Familiarity with basic terminal commands.

Step 1: Accessing MySQL as Root

Begin by logging into MySQL with the root account:

bash

sudo mysql
                            

If your MySQL instance requires password authentication, use the following command and enter your password when prompted:

bash

mysql -u root -p
                            

Step 2: Creating a New User

Create a new user with the CREATE USER statement:

sql

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'strong_password';
                            
Authentication Options
  • Recommended Method (MySQL 8.0+):

    sql
    
    CREATE USER 'newuser'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'strong_password';
                                        
  • PHP Compatibility Method:

    For older PHP-based applications that require mysql_native_password:

    sql
    
    CREATE USER 'newuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'strong_password';
                                        

Step 3: Granting User Permissions

Permissions determine what actions a user can perform. Use the GRANT statement to assign specific privileges.

Basic Syntax

sql

GRANT privileges ON database.table TO 'username'@'host';
                            

Example: Granting Comprehensive Permissions

To give full control over all databases and tables, use:

sql

GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE,
        SELECT, REFERENCES, RELOAD
ON *.*
TO 'newuser'@'localhost'
WITH GRANT OPTION;
                            
Common Permission Scenarios
  • Database-Specific Permissions:

    Grant all privileges on a specific database:

    sql
    
    GRANT ALL PRIVILEGES ON mydatabase.* TO 'newuser'@'localhost';
                                        
  • Read-Only Access:

    Allow a user to view data without modifying it:

    sql
    
    GRANT SELECT ON mydatabase.* TO 'newuser'@'localhost';
                                        

Additional User Management Commands

Reviewing User Permissions

Check the permissions assigned to a specific user:

sql

SHOW GRANTS FOR 'newuser'@'localhost';
                            

Revoking Permissions

To remove specific permissions:

sql

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

Deleting a User

Remove a user from the database entirely:

sql

DROP USER 'username'@'localhost';
                            

Logging In as the New User

Test the new user account by logging in:

bash

mysql -u newuser -p
                            

You’ll be prompted to enter the password you set for newuser.

Best Practices

  • Use strong, unique passwords for all database users.

  • Assign only necessary permissions to each user based on their role.

  • Avoid using root credentials for day-to-day operations.

  • Regularly review and audit user permissions to ensure security.

Troubleshooting Tips

  • Verify the correct authentication plugin for your user account.

  • Double-check the spelling of the username, hostname, and password.

  • Ensure the user has the required permissions for the tasks they need to perform.

Conclusion

Creating and managing users in MySQL is a foundational skill for database administrators and developers. By following this guide, you can establish secure, well-defined access controls for your database, ensuring both functionality and security. Implementing best practices and maintaining vigilance over permissions will help protect your system from unauthorized access.

Discover iDatam Dedicated Server Locations

iDatam servers are available around the world, providing diverse options for hosting websites. Each region offers unique advantages, making it easier to choose a location that best suits your specific hosting needs.

Up