Mysql server port number

I just created a mysql database on my server. I want to connect to this through 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 the username / password and I know the IP address of the server. I'm just wondering how to find out which port to use?

+46
database php mysql database-connection port-number
Sep 17 '10 at 14:51
source share
8 answers

If your MySQL server is running by default, you do not need to specify it.

The default MySQL port is 3306.

[updated to show use of mysql_error ()]

 $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql: '.mysql_error()); 
+56
Sep 17 '10 at 14:52
source share

For windows. If you want to find out the port number of your local host running Mysql , you can use this query for the 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.

+48
Aug 21 '13 at 9:10
source share

check out this 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); ?> 
+9
Sep 17 '10 at 16:18
source share

if you want your port to be 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); 
+4
Aug 6 '13 at 9:55 on
source share

If you specify "localhost", then client libraries use the system file socket on a Unix system by default - try the mysql_default_socket value from php.ini (if set), then my.cnf.

If you are connecting using another tool, try the command "show variables like"% socket% ""

If you want to use a network port (which is slightly slower), try specifying 127.0.0.1 or the physical interface associated with the machine.

+2
Sep 17 '10 at 15:26
source share

the default port for mysql is 3306

By default, the sql server server is 1433

+2
Jul 05 '13 at 17:25
source share

This visualization is for PDO only, since 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 does not seem to apply to afterall port numbers. If you always use the default port 3306 , then consider removing it from the uri, that is, remove the port=$port; .

If you change ports frequently, consider using the port above for better maintainability with the changes made to the $port variable.

Some probable 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 error below, we are at least closer after changing our connection information:

 PDO Exception: SQLSTATE[HY000] [1045] Access denied for user 'GuySmiley'@'localhost' (using password: YES) 

After further changes, we are really close, but not quite:

 PDO Exception: SQLSTATE[HY000] [1049] Unknown database 'mustard' 

From the PDO Connections Guide:

+1
Sep 12 '15 at 8:16
source share
  <?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'; } ?> 
0
Dec 15 '17 at 14:49
source share



All Articles