{"id":14549,"date":"2024-01-12T06:26:55","date_gmt":"2024-01-12T13:26:55","guid":{"rendered":"https:\/\/www.mysqltutorial.org\/?page_id=14549"},"modified":"2024-01-12T06:40:58","modified_gmt":"2024-01-12T13:40:58","slug":"mysql-port","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-port\/","title":{"rendered":"MySQL Port"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about the default MySQL port, find the port that MySQL is using, and change the default port.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Default MySQL port<\/h2>\n\n\n\n<p>By default, MySQL uses port 3306 for communication between the client and the MySQL server. When you <a href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/connect-to-mysql-server\/\">connect to a MySQL server<\/a>, if you don&#8217;t specify a port, the client assumes the port 3306.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Finding MySQL port<\/h2>\n\n\n\n<p>You can find the port that the MySQL server is listening from the <code>port<\/code> variable or the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-configuration-file\/\">MySQL configuration files<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL configuration files<\/h3>\n\n\n\n<p>To find the port of MySQL server, you can check the MySQL configuration files. Their locations depend on your operating system. <\/p>\n\n\n\n<p>Here are the common MySQL configuration locations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Linux<\/strong>: \/etc\/my.cnf or \/etc\/mysql\/my.cnf<\/li>\n\n\n\n<li><strong>Windows<\/strong>: C:\\ProgramData\\MySQL\\MySQL Server X.X\\my.ini<\/li>\n\n\n\n<li><strong>macOS<\/strong>: \/etc\/my.cnf or \/usr\/local\/mysql\/my.cnf.<\/li>\n<\/ul>\n\n\n\n<p>Find the <code>port<\/code> variable in the <code>[mysqld]<\/code> section in the configuration:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">&#91;mysqld]\nport=3306<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\">MySQL variables<\/h3>\n\n\n\n<p>Alternatively, you can find the port number MySQL is currently running by showing the <code>port<\/code> variable if you are already connected to the MySQL server:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">show <span class=\"hljs-keyword\">global<\/span> variables like <span class=\"hljs-string\">'port'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+---------------+-------+\r\n| Variable_name | Value |\r\n+---------------+-------+\r\n| port          | <span class=\"hljs-number\">3306<\/span>  |\r\n+---------------+-------+\r\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Changing MySQL port<\/h2>\n\n\n\n<p>First, open the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-configuration-file\/\">MySQL configuration file<\/a> in a text editor.<\/p>\n\n\n\n<p>Second, change the port under the <code>[mysqld]<\/code> section to a new port and save the configuration file.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">&#91;mysqld]\nport=3301 # new port<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-administration\/restart-mysql-server\/\">restart the MySQL server<\/a> for the changes to take effect.<\/p>\n\n\n\n<p class=\"note\">Note that you need to ensure that the new port is not in use by other applications.<\/p>\n\n\n\n<p>When you connect to a MySQL server using a non-default port, you need to use the <code>-P<\/code> or <code>--porr<\/code> option followed by the port number:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">mysql -h <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">hostname<\/span>&gt;<\/span> -P <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">port_number<\/span>&gt;<\/span> -u <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">username<\/span>&gt;<\/span> -p\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this command:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>&lt;hostname><\/code> is the host of the MySQL server.<\/li>\n\n\n\n<li><code>&lt;port_number&gt;<\/code> is the port number on which MySQL is listening.<\/li>\n\n\n\n<li><code>&lt;username><\/code> is the user account you want to use for the connection.<\/li>\n<\/ul>\n\n\n\n<p>The command will prompt you to enter the password for the specified username.<\/p>\n\n\n\n<p>For example, the following command connects to the local MySQL server at the port 3301 with the root password:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">mysql -h localhost -P 3301 -u root -p<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Port security<\/h2>\n\n\n\n<p>When you open port 3306, you should restrict which IP addresses can access it so the MySQL server will not be accessible from untrusted hosts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The default MySQL port is 3306.<\/li>\n\n\n\n<li>The port variable stores the port that the MySQL server is currently using.<\/li>\n\n\n\n<li>The port option under the [mysqld] section of the MySQL configuration file specifies the port to which the MySQL server is currently listening.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"14549\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-port\/\"\n\t\t\t\tdata-post-title=\"MySQL Port\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"14549\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-port\/\"\n\t\t\t\tdata-post-title=\"MySQL Port\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn about the default MySQL port, find the port that MySQL is using, and change the default port. Default MySQL port By default, MySQL uses port 3306 for communication between the client and the MySQL server. When you connect to a MySQL server, if you don&#8217;t specify a port, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":441,"menu_order":66,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-14549","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL Port 3306<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn about the default MySQL port, find the port that MySQL uses, and change the default port.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-port\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL Port 3306\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn about the default MySQL port, find the port that MySQL uses, and change the default port.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-port\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-12T13:40:58+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-administration\\\/mysql-port\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-administration\\\/mysql-port\\\/\",\"name\":\"MySQL Port 3306\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"datePublished\":\"2024-01-12T13:26:55+00:00\",\"dateModified\":\"2024-01-12T13:40:58+00:00\",\"description\":\"In this tutorial, you will learn about the default MySQL port, find the port that MySQL uses, and change the default port.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-administration\\\/mysql-port\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-administration\\\/mysql-port\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-administration\\\/mysql-port\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Administration\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-administration\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL Port\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL Port 3306","description":"In this tutorial, you will learn about the default MySQL port, find the port that MySQL uses, and change the default port.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-port\/","og_locale":"en_US","og_type":"article","og_title":"MySQL Port 3306","og_description":"In this tutorial, you will learn about the default MySQL port, find the port that MySQL uses, and change the default port.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-port\/","og_site_name":"MySQL Tutorial","article_modified_time":"2024-01-12T13:40:58+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-port\/","url":"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-port\/","name":"MySQL Port 3306","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"datePublished":"2024-01-12T13:26:55+00:00","dateModified":"2024-01-12T13:40:58+00:00","description":"In this tutorial, you will learn about the default MySQL port, find the port that MySQL uses, and change the default port.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-port\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-port\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-port\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Administration","item":"https:\/\/www.mysqltutorial.org\/mysql-administration\/"},{"@type":"ListItem","position":3,"name":"MySQL Port"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/14549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=14549"}],"version-history":[{"count":5,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/14549\/revisions"}],"predecessor-version":[{"id":14559,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/14549\/revisions\/14559"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/441"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=14549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}