Connecting to a MySQL database

The next step to using MySQL in your Python scripts is to make a connection to the database that you wish to use. All Python DB-API 2.0 modules implement a function 'module_name.connect'. This is the function that is used to connect to the database, in our case MySQL.

		db = MySQLdb.connect(host=MY_HOST, user=MY_USER, passwd=MY_PASS, db=MY_DB)

As you might guess this statement will connect me to the MySQL database located on MY_HOST, using user MY_USER, connecting with password MY_PASS, to database MY_DB. Note that the parameter for the password is 'passwd' not 'password'.

As you execute this function the parameters are essentially passed to the underlying Python extension _mysql. This lets you pass many of the MySQL specific connection parameters through the normal connection method. For a complete listing of the supported connection parameters check out the reference section.

<-- Home -->