简体   繁体   English

使用http协议的mysql_connect()

[英]mysql_connect() using http protocol

I am trying to connect to a MySql server (not localhost) from my computer using the code below. 我正在尝试使用以下代码从我的计算机连接到MySql服务器(而不是localhost)。 It is giving this error: 它给出了这个错误:

Warning: mysql_connect() [function.mysql-connect]: [2002] Connection refused (trying to connect via tcp://10.6.3.6:3306) in on line 7 警告:mysql_connect()[function.mysql-connect]:[2002]第7行的连接被拒绝(尝试通过tcp://10.6.3.6:3306进行连接)

I wonder if we can use the http protocol to connect instead of tcp that is being used by default? 我想知道我们是否可以使用http协议代替默认使用的tcp进行连接? I searched quite a bit on how to change the protocol, but most of the answers were describing how to connect to localhost, and not much about how to connect to another server. 我搜索了很多有关如何更改协议的内容,但是大多数答案都描述了如何连接到本地主机,而没有太多关于如何连接到另一台服务器的信息。 Please help. 请帮忙。

PS: I am able to connect to the server by going to http://10.6.3.6/phpmyadmin/ ...). PS:我可以通过访问http://10.6.3.6/phpmyadmin/ ...连接到服务器。 So I am sure the server is up. 因此,我确定服务器已启动。

My Code 我的守则

<?php

$db_hostname = '10.6.3.6';
$db_database = 'db_user11';
$db_username = 'db_user11';
$db_password = '########';

$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());

?>

You can't. 你不能。 HTTP is not a protocol suitable for this. HTTP不适用于此协议。

You are talking about 2 processes communicating here (MySQL Server and your app) and they either do it via shared memory, pipes or sockets. 您在这里谈论的是2个进程(MySQL Server和您的应用程序)进行通信,它们通过共享内存,管道或套接字进行通信。 Those are the 3 ways that processes communicate with each other. 这些是进程相互通信的3种方式。

First of all, HTTP is layered on top of TCP/IP. 首先,HTTP位于TCP / IP之上。
In order to connect to something via HTTP, it needs to run an HTTP server. 为了通过HTTP连接到某物,它需要运行HTTP服务器。 MySQL does not run an HTTP server and there's no current/realistic/supported way to tunnel an SQL connection through HTTP. MySQL不运行HTTP服务器,并且没有当前/实际/受支持的通过HTTP隧道建立SQL连接的方法。 Even if there was, HTTP is not exactly the best protocol for this. 即使有,HTTP也不是最好的协议。

In short: no. 简而言之:没有。 You're trying to solve the wrong problem. 您正在尝试解决错误的问题。 You need to configure your MySQL server to allow connections from other machines over the network, give the user you're trying to connect with appropriate permissions to connect from other machines and make sure the MySQL server is reachable from other machines. 您需要配置MySQL服务器,以允许通过网络与其他计算机的连接,为要尝试连接的用户提供从其他计算机进行连接的适当权限,并确保从其他计算机可以访问MySQL服务器。

Just because the phpmyadmin is hosted on that server, does not neccesarily mean that the database server is in the same IP address. 仅仅因为phpmyadmin托管在该服务器上,并不一定表示数据库服务器位于相同的IP地址。 Please ask the domain administrator to give you the details of database server. 请要求域管理员为您提供数据库服务器的详细信息。

Maybe just check and make sure the mysql user account you are connecting with has proper permissions set for remote access. 也许只需检查并确保与您连接的mysql用户帐户已为远程访问设置了适当的权限。

In other words, this may be obvious but make sure the user has valid remote credentials. 换句话说,这可能很明显,但是请确保用户具有有效的远程凭据。

On the command-line ( if you can relate to this ) , one can do this in mysql to add a user allowed to connect from remote connexion: 在命令行上(如果可以与此关联),可以在mysql中执行此操作以添加允许从远程连接进行连接的用户:

mysql> CREATE USER 'monty'@'%' IDENTIFIED BY 'some_pass';

mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'%'
->     WITH GRANT OPTION;

This would create a user, monty with password some_pass, with FULL privileges on all databases, all tables from anywhere . 这将创建一个用户monty,其密码为some_pass,并且对所有数据库, 任何位置的所有表都具有FULL特权。 The % is the key here, and is needed for remote access. %是此处的键,是远程访问所必需的。

MySQL users created through gui tools often set the host to 'localhost' and this would not allow a remote connexion. 通过gui工具创建的MySQL用户通常将主机设置为“ localhost”,这将不允许远程连接。

Hope this helps. 希望这可以帮助。 Good-luck. 祝好运。

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

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