Search code examples
pythonmysqlpython-3.xphpmyadminxampp

Connect MySql to a machine on the same network


I'm trying to connect a machine script X to a database on the machine Y. I can handle the bank perfectly on the machine Y, but when I run the machine X I get the error below:

"2003 (HY000): Can't connect to MySQL server on '172.22.128.1:3306 (10060)"

Below my connection script inserted into the machine Y

declaracao = f'INSERT INTO similares (momento,produto1,produto2,res) VALUES ("teste","teste","teste",10.00);'

try:
#CREATE CONNECTION MYSQL
   con = mysql.connector.connect(user='root', password='root', host='172.22.128.1', database='saneamento') #Here where to return the error

   cursor = con.cursor()
   cursor.execute(declaracao)
   con.commit()
   print(cursor.rowcount, "Insert dados!")
   cursor.close()

 except Error as e:
   print("Fail", e)

Below computer IP X

Adress IPv4. . . . . . . .  . . . . . . . : 172.22.128.1

Settings XAMPP:

Settings MySQL

Settings phpMyAdmin

Computers X and Y are on the same network


Solution

  • Can you manually connect to your MySQL database from a remote server?

    remote linux server> mysql -D <database> -h <host> -u <user> -p -e "<mysql command>"
    

    If that fails, you should check that MySQL will allow the root user to connect from a remote host. You might have it setup with a root@localhost entry, but not a root@<remote host> entry.

      mysql> select * from mysql.user;
    

    If you only have a localhost entry for root, you can do this on Linux to add a remote host entry.

      mysql> CREATE USER 'root'@'remote_server_ip' IDENTIFIED BY 'password';
    
      mysql> GRANT ALL PRIVILEGES ON <database_name>.* TO 'root'@'remote_server_ip' WITH GRANT OPTION;
    
      mysql> FLUSH PRIVILEGES;
    

    ** Note that you can replace 'remote_server_ip' with '%' to allow access from any remote server.

    If it still doesn't work, you might need to check your firewall.