mysql server port number
I’ve just made a database on mysql on my server. I want to connect to this via my website using php. This is the contents of my connections file:
$dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'password'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql'); $dbname = 'epub'; mysql_select_db($dbname);
I know what the username/passwords are, and I know the IP address of the server. What I’m just wondering is how do I know which port to use?
What type of machine is this? If its linux check if mysqld is running, either «service mysqld status» or «/etc/init.d/mysqld status» as root.
9 Answers 9
If your MySQL server runs on default settings, you don’t need to specify that.
Default MySQL port is 3306.
[updated to show mysql_error() usage]
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql: '.mysql_error());
I’ve tried both the IP address on it’s own, and with :3306 appended to the end of it, none work. Is there a way I can find out if there is a different port? I did not set up this server.
Fitst you should check what is the error you’re getting from mysql_connect(). Use mysql_error() function for that. Knowing what the error is, will help us find out what is going on, and what’s the actual issue.
Never mind. I added a rule in the firewall settings for the port number. I thought this had been done before hand. Thanks anyway
For windows, If you want to know the port number of your local host on which Mysql is running you can use this query on MySQL Command line client —
SHOW VARIABLES WHERE Variable_name = 'port'; mysql> SHOW VARIABLES WHERE Variable_name = 'port'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3306 | +---------------+-------+ 1 row in set (0.00 sec)
It will give you the port number on which MySQL is running.
echo 'Connected successfully'; mysql_close($link); // we connect to localhost at port 3307 $link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password'); if (!$link) < die('Could not connect: ' . mysql_error()); >echo 'Connected successfully'; mysql_close($link); ?>
if you want to have your port as a variable, you can write php like this:
$username = user; $password = pw; $host = 127.0.0.1; $database = dbname; $port = 3308; $conn = mysql_connect($host.':'.$port, $username, $password); $db=mysql_select_db($database,$conn);
If you specify ‘localhost’ the client libs default to using the filesystem system socket on a Unix system — trying the mysql_default_socket value from php.ini (if set) then the my.cnf value.
If you connect using a different tool, try issuing the command «show variables like ‘%socket%'»
If you want to use a network port (which is a wee bit slower) then try specifying 127.0.0.1 or a physical interface asociated with the machine.
default port of mysql is 3306
default pot of sql server is 1433
This is a PDO-only visualization, as the mysql_* library is deprecated.
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "I am connected.
"; // . continue with your code // PDO closes connection at end of script > catch (PDOException $e) < echo 'PDO Exception: ' . $e->getMessage(); exit(); > ?>
Note that this OP Question appeared not to be about port numbers afterall. If you are using the default port of 3306 always, then consider removing it from the uri, that is, remove the port=$port; part.
If you often change ports, consider the above port usage for more maintainability having changes made to the $port variable.
Some likely errors returned from above:
PDO Exception: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. PDO Exception: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.
In the below error, we are at least getting closer, after changing our connect information:
PDO Exception: SQLSTATE[HY000] [1045] Access denied for user 'GuySmiley'@'localhost' (using password: YES)
After further changes, we are really close now, but not quite:
PDO Exception: SQLSTATE[HY000] [1049] Unknown database 'mustard'
How to Determine Which Port MySQL is Running On
When the MySQL server is configured, the user needs to set the suitable port. Later, a specified port is used to run the MySQL server on the local system. Sometimes, users encounter multiple situations where they want to determine the port number. During that time, multiple SQL queries can be used.
This post will discuss multiple ways to get which port MySQL runs.
How to Find Which Port MySQL is Running?
Multiple commands can be used to get which port MySQL is running on, such as:
- SHOW variable WHERE variable_name = ‘port’;
- SELECT @@port;
- SHOW GLOBAL variable LIKE ‘port’;
- SHOW variable WHERE variable_name in (‘hostname’,’port’);
Let’s check out the working of all commands one by one!
Initially, connect with the MySQL server by executing the following command:
Run the “SHOW” command with a “WHERE” clause to get the port number:
- “SHOW” command will display all specified records.
- “WHERE” clause is utilized for extracting the required records.
- “variable_name” is the resultant column name.
- “port” is added to get the port number.
As you can see, the running port of the MySQL server is obtained as “3306”:
The “SELECT” command can also be used for determining the running port of the MySQL server:
In the above-stated command, the “@@port” is the system variable:
Another way to get the current MySQL running port number is this:
- “GLOBAL variable” statement is used to show the global system variable value.
- “LIKE” clause searches for a provided pattern in the table column:
If you want to get the port number along with the hostname, run the below-provided command:
It can be observed that the hostname is “Haier-PC” and the port number is “3306”:
We have provided different ways to find which port MySQL is running on.
Conclusion
To determine which port MySQL is running on, the “SHOW variable WHERE variable_name = ‘port’;” command, the “SELECT @@port;” command, the “SHOW GLOBAL variable LIKE ‘port’;” command, and the “SHOW variable WHERE variable_name in (‘hostname’,’port’);” command can be used. This post demonstrated several ways to get which port MySQL is running on.
About the author
Maria Naz
I hold a master’s degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.
How to test which port MySQL is running on and whether it can be connected to?
Neither works. Not sure if both are supposed to work, but at least one of them should 🙂 How can I make sure that the port is indeed 3306? Is there a linux command to see it somehow? Also, is there a more correct way to try it via a url?
14 Answers 14
To find a listener on a port, do this:
You should see a line that looks like this if mysql is indeed listening on that port.
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
Port 3306 is MySql’s default port.
To connect, you just have to use whatever client you require, such as the basic mysql client.
mysql -h localhost -u user database
Or a url that is interpreted by your library code.
@mbmast the 127. means listen on local host only (not externally accessible). The 0.0.0.0 means «all interfaces», and therefore is (usually) externally visible.
I thought to be externally visible, it had to be the machine’s own IP address and that 0.0.0.0 means the service is not available from anywhere. Do I have that wrong? I have a box running MySQL, the firewall has 3306 open from any IP address but MySQL is refusing the connection, I thought because currently MySQL is listening on 0.0.0.0.
Should merge this with @bortunac’s answer, which references the -p parameter. Adding the process(es) really helps make this information more useful.
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
grep port /etc/mysql/my.cnf ( at least in debian/ubuntu works )
mysql -u user_name -puser_pass -e "SHOW variables LIKE 'port';"
in /etc/mysql/my.cnf to see possible restrictions
It will show the list something like below:
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1393/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1859/master tcp 0 0 123.189.192.64:7654 0.0.0.0:* LISTEN 2463/monit tcp 0 0 127.0.0.1:24135 0.0.0.0:* LISTEN 21450/memcached tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 16781/mysqld
Use as root for all details. The -t option limits the output to TCP connections, -l for listening ports, -p lists the program name and -n shows the numeric version of the port instead of a named version.
In this way you can see the process name and the port.
Try only using -e ( —execute ) option:
$ mysql -u root -proot -e "SHOW GLOBAL VARIABLES LIKE 'PORT';" (8s 26ms) +---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3306 | +---------------+-------+
Replace root by your «username» and «password»
A simpler approach for some : If you just want to check if MySQL is on a certain port, you can use the following command in terminal. Tested on mac. 3306 is the default port.
mysql —host=127.0.0.1 —port=3306
If you successfully log in to the MySQL shell terminal, you’re good! This is the output that I get on a successful login.
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9559 Server version: 5.6.21 Homebrew Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>