Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

457
Visualizações
Python - Connect MySQL through localhost not working but 127.0.0.1 is working?

I just setup a VPS with a Mysql Database on it. I'm able to log into the database in the SSH terminal with mysql -uroot -p however, the python file shown below is unable to form a connection, and errors with Access denied for root@localhost (using password: YES). I'm confused as to why this is happening. I'm 100% sure my password is correct. I'm using mysql.connector

Python file:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="pwd",
  database="db"
)

mycursor = mydb.cursor()

sql = "INSERT INTO test (tests) VALUES (%s)"
val = ("testing")

mycursor.execute(sql, val)

mydb.commit()

I'm a bit new to this DB and VPS stuff, so please bear with me if I don't understand on the first go.

Thanks!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

In addition to @don's answer

I assume it has something to do with the dns not properly resolving localhost. I have experienced that before, but outside of just mapping localhost to 127.0.01 in the host's file,

This can also be fixed by changing a value in your /etc/mysql/my.cnf file.

It's likely your bind-address line is

bind-address = localhost 

You can try changing it to

bind-address = 127.0.0.1

You can also set it to 0.0.0.0 but this will expose the connection to the outside world (inc. public IP), so this is highly not recommended but will solve the problem.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda