File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes How to tell which port mySQL is running on? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to tell which port mySQL is running on? " Watch "How to tell which port mySQL is running on? " New topic
Author

How to tell which port mySQL is running on?

Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
I'm trying to view some JSP code example in the browser and I have been told I need to set the correct port address in con variable in my code in order to get connection.
Where do I find out the address of the port running mySQL?


Rob Petterson
SCJP
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
The default port is 3306. MySQL stores these settings in a file called my.cnf. That is where you will look to find out which port it is assigned to.


Matthew Phillips
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
That's the default port, but it might not be running on that port. You can't find out in the JSP (without scanning all the possible ports -ugh) what port it's on. Instead, go directly to MySQL (through the command-line interface) and check what port - or look in:
/etc/my.cnf (on Unix/Linux)
C:\WINNT\my.ini (on Windows)
If it doesn't list anything, or that file doesn't exist, don't panic. It means it's using the defaults: port 3306.
Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
Can you tell me the exact command I have to enter
on the command line interface, as I'm a little confused on this.
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
I'm pretty sure that if you start up the client program (called mysql, on windows it's mysql.exe and it's in the "bin" folder for your mysql installation) and it finds your mysql database, then it HAS to be on port 3306. The only way to get mysql to connect to a database on a different port is to start it with the "-p" option.
If you start it and can't find your mysql database, then you have to look for your my.ini (on windows) file or my.cnf (also sometimes on windows now that I think of it - but not recommended - and on Unix/linux) and see what it says in there. If both my.ini AND my.cnf do not exist THEN the following is true:
1. If YOU (or whoever gave you this mysql installation) did NOT recompile mysql with a different port hardcoded in, then it HAS to be port 3306.
If all this fails, then there is one other way: netstat. netstat is a command-line utility that comes with Windows, Linux, Unix. Go to a command-prompt and type:
netstat -a
It will tell you what's listening on what port. You should be able to find your mysql there. NOTE: YOU MUST DO netstat -a ON THE SERVER THAT HOUSES THE MYSQL DATABASE, NOT ON THE CLIENT!!
Good luck!
Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
Thanks Rob for the reply.
When I typed netstat -a, this was part of the result:
local Address - rob:3306
foreign address - localhost:3654
state - ESTABLISHED

Part of the java code I'm using is this:
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/wrox?user=xxx&password=xxxxxxxx");

When I try and fire up the application in the browser, the book I'm using for this particular example tells me to enter:
http://localhost:8080/begjsp-ch16/booklist.jsp
But I'm still getting an error saying that 'Could not locate DB Driver'.
Any ideas as to where I'm going wrong?
[ January 29, 2003: Message edited by: Rob Petterson ]
Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
I'm answering my own post here, but this is for anyone else who may have the same query:
To verify the port, perform the following steps:
1. Execute the winmysqladmin.exe file; it's available in the mysql\bin folder. I'm assuming that you are working on Windows.
2. From the pop-up window select the Variables tab.
3. In this tab, under the Variable name column search for an entry called port. The value against this indicates the port number used by mySQl.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
I am also facing the same issue:

I tried to find out on which port my sql is running, but
c:\Users\Prabhat\netstart -a

its giving me result:

'netstart' is not a recognizable command.

where i am wrong ?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3837

Prabhat Ranjan wrote:... 'netstart' is not a recognizable command. where i am wrong ?

It might be easier to find it by the method suggested in the previous reply.
BTW, the Correct command is netstat


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

Find whoever setup the database and ask them. Else, assume 3306. Guessing or searching with netstat is what you do when you are desperate.


My Blog: Down Home Country Coding with Scott Selikoff
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
I tried this correct command: netstat -a

Result is below:


C:\Users\Prabhat>netstat -a

Active Connections

TCP 0.0.0.0:3306 Prabhat-PC:0 LISTENING


What i can assume on which port my mysql is running (3306 )
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3837

