JohnDotOwl
JohnDotOwl

Reputation: 3755

Python connecting to mysql

I installed Python 2.7 to try to connect to MySQL online. Basically, MySQL and phpMyAdmin is on a server and I can access it via localhost:8888/phpmyadmin via putty on my windows desktop. I cant seem to connect to it even with the putty on. Any idea? I face the same issue with Python 3.3 using CyMySQL.

import MySQLdb

db = MySQLdb.connect(host="127.0.0.1", # your host, usually 127.0.0.1
                     user="megamonster", # your username
                     passwd="", # your password
                     db="extractor") # name of the data base

# you must create a Cursor object. It will let
#  you execute all the query you need
cur = db.cursor() 

# Use all the SQL you like
cur.execute("SELECT * FROM abc")

# print all the first cell of all the rows
for row in cur.fetchall() :
    print row[0]

Error:

Traceback (most recent call last):
  File "C:\Users\Jonathan\Desktop\testSQL.py", line 6, in <module>
    db="extractor") # name of the data base
  File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (10061)")

Update

i added port(3306) and got this. OperationalError: (2013, "Lost connection to MySQL server at 'waiting for initial communication packet', system error: 0")

Currently looking at MySQL error: 2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0"

Hmm cant work still...

Upvotes: 1

Views: 4292

Answers (2)

Rohan Gala
Rohan Gala

Reputation: 651

I used sqlplus it worked

sqlplus User_name/password@Host_ip:Port/Service_Name@$SQLFILENAME

Just specify the SQLFILENAME if you want to utilize a file. Otherwise, you can ignore this parameter and can directly run sql statements

Upvotes: 2

beroe
beroe

Reputation: 12326

It could be a number of things, but as far as MySQL is concerned, permissions are set independently for localhost and for 127.0.0.1. Make sure you can connect with the exact host and credentials. Possibly related

For example, check this when connected with your PUTTY connection.

mysql> use mysql;
Database changed

mysql> SELECT host,user,select_priv FROM user;

+-------------------------+------+-------------+
| host                    | user | select_priv |
+-------------------------+------+-------------+
| localhost               | root | Y           |
| 127.0.0.1               | root | Y           |
+-------------------------+------+-------------+

Also check who you are connected as (on PUTTY) and use that same info in the script:

mysql> SELECT USER(),CURRENT_USER();

+----------------+----------------+
| USER()         | CURRENT_USER() |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+

Upvotes: 1

Related Questions