2016-12-23 15 views
1

나는 윈도우 10에서 파이썬 3.5.0와 PyMySQL 0.79를 사용하지만 'test.py'라는 간단한 프로그램을 실행할 때 연결 오류를 얻으려고 ...PyMySQL 코드에 연결 오류 Windows 10 Python 3.5.0?

import pymysql 
db = pymysql.connect(host = 'sqlserver.example.com', passwd 'SECRET12345', user = 'dbuser', db='myDatabase') 
cursor = db.cursor() 
sql = "INSERT INTO people (name, email, age) VALUES (%s, %s, %s)" 
cursor.execute(sql, ("Jay", "[email protected]", '77')) 
cursor.close() 
db.commit() #Makes sure the DB saves your changes! 
db.close() 

그러나 위의 코드 제공 나 오류 메시지 :

을 : 나에게주는

'PIP 목록'

C:\Users\Avtar\AppData\Local\Programs\Python\Python35-32\Lib\site-packages\pymysql>test.py 
Traceback (most recent call last): 
    File "C:\Users\Avtar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pymysql\connections.py", line 890, in connect 
    (self.host, self.port), self.connect_timeout) 
    File "C:\Users\Avtar\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 689, in create_connection 
    for res in getaddrinfo(host, port, 0, SOCK_STREAM): 
    File "C:\Users\Avtar\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 728, in getaddrinfo 
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags): 
socket.gaierror: [Errno 11001] getaddrinfo failed 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:\Users\Avtar\AppData\Local\Programs\Python\Python35-32\Lib\site-packages\pymysql\test.py", line 2, in <module> 
    db = pymysql.connect(host = 'sqlserver.example.com', passwd = 'SECRET12345', user = 'dbuser', db='myDatabase') 
    File "C:\Users\Avtar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pymysql\__init__.py", line 90, in Connect 
    return Connection(*args, **kwargs) 
    File "C:\Users\Avtar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pymysql\connections.py", line 688, in __init__ 
    self.connect() 
    File "C:\Users\Avtar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pymysql\connections.py", line 937, in connect 
    raise exc 
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'sqlserver.example.com' ([Errno 11001] getaddrinfo failed)") 

내가 명령 'pymysql를 설치 PIP'내가 입력하여 설치를 확인할 수를 사용하여 pyMySQL 설치

Name: PyMySQL 
Version: 0.7.9 
Summary: Pure Python MySQL Driver 
Home-page: https://github.com/PyMySQL/PyMySQL/ 
Author: INADA Naoki 
Author-email: [email protected] 
License: MIT 
Location: c:\users\avtar\appdata\local\programs\python\python35-32\lib\site-packages 
Requires: 

내가 잘못하고있는 무슨이에서 말할 수 없다, 그래서 사람이를 정렬 나를 도울 수 있다면 것 정말 감사 :

pip (9.0.1) 
PyMySQL (0.7.9) 
setuptools (18.2) 
yolk (0.4.3) 

나에게주는 'pymysql을 보여 PIP'. 미리 감사드립니다.

답변

0

"sqlserver.example.com"유형의 호스트는 대개 MySQL이 아니고 Microsoft SQL Server입니다. 그렇다면 (즉, SQL Server를 사용하는 경우) pymysql 라이브러리를 사용하는 대신보다 일반적인 라이브러리를 사용해야합니다. 예 : pyodbc.

이 따르는 다른 stackoverflow question을 바탕으로, Python2.7를 들어,이 라이브러리를 사용하는 방법입니다 같이

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=sqlserver.example.com;DATABASE=testdb;UID=me;PWD=pass') 
cursor = cnxn.cursor() 

을 그러나 가리키고 당신이 MySQL을 사용하는 다음 구성이 잘못된 경우 ...

코드에 passwd와 ""사이에 "="이 없음을 확인하십시오.; 포트 번호를 가지고 있지 않습니다. 포트가없는 3306 경우

db = pymysql.connect(host = 'sqlserver.example.com', port=3306, passwd='SECRET12345', user = 'dbuser', db='myDatabase') 

를, 당신은 다음을 수행 포트를 찾기 위해, 당신의 MySQL이에서 어떤 포트 확실하지 않은 :

db = pymysql.connect(host = 'sqlserver.example.com', passwd 'SECRET12345', user = 'dbuser', db='myDatabase') 

따라서 나는 제안 :

mysql> select * from INFORMATION_SCHEMA.GLOBAL_VARIABLES WHERE VARIABLE_NAME LIKE 'PORT'; 
+---------------+----------------+ 
| VARIABLE_NAME | VARIABLE_VALUE | 
+---------------+----------------+ 
| PORT   | 2127   | 
+---------------+----------------+ 
1 row in set (0.00 sec) 
+0

지연에 대한 사과 - 답장을 보내 주셔서 감사합니다. 솔직히 말해서 "db = pymysql.connect"명령에 무엇이 있어야할지 모르겠습니다. 누락 된 '='을 추가하라는 제안에 따라했지만 여전히 동일한 오류가 발생합니다. 'Microsoft SQL Server'vs 'MySQL'의 관점에서 내가 무엇을 사용해야하는지 확신 할 수 없습니다. 어떻게 결정됩니까? 또한 나열된 명령을 실행하여 포트 번호를 찾는 방법은 무엇입니까? (이 분야에 대한 나의 지식 부족을 용서하십시오). – Avtar