无法通过mac上的socket'/var/run/mysqld/mysqld.sock'(2)连接到本地mysql服务器

5hcedyr0  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(409)

我目前正在使用以下php代码:

<?php
// Get a connection for the database
require_once('mysqli_connect.php');

// Create a query for the database
$query = "SELECT username, message FROM messages";

// Get a response from the database by sending the connection
// and the query
$response = @mysqli_query($dbc, $query);

// If the query executed properly proceed
if($response){

echo '<table align="left"
cellspacing="5" cellpadding="8">

<tr><td align="left"><b>username</b></td>
<td align="left"><b>Message</b></td></tr>';

// mysqli_fetch_array will return a row of data from the query
// until no further data is available
while($row = mysqli_fetch_array($response)){

echo '<tr><td align="left">' . 
$row['username'] . '</td><td align="left">' . 
$row['message'] . '</td><td align="left">';

echo '</tr>';
}

echo '</table>';

} else {

echo "Couldn't issue database query<br />";

echo mysqli_error($dbc);

}

// Close connection to the database
mysqli_close($dbc);

?>

要尝试使用外部代码显示表“messages”中的选项,请执行以下操作:

<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'Cheese');

$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('Could not connect to MySQL: ' .
mysqli_connect_error());
?>

以连接到数据库。但是,它只返回错误“can't connect to local mysql server through socket'/var/run/mysqld/mysqld.sock'(2)”,我没有找到(mac兼容的)解决方法。在此期间,服务器正在运行且可用。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题