Nicht behandelte Ausnahme: SocketException: Verbindung abgelehnt (Betriebssystemfehler: Verbindung abgelehnt, Fehlernummer=111), Adresse=localhost, Port=60332
P粉921130067
2023-08-29 22:47:40
<p>首先,我想从 MySQL Workbench 中的本地主机获取数据,我已经创建了虚拟数据库并将表放入其中,但是当我要使用 mysql1 包获取数据时,出现错误</p>
<pre class="brush:php;toolbar:false;">E/flutter (15888): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 60332
E/flutter (15888): #0 _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:694:35)
E/flutter (15888): #1 _NativeSocket.connect (dart:io-patch/socket_patch.dart:960:12)
E/flutter (15888): #2 _RawSocket.connect (dart:io-patch/socket_patch.dart:1843:26)
E/flutter (15888): #3 RawSocket.connect (dart:io-patch/socket_patch.dart:21:23)
E/flutter (15888): #4 BufferedSocket.defaultSocketFactory
buffered_socket.dart:72
E/flutter (15888): #5 BufferedSocket.connect
buffered_socket.dart:89
E/flutter (15888): #6 MySqlConnection.connect
single_connection.dart:139
E/flutter (15888): #7 Mysql.getConnection
mysql.dart:11
E/flutter (15888): #8 _MyHomePageState._getCustomer
main.dart:37
E/flutter (15888): #9 _InkResponseState.handleTap
ink_well.dart:1096
E/flutter (15888): #10 GestureRecognizer.invokeCallback
recognizer.dart:253
E/flutter (15888): #11 TapGestureRecognizer.handleTapUp
tap.dart:627
E/flutter (15888): #12 BaseTapGestureRecognizer._checkUp
tap.dart:306
E/flutter (15888): #13 BaseTapGestureRecognizer.acceptGesture
tap.dart:276
E/flutter (15888): #14 GestureArenaManager.sweep
arena.dart:163
E/flutter (15888): #15 GestureBinding.handleEvent
binding.dart:464
E/flutter (15888): #16 GestureBinding.dispatchEvent
binding.dart:440
E/flutter (15888): #17 RendererBinding.dispatchEvent
binding.dart:336
E/flutter (15888): #18 GestureBinding._handlePointerEventImmediately
binding.dart:395
E/flutter (15888): #19 GestureBinding.handlePointerEvent
binding.dart:357
E/flutter (15888): #20 GestureBinding._flushPointerEventQueue
binding.dart:314
E/flutter (15888): #21 GestureBinding._handlePointerDataPacket
binding.dart:295
E/flutter (15888): #22 _invoke1 (dart:ui/hooks.dart:164:13)
E/flutter (15888): #23 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:361:7)
E/flutter (15888): #24 _dispatchPointerDataPacket (dart:ui/hooks.dart:91:31)
E/flutter (15888):</pre>
<p>我尝试了很多参考资料,告诉我将代码中的端口更改为 127.0.0.1 或 10.0.2.2,但仍然遇到相同的错误</p>
<pre class="lang-dart prettyprint-override"><code>import 'package:mysql1/mysql1.dart';
class Mysql {
Future<MySqlConnection> getConnection() async {
var settings = ConnectionSettings(
host: 'localhost',
port: 3306,
user: 'root',
password: '',
db: 'company');
return await MySqlConnection.connect(settings);
}
}
</code></pre>
<p>这是我用来从用户获取数据的代码</p>
<pre class="lang-dart prettyprint-override"><code> void _getCustomer() {
db.getConnection().then((conn) {
String sql = 'select mail from company.customer where id = 10;';
conn.query(sql).then((results) {
for (var row in results) {
setState(() {
mail = row[0];
print(row[0]);
});
print(results);
}
});
});
}
</code></pre>
<p>那里没有打印任何内容</p>
<p>这是我的 SQL Server 配置并且它正在运行
</p>
<p>尝试重新启动 SQL Server,但仍然得到相同的结果。但是当我尝试在我的工作台上使用相同的命令时,肯定会给我一个响应</p>
移动应用程序无法识别本地主机,您可以尝试此修复:
-以管理员身份打开cmd
ipconfig
3 从cmd复制IPv4地址
4 将“localhost”替换为您的 IPv4 地址
5 在 flutter clean 后运行您的应用