[英]SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 44382
This happens because the localhost
(or 127.0.0.1
) on the device is only accessible to the device itself.发生这种情况是因为设备上的
localhost
(或127.0.0.1
)只能由设备本身访问。
You can reverse-proxy a localhost
port to the Android device/emulator running adb reverse
on the command prompt like so:您可以将
localhost
端口反向代理到在命令提示符下运行adb reverse
的 Android 设备/模拟器,如下所示:
adb reverse tcp:5000 tcp:5000
Use the machine's IP address where the DB is running.使用运行 DB 的机器的 IP 地址。 Also, the DB should be listening to the IP
0.0.0.0
to be accessible outside the localhost
.此外,数据库应该监听 IP
0.0.0.0
以便在localhost
之外访问。
Supposing the DB machine's IP is 192.168.1.123
it's going to be something like:假设数据库机器的 IP 是
192.168.1.123
它将是这样的:
Future db() async {
print('@');
final conn = await MySQLConnection.createConnection(
host: '192.168.1.123', // <- Here
port: 3306,
userName: 'root',
password: '',
databaseName: 'flutter', // optional
);
print('^');
}
Just take care because changing the DB to listen to 0.0.0.0
is a security risk as the DB is going to be accessible to the outside world.请注意,因为将数据库更改为侦听
0.0.0.0
会带来安全风险,因为外部世界将可以访问数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.