mysql server port number

PhpMysqlDatabaseDatabase ConnectionPort Number

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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());

Solution 2 - Php

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.

Solution 3 - Php

check this out dude

<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
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);
?>

Solution 4 - Php

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);

Solution 5 - Php

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.

Solution 6 - Php

> default port of mysql is 3306 > > default pot of sql server is 1433

Solution 7 - Php

This is a PDO-only visualization, as the mysql_* library is deprecated.

<?php
	// Begin Vault (this is in a vault, not actually hard-coded)
	$host="hostname";
	$username="GuySmiley";
	$password="thePassword";
	$dbname="dbname";
	$port="3306";
	// End Vault
	
	try {
		$dbh = new PDO("mysql:host=$host;port=$port;dbname=$dbname;charset=utf8", $username, $password);
		$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		echo "I am connected.<br/>";

		// ... 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'

From the Manual on PDO Connections:

Solution 8 - Php

port number 3306 is used for MySQL and tomcat using 8080 port.more port numbers are available for run the servers or software whatever may be for our instant compilation..8080 is default for number so only we are getting port error in eclipse IDE. jvm and tomcat always prefer the 8080.3306 is default port number for MySQL.So only do not want to mention every time as "localhost:3306"

  <?php  
    $dbhost = 'localhost:3306';
//3306 default port number $dbhost='localhost'; is enough to specify the port number
//when we are utilizing xammp default port number is 8080.
      $dbuser = 'root';
      $dbpass = '';
    	 $db='users';
    				
             $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$db) or die ("could not connect to mysql");      
    
              // mysqli_select_db("users") or die ("no database");  
    
    if(! $conn ) {
        die('Could not connect: ' . mysqli_error($conn));
    }else{
        echo 'Connected successfully';
    }
    ?>

Solution 9 - Php

try

$conn = mysql_connect($host, $username, $password, $port);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question109221793View Question on Stackoverflow
Solution 1 - PhpMchlView Answer on Stackoverflow
Solution 2 - PhpNikhil AgrawalView Answer on Stackoverflow
Solution 3 - PhpAdi SembiringView Answer on Stackoverflow
Solution 4 - PhpzhihongView Answer on Stackoverflow
Solution 5 - PhpsymcbeanView Answer on Stackoverflow
Solution 6 - PhpRavi KumarView Answer on Stackoverflow
Solution 7 - PhpDrewView Answer on Stackoverflow
Solution 8 - Phpsivaa reddyView Answer on Stackoverflow
Solution 9 - PhpSudishresthaView Answer on Stackoverflow