Port Number in MySQL

Preet Sanghavi Jul 12, 2022
  1. Concept of Port Numbers
  2. Create a Table in MySQL
  3. Show the Port Number in MySQL
Port Number in MySQL

In this tutorial, we aim to explore how to show the port number in MySQL.

Concept of Port Numbers

Before we begin, let us try to understand the concept of port numbers. A port number is used to understand and comprehend the process in a particular network on the internet or a remote server.

An example of the use case of port numbers is the Hypertext Transfer Protocol, also referred to as HTTPS, which runs on port 80 by default.

While working with MySQL, it is important to understand which port is used to run the MySQL server. Let us try to understand how to fetch this port number.

Create a Table in MySQL

Before we begin, we will create a dummy dataset to work with. Here we will create a table, student_details, along with a few rows.

-- create the table student_details
CREATE TABLE student_details(
  stu_id int,
  stu_firstName varchar(255) DEFAULT NULL,
  stu_lastName varchar(255) DEFAULT NULL,
  primary key(stu_id)
);
-- insert rows to the table student_details
INSERT INTO student_details(stu_id,stu_firstName,stu_lastName)
 VALUES(1,"Preet","Sanghavi"),
 (2,"Rich","John"),
 (3,"Veron","Brow"),
 (4,"Geo","Jos"),
 (5,"Hash","Shah"),
 (6,"Sachin","Parker"),
 (7,"David","Miller");

The above query creates a table with rows containing the students’ first and last names. To view the entries in the data, we will use the following code:

SELECT * FROM student_details;

The above code would give the following output:

stu_id	stu_firstName	stu_lastName
1	      Preet	        Sanghavi
2	      Rich	        John
3	      Veron	        Brow
4	      Geo	        Jos
5	      Hash	        Shah
6	      Sachin	    Parker
7	      David	        Miller

Show the Port Number in MySQL

Now, we have understood how to create a table and view it. Let us understand which port is used to create this table in the database.

In this example, we have used the boatdb database and the student_details table. We can access the port number in MySQL with the help of the following block of code:

SHOW VARIABLES WHERE Variable_name = 'port';

The output of the query mentioned above can be illustrated as follows:

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+

It is important to note that this is not the only way to fetch the port number. We can also access the port number with the help of the following query.

select @@port;

The output of the query above is the same as before and can be illustrated as follows:

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+

Therefore, we have successfully ventured to understand two different techniques to identify the port number of a MySQL server.

Preet Sanghavi avatar Preet Sanghavi avatar

Preet writes his thoughts about programming in a simplified manner to help others learn better. With thorough research, his articles offer descriptive and easy to understand solutions.

LinkedIn GitHub