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

5

我和我的朋友都在Ubuntu上使用XAMPP,我们想要连接彼此的网站。我们都创建了相同的PHP文件进行连接,但是当我们使用对方的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

我们在connection.php文件中有这段代码:
```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);
?>

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

你是否已经给 coke-laptop.local 访问数据库的权限了?也就是说,该主机有一个用户可以访问数据库? - Stephen
2个回答

11

请检查是否已启用MySQL服务器的远程访问。打开my.cnf文件(通常位于xampp/etc/内),转到[mysqld]部分并添加以下内容(将示例中的IP地址替换为您自己的IP地址)

bind-address=192.168.1.100
如果有一行内容写着 skip-networking,请注释掉它,使其看起来像这样:
# skip-networking

然后重新启动MySQL服务器


1

看起来你提供的凭据无法让你远程连接到MySQL数据库。你需要配置一个远程用户来进行连接。可以尝试查看MySQL授权

例如:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接