php mysql服务器端口号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3736407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql server port number
提问by 109221793
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:
我刚刚在我的服务器上的 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);
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?
我知道用户名/密码是什么,我知道服务器的 IP 地址。我只是想知道我怎么知道要使用哪个端口?
回答by Mchl
If your MySQL server runs on default settings, you don't need to specify that.
如果您的 MySQL 服务器以默认设置运行,则无需指定。
Default MySQL port is 3306.
默认 MySQL 端口是 3306。
[updated to show mysql_error() usage]
[更新以显示 mysql_error() 用法]
$conn = mysql_connect($dbhost, $dbuser, $dbpass)
or die('Error connecting to mysql: '.mysql_error());
回答by Nikhil Agrawal
For windows, If you want to know the port number of your local host on which Mysqlis running you can use this query on MySQL Command line client --
对于 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)
It will give you the port number on which MySQL is running.
它将为您提供运行 MySQL 的端口号。
回答by Adi Sembiring
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);
?>
回答by zhihong
if you want to have your port as a variable, you can write php like this:
如果你想把你的端口作为一个变量,你可以这样写 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);
回答by Drew
This is a PDO-only visualization, as the mysql_*
library is deprecated.
这是一个仅限 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();
}
?>
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.
请注意,此 OP 问题毕竟与端口号无关。如果你使用的是3306
always的默认端口,那么考虑把它从uri中去掉,也就是去掉port=$port;
一部分。
If you often change ports, consider the above port usage for more maintainability having changes made to the $port
variable.
如果您经常更改端口,请考虑上述端口使用情况,以便对$port
变量进行更改以提高可维护性。
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 changingour 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:
从PDO 连接手册:
回答by Ravi Kumar
default port of mysql is 3306
default pot of sql server is 1433
mysql 的默认端口是 3306
sql server 的默认锅是 1433
回答by symcbean
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.
如果您指定 'localhost',则客户端库默认使用 Unix 系统上的文件系统套接字 - 尝试来自 php.ini(如果设置)的 mysql_default_socket 值,然后是 my.cnf 值。
If you connect using a different tool, try issuing the command "show variables like '%socket%'"
如果您使用其他工具进行连接,请尝试发出命令“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.
如果您想使用网络端口(稍微慢一点),请尝试指定 127.0.0.1 或与机器关联的物理接口。
回答by Sudishrestha
try
尝试
$conn = mysql_connect($host, $username, $password, $port);
回答by sivaa reddy
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"
端口号 3306 用于 MySQL 和使用 8080 端口的 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';
}
?>