0
我在java中使用了ServerSocket来检查mysql是否正在运行。如果它没有运行,我提示用户请运行你的mysql服务器。但是,我的一些用户抱怨说,尽管mysql正在运行,但我们收到了错误消息。它只发生在马科斯。我发现的是。macos上的端口绑定
Java代码:
public static boolean isPortFree(int port)
{
if(port <= 0)
{
return false;
}
ServerSocket sock = null;
try
{
String bindAddress = System.getProperty("bindaddress", "127.0.0.1");
sock = new ServerSocket(port,0,InetAddress.getByName(bindAddress));
}
catch(Exception ex)
{
return false;
}
finally
{
if (sock != null)
{
try
{
sock.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
return true;
}
如果我设置系统属性bindaddress
到localhost
返回false同我设置为127.0.0.1
返回true。 本地主机,127.0.0.1是相同的,为什么它的行为是这样的。它只发生在macos中。我认为这不是my.cnf
bindaddress问题。因为我在
mysql -uroot -hlocalhost
mysql -uroot -h127.0.0.1
都检查命令提示符工作。
的Netstat
Mac系统
tcp4 0 0 *.3306 *.* LISTEN
其他操作系统
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
有TCP4和TCP之间有什么区别?
任何想法为什么会发生?
我不明白你为什么使用'ServerSocket'这个。试着打开一个'Socket'到端口3306.如果成功,MySQL正在运行。如果没有,不是。 – EJP