Proto Local Address Foreign Address State
TCP 0.0.0.0:135 Prabhat-PC:0 LISTENING
TCP 0.0.0.0:445 Prabhat-PC:0 LISTENING
TCP 0.0.0.0:2869 Prabhat-PC:0 LISTENING
TCP 0.0.0.0:3306 Prabhat-PC:0 LISTENING
TCP 0.0.0.0:5151 Prabhat-PC:0 LISTENING
.........
......

Looks like your system is listening on port 3306, which most probably be MySql. Are you not able to connect to MySql?
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
I am able to connect and login into sql.

But when i am running simple jdbc code. i am getting error.

I was running Hibernate but some error so some one suggested to first check your mysql is running on which port.

I am trying hard from last 3 days . But this is final point i am here.


Error in hibernate: org.hibernate.exception.GenericJDBCException: Cannot open connection

Error in jdbc connection with mysql


Exception in thread "main" com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.ConnectException
MESSAGE: Connection refused: connect

STACKTRACE:

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at JDBCSample.main(JDBCSample.java:13)


** END NESTED EXCEPTION **



Last packet sent to the server was 0 ms ago.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at JDBCSample.main(JDBCSample.java:13)

Paul Okeke
Ranch Hand

Joined: May 16, 2009
Posts: 58
Executing: D:\WINDOWS\system32\CScript.exe


Shows me the exact port where my MySQL is installed.
But I am not really sure if this is from windows or was added by my XAMPP installation.

Check your system.....

OCJP 1.6
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
I executed this command on c:\Windows\system32

below is output:

C:\Windows\System32>CScript.exe
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Usage: CScript scriptname.extension [option...] [arguments...]

Options:
//B Batch mode: Suppresses script errors and prompts from displaying
//D Enable Active Debugging
//E:engine Use engine for executing script
//H:CScript Changes the default script host to CScript.exe
//H:WScript Changes the default script host to WScript.exe (default)
//I Interactive mode (default, opposite of //B)
//Job:xxxx Execute a WSF job
//Logo Display logo (default)
//Nologo Prevent logo display: No banner will be shown at execution time
//S Save current command line options for this user
//T:nn Time out in seconds: Maximum time a script is permitted to run
//X Execute script in debugger
//U Use Unicode for redirected I/O from the console
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
my problem solved now. I am able to connect both jdbc and hibernate also.

Thanks everyone.

Writing the wrong port no on url;
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
This is not because any error in mysql configuration.
Only thing is people copy and paste code form so many toutrial, where the code is not complete.

See the below link and so many people faced same issue because of only port is missing in url.

http://www.roseindia.net/hibernate/firstexample.shtml#post

Also i suggest i was using the wrong port only in url while mysql db was running on the other one.

Usually mysql runs on port as default 3306.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Prabhat Ranjan wrote:See the below link and so many people faced same issue because of only port is missing in url.

The RoseIndia site is full of bad examples, wrong code and horrible advice. Do the tech world a favor and spread the word!
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
thanks for your comments.

I have seen for the save example for error on hibernate they were suggesting to use Transcation.

While transcation Object and transcation .commit() is used when wen we do update but not mandatory for save.
As session object is there and flussing the session is enough for inserting the records in table.


are you agree with me ? or correct if i am wrong.

Now my am going ahead and making new concepts by executing live.

can i ask a question.

I am facing one probelm while retriving the data using select query in hibernate.

when to use

and

I am facing problem while retriving the records using iterator over List.

it could be better if you tell me the difference bteween to use List and Query() in above case.


About Rose India, they are having n no of topics in every Area. But more important is accuracy , first to execute it properly. As so many developers are very new to topics and use it blindly, finally gets error and speding lot of time to search error types in google.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3837

This looks a different question now from the topic. You may start a new thread in the appropriate forum for that as this is now a Hibernate related question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to tell which port mySQL is running on?