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