Lunarpages Web Hosting Forum

Author Topic: python connect remote mysql db?  (Read 12623 times)

Offline derekonly

  • Newbie
  • *
  • Posts: 4
python connect remote mysql db?
« on: December 27, 2009, 10:37:45 AM »
Is there anyone knows how to script python to connect remote lunarpages mysql database for updating data, Thanks in advance

Offline derekonly

  • Newbie
  • *
  • Posts: 4
Re: python connect remote mysql db?
« Reply #1 on: December 27, 2009, 03:27:32 PM »
I worked out. Happy new year  :roflmao:

Offline Jassi

  • Administrator
  • Über Jedi
  • *****
  • Posts: 1555
  • Waheguru Ji Ka Khalsa, Waheguru Ji Ki Fateh
Re: python connect remote mysql db?
« Reply #2 on: December 27, 2009, 06:29:00 PM »
Hello,

First of all you need to add your host IP address from remote mysql server so that your remote host can connect to database remotely and you can use following phyton script to connect to remote mysql database.

import MySQLdb
myDB = MySQLdb.connect(host="x.x.x.x", port=3306, user="**********", passwd="*****************")
cHandler=myDB.cursor()
cHandler.execute("SHOW DATABASES")
results=cHandler.fetchall()
print "========================="
for items in results:
   print items[0]


I hope it helps.

Thanks.
--

Er. Sarwan Singh Jassi
Manager - IT Systems

Lunarpages Internet Solutions
Website: http://lunarpages.com
Email: support@lunarpages.com
Phone: 1-714-521-8150
Community - http://www.lunarforums.com
Tutorials - http://www.lunarpages.com/tutorials/

Offline fates66

  • Intergalactic Cowboy
  • *****
  • Posts: 61
Re: python connect remote mysql db?
« Reply #3 on: March 31, 2010, 10:27:26 PM »
I have modified your code as follows,

Code: [Select]
#!/usr/bin/python
print "Content-Type: text/html;charset=utf-8\n\n"
print "Before import statement", "<br>"
import MySQLdb
print "After import statement", "<br>"
myDB = MySQLdb.connect(host="xx.xx.xx.xx", port=3306, user="xxxxxxxxxx", passwd="xxxxxx")
cHandler=myDB.cursor()
cHandler.execute("SHOW DATABASES")
results=cHandler.fetchall()
print "=========================", "<br>"
for items in results:
   print items[0]

and then upload it the folder /cgi-bin. When I access it via http://www.iching3289.com/cgi-bin/test.py, the result as

Code: [Select]
Before import statement

Obviously, the MySQLdb  module doesn't exist at this time. So any advice to connect to MySQL from Python. Thanks.