Python-Ref > Databases and Python > Python and MySQL > Connecting to MySQL server

Klíčová slova
Knihovní funkce

Connecting to MySQL server

What to import and how to connect to a MySQL server.
To connect to a MySQL server we need to have MySQLdb extension installed, know the servers address (if its not localhost) and have an account and password on that server. When all these requirements are met and there is not problem on the way, we can connect to the server.
Zdroj: (
  1   # at first we import the MySQLdb module
  2   import MySQLdb
  4   # user   = user name for the MySQL database
  5   # passwd = password of this user
  6   # db     = name of the database
  7   # host   = hostname of the computer MySQL is running on
  8   connection = MySQLdb.connect( user="test", passwd="pass123", db="world", host="localhost")
 10   print connection
 11   print "MySQL version", connection.get_server_info()
 12   connection.close()
<_mysql.connection open to 'localhost' at 698e60>
MySQL version 5.0.44
Doba běhu: 441.6 ms