Grant MySQL table and column permissions using Python
Last Updated :
20 May, 2021
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.
Similar Reads
Rename column name in MySQL using Python In this article, we are going to see how we can rename or alter the name of a column table using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in
1 min read
Add comment to column in MySQL using Python MySQL server is an open-source relational database management system which 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
3 min read
How to Concatenate Column Values of a MySQL Table Using Python? Prerequisite: Python: MySQL Create Table In this article, we show how to concatenate column values of a MySQL table using Python. We use various data types in SQL Server to define data in a particular column appropriately. We might have requirements to concatenate data from multiple columns into a s
2 min read
CRUD Operation in Python using MySQL In this article, we will be seeing how to perform CRUD (CREATE, READ, UPDATE and DELETE) operations in Python using MySQL. For this, we will be using the Python MySQL connector. For MySQL, we have used Visual Studio Code for python. Before beginning we need to install the MySQL connector with the co
6 min read
How to Copy a Table in MySQL Using Python? In this article, we will create a table in MySQL and will create a copy of that table using Python. We will copy the entire table, including all the columns and the definition of the columns, as well as all rows of data in the table. To connect to MySQL database using python, we need PyMySql module.
3 min read