mysql服务器端口号

发布于 2024-09-24 12:23:24 字数 349 浏览 1 评论 0原文

我刚刚在我的服务器上的 mysql 上创建了一个数据库。我想使用 php 通过我的网站连接到此。这是我的连接文件的内容:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass)
    or die('Error connecting to mysql');

$dbname = 'epub';
mysql_select_db($dbname);

我知道用户名/密码是什么,并且我知道服务器的 IP 地址。我只是想知道我如何知道要使用哪个端口?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

红颜悴 2024-10-01 12:23:24

如果您的 MySQL 服务器以默认设置运行,则无需指定。

默认 MySQL 端口为 3306。

[已更新以显示 mysql_error() 用法]

$conn = mysql_connect($dbhost, $dbuser, $dbpass)
    or die('Error connecting to mysql: '.mysql_error());

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());
断念 2024-10-01 12:23:24

对于 Windows,如果您想知道运行 Mysql 的本地主机的端口号,您可以在 MySQL 命令行客户端上使用此查询 -

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)

它将为您提供 MySQL 所在的端口号跑步。

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.

月朦胧 2024-10-01 12:23:24

看看这个伙计

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

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);
?>
打小就很酷 2024-10-01 12:23:24

如果你想让你的端口作为变量,你可以这样编写 php:

$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 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);
許願樹丅啲祈禱 2024-10-01 12:23:24

如果您指定“localhost”,则客户端库默认使用 Unix 系统上的文件系统系统套接字 - 尝试 php.ini 中的 mysql_default_socket 值(如果已设置),然后尝试 my.cnf 值。

如果您使用其他工具进行连接,请尝试发出命令“显示变量,如'%socket%'”。

如果您想使用网络端口(速度稍慢),请尝试指定 127.0.0.1 或与机器。

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.

疯到世界奔溃 2024-10-01 12:23:24

mysql默认端口是3306

sql server默认的pot是1433

default port of mysql is 3306

default pot of sql server is 1433

说谎友 2024-10-01 12:23:24

这是仅 PDO 可视化,因为 mysql_* 库已弃用。

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

请注意,这个OP问题似乎与端口号无关。如果您始终使用默认端口 3306,请考虑将其从 uri 中删除,即删除 port=$port; 部分。

如果您经常更改端口,请考虑上述端口用法,以提高对 $port 变量进行更改的可维护性。

上面返回的一些可能的错误:

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.

在下面的错误中,在更改我们的连接信息之后,我们至少更接近了:

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

经过进一步的更改,我们现在确实很接近,但还不够:

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

从手册中PDO 连接

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:

思念绕指尖 2024-10-01 12:23:24

端口号 3306 用于使用 8080 端口的 MySQL 和 tomcat。更多端口号可用于运行服务器或软件,无论我们的即时编译是什么。8080 是默认端口号,因此只有我们在 eclipse IDE 中收到端口错误。 jvm和tomcat总是更喜欢8080.3306作为MySQL的默认端口号。所以只是不想每次都提到“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';
    }
    ?>

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';
    }
    ?>
浅浅 2024-10-01 12:23:24

尝试

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

try

$conn = mysql_connect($host, $username, $password, $port);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文