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 return 0L
. If you execute this code in a Python program file, you will not get such feedback (unless you overtly tell Python to print it). But for debugging purposes, you have two choices: check the MySQL...