0

I wrote a Python program that users can send posts, signup and login. I used MySQL in this app. When I try to open MySQL with localhost it works, But when I insert my IP Address it does not work. I use Ubuntu and ifconfig returns a lot of addresses. I tried all of them but did not work. MySQL returns : Can't connect to MySQL server on (IP):3306' (113). What can I do? Python code starting is like:

import tkinter as tk
from tkinter import messagebox
import mysql.connector

DB_HOST = 'My IP Address'
DB_USER = 'My User Name'
DB_PASSWORD = 'My Password'
DB_NAME = 'My Database'

# Connection Here : 
db_connection = mysql.connector.connect(
    host=DB_HOST,
    user=DB_USER,
    password=DB_PASSWORD,
    database=DB_NAME
)
db_cursor = db_connection.cursor()
  • 1
    Does this answer your question? [localhost vs. 127.0.0.1 in mysql\_connect()](https://stackoverflow.com/questions/3715925/localhost-vs-127-0-0-1-in-mysql-connect) – erik258 Jul 21 '23 at 15:23
  • @erik258 I Think 127.0.0.1 is current machine's IP. I want to be able to open app on multiple devices with different IP addresses. – Merd Ceferzade Jul 21 '23 at 15:26
  • That is not your IP, that is your *loopback address*. Pretty much every IPv4 system has that local address. Your machine will typically have a 192.168.x.x or 10.x.xx.x or 172.x.x.x type address that's on your *local network*, and then will show up with another *external IP* which is what other machines on the internet see it via NAT. – tadman Jul 21 '23 at 15:30
  • Mysql server can listen on unix domain socket and/or one or more IP addresses. If you want to configure mysql to listen on `(IP):3306` add it to the `bind-address` in the mysql server config. If you want to listen on all IP addresses, you can set `bind-address` to `0.0.0.0`. – erik258 Jul 21 '23 at 15:42
  • @erik258 Changing Bind Gave A Different Error : `ERROR 1045 (28000): Access denied for user 'root'@'merdiumasus' (using password: YES)` – Merd Ceferzade Jul 21 '23 at 15:54
  • your `GRANT` command probably specified `root`@`localhost`. Or that might have been done implicitly. Needless to say, `root` is not the user you should be using for your application. Create a new user with GRANT and allow it to connect from a range of IPs https://dev.mysql.com/doc/refman/8.0/en/grant.html – erik258 Jul 21 '23 at 16:07
  • @erik258 Thanks for help, it worked really good! – Merd Ceferzade Jul 21 '23 at 16:22

2 Answers2

0

Error 113 indicates, that the client can't connect to the server, since there is no physical route to the server.

Possible reasons:

  • The IP is wrong
  • The Port is wrong
  • MySQL server is not bound to this IP address
  • MySQL server was started with --skip-networking
Georg Richter
  • 5,970
  • 2
  • 9
  • 15
-2

localhost is 127.0.0.1. This is defined in your /etc/hosts file.
MySQL however behaves differently. Read the comment by erik258 below.

TheNXGuy
  • 1
  • 2
  • mysql behaves differently. by default `localhost` is taken to be a unix domain socket. Just because mysql listens on a unix domain socket, does not mean it's listening also to tcp 3306 on localhost or any other address – erik258 Jul 21 '23 at 15:22
  • I want to run this app on other computers too, doesn't 127.0.0.1 return current machine's ip? – Merd Ceferzade Jul 21 '23 at 15:24
  • First, an IP address identifies a *network interface*, not a machine. A single machine can have 0 or more network interfaces, each with its own IP address. 127.0.0.1 is a *loopback address*, which effectively identifies the IP software stack itself rather than a network interface. Roughly speaking, a packet sent to 127.0.0.1 never actually leaves the IP stack; it's just shifted from one memory location to another for processing. – chepner Jul 21 '23 at 15:38
  • (The end result is that a packet sent to 127.0.0.1 "targets" the current machine, but in no sense can you say that it came from your wireless card or an EtherNet port or any other kind of networking hardware, because it *didn't*. It came from a software-only *loopback device* provided by your IP stack itself.) – chepner Jul 21 '23 at 15:39
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 25 '23 at 18:12