简体   繁体   English

我应该如何在Web主机上引用MySQL服务器?

[英]How should I refer to a MySQL server on a web host?

When using PHP to connect to a MySQL database on a web host, what is the best way to refer to the server? 使用PHP连接到Web主机上的MySQL数据库时,引用服务器的最佳方法是什么?

The MySQL admin page on my web host says what the IP address of the server is, but can I use something else other than the IP number? 我的网络主机上的MySQL管理页面说明服务器的IP地址是什么,但我可以使用除IP号码之外的其他内容吗?

Eg. 例如。

$con = mysql_connect("l00.50.10.10","user","password");
if (!$con) { die('Could not connect: ' . mysql_error()); }

When I use localhost I get this output: 当我使用localhost我得到这个输出:

Could not connect: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 无法连接:无法通过套接字'/var/lib/mysql/mysql.sock'连接到本地MySQL服务器(2)

You can refer to the script's system with "localhost" as the host. 您可以使用“localhost”作为主机来引用脚本的系统。

$con = mysql_connect("localhost", "user", "password");

However, connecting to the IP is most likely failing because you have a lowercase L where you should have a one digit (but I'm hoping the IP in the question was just an example). 但是,连接到IP很可能是失败的,因为你有一个小写的L,你应该有一个数字(但我希望问题中的IP只是一个例子)。

Also, use mysqli or PDO instead of mysql, which is on the verge of deprecation. 另外,使用mysqli或PDO而不是mysql,它正处于弃用的边缘。

You can the database server's host name, eg mydbbox.example.com . 您可以使用数据库服务器的主机名,例如mydbbox.example.com Using the IP address may be faster, as no DNS lookup will be necessary. 使用IP地址可能更快,因为不需要DNS查找。

Also see this SO question . 另见这个问题

The answer to this question depends on how your host (or their control panel) configures MySQL privileges. 这个问题的答案取决于您的主机(或其控制面板)如何配置MySQL权限。 The privileges are IP or hostname specific, so often connecting by IP or by hostname may not have the same connection results. 权限是特定于IP或主机名的,因此通常通过IP或主机名连接可能不会具有相同的连接结果。 If in doubt, I would stick to exactly the settings they give you. 如果有疑问,我会坚持他们给你的设置。

Read more: http://dev.mysql.com/doc/refman/5.0/en/account-names.html 阅读更多: http//dev.mysql.com/doc/refman/5.0/en/account-names.html

If database host is same computer - use localhost . 如果数据库主机是同一台计算机 - 请使用localhost MySQL will connect by local socket, not tcpip (network) when you use localhost with port number or not. 当您使用带有端口号的localhost时,MySQL将通过本地套接字连接,而不是tcpip(网络)。

Sometimes server does not support socket connections, and you will get error like this: 有时服务器不支持套接字连接,您将收到如下错误:

Could not connect: Can't connect to local MySQL server through socket ... 无法连接:无法通过套接字连接到本地MySQL服务器...

EDIT/CORRECTED: If you cant connect by socket - use 127.0.0.1 . 编辑/更正:如果您无法通过套接字连接 - 请使用127.0.0.1 It will force conncetion by tcpip. 它将强制通过tcpip进行conncetion。

If database server is installed on other machine - you can use IP address or domain . 如果数据库服务器安装在其他计算机上 - 您可以使用IP addressdomain

IP address is better in most cases, because connection can be established even when DNS (domain name server) is not working correctly or when domain expired. 在大多数情况下,IP地址更好,因为即使DNS(域名服务器)无法正常工作或域过期,也可以建立连接。


And one advice - use mysqli_ instead of mysql_ functions in new application. 一个建议 - 在新的应用程序中使用mysqli_而不是mysql_函数 New functions are very similar in use, but more secure. 新功能在使用中非常相似,但更安全。

Old mysql_ functions are obsolete and they are not recommended for new applications. 旧的mysql_函数已过时, 不建议用于新的应用程序。 They are still available in PHP, because of compatibility with old, outdated software (CMS systems, e-commerce systems etc 由于与旧的,过时的软件(CMS系统,电子商务系统等)的兼容性,它们仍然可以在PHP中使用

PHP Help says (about mysql_connect): PHP帮助说(关于mysql_connect):

Use of this extension is discouraged. 不鼓励使用此扩展名。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应该使用MySQLi或PDO_MySQL扩展。


More about connecting with localhost - info from MySQL Reference 有关连接localhost的更多信息 - 来自MySQL Reference的信息

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. 在Unix上,MySQL程序特别对待主机名localhost,其方式可能与您期望的与其他基于网络的程序相比有所不同。 For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. 对于与localhost的连接,MySQL程序尝试使用Unix套接字文件连接到本地服务器。 This occurs even if a --port or -P option is given to specify a port number. 即使给出--port或-P选项指定端口号,也会发生这种情况。 To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1 要确保客户端与本地服务器建立TCP / IP连接,请使用--host或-h指定主机名值127.0.0.1

Note, that "--port" and "-P" options are decribed in other context than PHP, but connection mechanism works similar and localhost is "special" name, doesnt matter is this PHP, console or some other software. 请注意,“ - port”和“-P”选项在PHP之外的其他上下文中描述,但连接机制的工作方式类似, localhost是“特殊”名称,无关紧要是这个PHP,控制台或其他一些软件。

Have you tried to type in google something like this? 您是否曾尝试输入类似Google的内容?

php Could not connect: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) php无法连接:无法通过套接字'/var/lib/mysql/mysql.sock'连接到本地MySQL服务器(2)

This depends mostly on how your host has things configured. 这主要取决于主机配置的方式。 From my experience you will use "localhost", the server's IP address or a subdomain of some sort set up by the host. 根据我的经验,您将使用“localhost”,服务器的IP地址或主机设置的某种子域。

I'd start by testing "localhost" as this is the default for most shared hosting environments. 我首先测试“localhost”,因为这是大多数共享托管环境的默认设置。

$con = mysql_connect("localhost", "username", "password");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM