php 从另一台计算机访问 XAMPP MySql 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3507205/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:01:39  来源:igfitidea点击:

Accesing XAMPP MySql Database from Another Computer

phpmysqlxampp

提问by Osukaa

So a friend of mine and I are using both xampp on ubuntu, if that helps, to connect between each other's website, We both created the same php file to connect, so we use de IP of the other, but then it says an error

所以我和我的一个朋友在 ubuntu 上同时使用 xampp,如果这有帮助,在彼此的网站之间连接,我们都创建了相同的 php 文件来连接,所以我们使用对方的 de IP,但它说一个错误

Warning: mysql_connect() [function.mysql-connect]: Host 'coke-laptop.local' is not allowed to connect to this MySQL server in /opt/lampp/htdocs/connection.php on line 2
Could not connect: Host 'coke-laptop.local' is not allowed to connect to this MySQL server

We have this code on the connection.php file:

我们在 connection.php 文件中有以下代码:

<?php
$link = mysql_connect('10.100.161.37','root','');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
//echo 'Connected successfully';

$db_selected = mysql_select_db('Prueba', $link);
if (!$db_selected) {
    die ('Can\'t use Prueba : ' . mysql_error());
}

// This could be supplied by a user, for example
$firstname = 'fred';
$lastname  = 'fox';

// Formulate Query
// This is the best way to perform an SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT * FROM Agencia");

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
    echo $row['ID'] . " ";
    echo $row['Nombre'] . "\n\r";
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
mysql_close($link);
?>

If we use the IP just like that, we can enter each others xampp normal welcome page.

如果我们就这样使用IP,我们就可以进入彼此的xampp正常欢迎页面。

回答by Colin Pickard

Check you have enabled remote access to the MySQL server. Open the my.cnf file (probably found inside xampp/etc/), go to the [mysqld]section and add the following (using your own ip address instead of the example)

检查您是否启用了对 MySQL 服务器的远程访问。打开 my.cnf 文件(可能在 xampp/etc/ 里面找到),进入该[mysqld]部分并添加以下内容(使用您自己的 ip 地址而不是示例)

bind-address=192.168.1.100

If there is a line that says skip-networking, comment that out so it looks like this:

如果有一行说skip-networking,请将其注释掉,使其看起来像这样:

# skip-networking

then restart the MySQL server

然后重启MySQL服务器

回答by pferate

It looks like your MySQL database isn't allowing you to connect remotely with the credentials you provided. You will need to configure a remote user to connect. Try looking into MySQL Grants.

看起来您的 MySQL 数据库不允许您使用您提供的凭据进行远程连接。您需要配置远程用户才能连接。尝试查看MySQL Grants

For Example:

例如:

GRANT SELECT, INSERT ON database.* TO 'someuser'@'somehost';