(не удается подключиться к серверу MySQL на '' localhost: 3306 '' (-5 - нет адреса, связанного с именем хоста)

290
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???  

У меня такое ощущение, что это что-то в днс или в порту, но почему то одно, а не другое.

Файл config.ini:

[MySQL]

база данных = 'jcd'

host = 'localhost'

user = 'pi'

пароль = "Коди"

Код Python, который работает правильно и печатает «связано с MySQL»

импортировать mysql.connector из mysql.connector Ошибка импорта

def connect ():

def connect (): "" "Подключиться к базе данных MySQL" "" try: print ('пытаясь подключиться') conn = mysql.connector.connect (host = 'localhost', user = 'pi', database = 'jcd', password = 'cody'), если conn.is_connected (): print ('Подключено к базе данных MySQL'), за исключением ошибки как e: print (e)

finally: conn.close() 

если name == ' main ': connect ()

Программа на python, которая получает ошибку (не может подключиться к серверу mysql по '' localhost: 3306 '' (-5 - нет адреса, связанного с именем хоста)

импорт mysql.connector

из mysql.connector Ошибка импорта

из jcddbconfig import 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) 

если name == ' main ':

connect() 

Заранее спасибо jcd

0

0 ответов на вопрос