Grant MySQL table and column permissions using Python
MySQL server is an open-source relational database management system that is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a web server, we use various modules in Python such as PyMySQL, mysql.connector, etc.
In this article, we are going to grant permissions to a user in accessing a database and its MySQL tables. The CREATE USER statement creates a user account with no privileges. The statement for creating a user in MySQL is given below.
CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'password';
The above user can log in into MySQL Server, but cannot do anything such as querying data and selecting a database from tables. In our case, user_name is geeksforgeeks and password for login is 1234.
To change the user in MySQL client, use the below command:
SYSTEM MYSQL -u geeksforgeeks -p1234;
To check the current user, one can use the below command:
The above statement could be used to know the permissions of the user.
SHOW GRANTS FOR user_name@localhost;
See the below example:
Note: To grant permissions to the user geeksforgeesks you must be logged into root account. Users can’t grant permissions to themselves.
Below is the python program to add table and column permissions to the user geeksforgeeks:
We could see that permissions to CREATE and ALTER MySQL tables have been provided to user geeksforgeeks.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course