title: how to secure mysql connection using python
body: <p>I'm writing python script which uses a mysql db. I want to secure mysql connection and the database will not be accessible to local user . Is there a good way to product this?</p>
  
  <pre><code>#!/usr/bin/python
  
  import MySQLdb
  
  # Open database connection
  
  db = MySQLdb.connect("localhost","username","password","db")
  
  
  # prepare a cursor object using cursor() method
  
  cursor = db.cursor()
  
  # Prepare SQL query to INSERT a record into the database.
  
  cursor.execute("CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, userid VARCHAR(30), 
  activity_id VARCHAR(30), date_time VARCHAR(30), screenshot_filename VARCHAR(255), screenshot_md5 VARCHAR(255), num_clicks INT, num_of_mouse_movements INT, num_pause INT );")
  
  
  try:
  
     # Execute the SQL command
  
     cursor.execute(sql)
  
     # Commit your changes in the database
  
     db.commit()
  
  except:
  
     # Rollback in case there is any error
  
     db.rollback()
  
     # disconnect from server
  
     db.close() 
  </code></pre>