[英]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 .我正在尝试将机器脚本X连接到机器Y上的数据库。 I can handle the bank perfectly on the machine Y , but when I run the machine X I get the error below:
我可以在机器Y上完美地处理银行,但是当我运行机器X时,出现以下错误:
"2003 (HY000): Can't connect to MySQL server on '172.22.128.1:3306 (10060)"
Below my connection script inserted into the machine Y下面我的连接脚本插入到机器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电脑下方IP X
Adress IPv4. . . . . . . . . . . . . . . : 172.22.128.1
Settings phpMyAdmin设置 phpMyAdmin
Computers X and Y are on the same network计算机 X 和 Y 在同一网络上
Can you manually connect to your mysql database from a remote server?您可以从远程服务器手动连接到您的 mysql 数据库吗?
remote linux server> mysql -D <database> -h <host> -u <user> -p -e "<mysql command>"远程 linux 服务器> mysql -D <数据库> -h <主机> -u <用户> -p -e "<mysql 命令>"
If that fails you should check that mysql will allow the root user to connect from a remote host.如果失败,您应该检查 mysql 是否允许 root 用户从远程主机连接。 You might have it setup with a root@localhost entry but not a root@<remote host> entry.
您可能使用 root@localhost 条目而不是 root@<remote host> 条目来设置它。
mysql> select * from mysql.user; mysql> select * 来自 mysql.user;
If you only have a localhost entry for root you can do this on Linux to add a remote host entry.如果你只有一个 root 的 localhost 条目,你可以在 Linux 上添加一个远程主机条目。
mysql> CREATE USER 'root'@'remote_server_ip' IDENTIFIED BY 'password'; 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> GRANT ALL PRIVILEGES ON <database_name>.* TO 'root'@'remote_server_ip' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES; mysql> 刷新权限;
** Note that you can replace 'remote_server_ip' with '%' to allow access from any remote server. ** 请注意,您可以将 'remote_server_ip' 替换为 '%' 以允许从任何远程服务器进行访问。
If it still doesn't work you might need to check your firewall.如果它仍然不起作用,您可能需要检查您的防火墙。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.