Grant MySQL table and column permissions using Python Last Updated : 20 May, 2021 Comments Improve Suggest changes 1 Likes Like Report See More 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: SELECT user(); The above statement could be used to know the permissions of the user. SHOW GRANTS FOR user_name@localhost; See the below example: Default Privileges 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: Python3 # import required module import pymysql # establish connection to MySQL connection = pymysql.connect( # specify host host='localhost', # specify root account user='root', # specify password for root account password='1234', # default port number is 3306 fro MySQL port=3306 ) # make a cursor to run sql queries mycursor = connection.cursor() # granting all permissions on all databases and their # tables of geeksforgeeks user permission also includes # table and column grants mycursor.execute("Grant all on *.* to geeksforgeeks@localhost") # print all privileges of geeksforgeeks user mycursor.execute("Show grants for geeksforgeeks@localhost") result = mycursor.fetchall() print(result) # commit privileges mycursor.execute("Flush Privileges") # close connection to MySQL connection.close() Output Python Output MySQL Terminal MySQL output We could see that permissions to CREATE and ALTER MySQL tables have been provided to user geeksforgeeks. Create Quiz Comment R rohanchopra96 Follow 1 Improve R rohanchopra96 Follow 1 Improve Article Tags : Python Python-mySQL Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like