Avatar of Danny Kon
Danny KonFlag for Netherlands

asked on 

Use a cmd/batch to check if Mysql port 3306 is open and if not open the 3306 port (netsh)

Use a cmd/batch to check if Mysql port 3306 is open and if not open the 3306 port (netsh)
This has to work at windows 7 or higher

Thanks for your help

Danny
Windows BatchScripting LanguagesWindows OS

Avatar of undefined
Last Comment
oBdA
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

In order to see which ports are open
netstat -an

Open in new window

In order to check if the service (MySQL) is listening correctly you have to telnet to it
telnet serverName 3306

Open in new window

In order to open port
netsh firewall add portopening TCP 3306 "Open Port 3306 MySQL"

Open in new window

Avatar of Qlemo
Qlemo
Flag of Germany image

Do you mean "open in the Windows Firewall"?
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Danny Kon
Danny Kon
Flag of Netherlands image

ASKER

oBdA:

:))

Thanks Danny
Avatar of oBdA
oBdA

Just noticed that there's an "exit" missing after line 16, so for the sake of completeness, here's the corrected version:
@echo off
setlocal enabledelayedexpansion
set RuleName=MySql
set RulePort=3306
REM Profile can be any one of these: public, private, domain, any
set RuleProfile=domain

netsh.exe advfirewall firewall show rule name="%RuleName%" | find.exe /i "-----" >NUL
if not errorlevel 1 (
	echo Rule '%RuleName%' already set, exiting.
	exit /b 0
)
echo Rule '%RuleName%' not set, will add ...
whoami.exe /groups | find.exe "S-1-16-12288" >NUL
if errorlevel 1 (
	echo ERROR: Not running elevated, unable to change firewall rules.
	exit /b 1
)
netsh.exe advfirewall firewall add rule name="%RuleName%" dir=in action=allow protocol=TCP localport=%RulePort% profile=%RuleProfile%
if errorlevel 1 (
	echo ERROR: Could not set the firewall rule.
	exit /b 1
)

Open in new window

Windows OS
Windows OS

This topic area includes legacy versions of Windows prior to Windows 2000: Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions including Windows Mobile.

129K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo