Creating users from Python

The ability to create users is obviously an administrative task. By default, this means that one must log in as root, or any other user who has administrative rights, to use them. If your Python program does not login as root, it will not be able to affect user creation. Therefore, one's connection credentials must read accordingly:

import MySQLdb
mydb = MySQLdb.connect(host = 'localhost', 
                       user = 'root', 
                       passwd = 'rootsecret')
cursor = mydb.cursor()

From here, one can similarly form the statement to the other CREATE statements that we have used.

statement = """CREATE USER 'exemplar'@'localhost' IDENTIFIED BY 'MoreSecurity'"""
cursor.execute(statement)

In a Python shell, passing the statement through cursor.execute() will ...

Get MySQL for Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.