(Verbindung zum MySQL-Server unter '' localhost: 3306 'nicht möglich) (-5 keine Adresse mit Hostname verknüpft)

414
user3158464
I am using pie 3 b+ and installed mysql. I have no problem getting sql from the command line and creating databases and getting data back from it. I am new to python, mysql and lunix in general. I searched on google and found the Following to examples of how to connect to a mysql database. The first works ok and the second gets the error decribed below. When I print the values of the ini file in the second program they appear as what is in the first program that is hard coded. My confusion is why would the first program work and second not, are they not doing the same thing in mysql???  

Ich habe das Gefühl, es ist etwas in den DNS oder im Hafen, aber warum das eine und nicht das andere.

Die config.ini-Datei:

[mysql]

database = 'jcd'

host = 'localhost'

user = 'pi'

password = 'cody'

Der Python-Code, der korrekt funktioniert und "verbunden mit MySQL" druckt

Import mysql.connector aus mysql.connector importieren Fehler

def connect ():

def connect (): "" "Verbindung zur MySQL-Datenbank herstellen" "" "try: print (" try yo connect ") conn = mysql.connector.connect (host = 'localhost', user = 'pi', Datenbank = 'jcd', password = 'cody') if conn.is_connected (): print ('Verbunden mit MySQL-Datenbank'), außer Fehler als e: print (e)

finally: conn.close() 

wenn name == ' main ': connect ()

Das Python-Programm, das den Fehler abruft (Verbindung zum MySQL-Server unter '' localhost: 3306 'nicht möglich) (-5 keine Adresse mit Hostname verknüpft)

importiere mysql.connector

von mysql.connector importieren Fehler

von jcddbconfig importieren Sie read_db_config

def connect ():

db_config = read_db_config()  for i in db_config:  print i,db_config[i]  try:  print('trying conect to mysql')  conn = mysql.connector.connect(**db_config)  if conn_is_connected():  print('connected ')  else:  print('conect failed')  except Error as error:  print(error) 

wenn name == ' main ':

connect() 

Danke im voraus jcd

0

0 Antworten auf die Frage