Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) on Mac (SOLVED)












1














I am currently using this PHP code:






<?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);

?>





To try and display selections from the table 'messages' using external code:






<?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());
?>





To connect to the database with. However, it only returns the error "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)", which I have found no (Mac-compatible) answers for solving. The server was running and available during this.










share|improve this question
























  • Try specifying 127.0.0.1 instead of localhost. Most mysql drivers try to short-circuit localhost to the unix socket path.
    – Sammitch
    Nov 13 at 17:56










  • That returns Could not connect to MySQL: Can't connect to MySQL server on '127.0.0.1' (111)? I used localhost as the hostname in the database
    – owensteel
    Nov 13 at 18:09










  • Then chances are that either: 1. MySQL isn't running. 2. MySQL isn't running on the default port. 3. Something else on your system is preventing the connection, eg a firewall or other security thing.
    – Sammitch
    Nov 13 at 18:14






  • 1




    Also you should stop using @ to suppress errors. It's like shoving your fingers in your ears and yelling so that you can't hear the fire alarm going off. Errors and warnings should be fixed, not suppressed.
    – Sammitch
    Nov 13 at 18:14








  • 2




    Connect using the mysql command in Terminal and use the status command. It will tell you the socket file path, for example in my case UNIX socket: /tmp/mysql.sock
    – Joni
    Nov 13 at 21:14
















1














I am currently using this PHP code:






<?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);

?>





To try and display selections from the table 'messages' using external code:






<?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());
?>





To connect to the database with. However, it only returns the error "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)", which I have found no (Mac-compatible) answers for solving. The server was running and available during this.










share|improve this question
























  • Try specifying 127.0.0.1 instead of localhost. Most mysql drivers try to short-circuit localhost to the unix socket path.
    – Sammitch
    Nov 13 at 17:56










  • That returns Could not connect to MySQL: Can't connect to MySQL server on '127.0.0.1' (111)? I used localhost as the hostname in the database
    – owensteel
    Nov 13 at 18:09










  • Then chances are that either: 1. MySQL isn't running. 2. MySQL isn't running on the default port. 3. Something else on your system is preventing the connection, eg a firewall or other security thing.
    – Sammitch
    Nov 13 at 18:14






  • 1




    Also you should stop using @ to suppress errors. It's like shoving your fingers in your ears and yelling so that you can't hear the fire alarm going off. Errors and warnings should be fixed, not suppressed.
    – Sammitch
    Nov 13 at 18:14








  • 2




    Connect using the mysql command in Terminal and use the status command. It will tell you the socket file path, for example in my case UNIX socket: /tmp/mysql.sock
    – Joni
    Nov 13 at 21:14














1












1








1







I am currently using this PHP code:






<?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);

?>





To try and display selections from the table 'messages' using external code:






<?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());
?>





To connect to the database with. However, it only returns the error "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)", which I have found no (Mac-compatible) answers for solving. The server was running and available during this.










share|improve this question















I am currently using this PHP code:






<?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);

?>





To try and display selections from the table 'messages' using external code:






<?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());
?>





To connect to the database with. However, it only returns the error "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)", which I have found no (Mac-compatible) answers for solving. The server was running and available during this.






<?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);

?>





<?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);

?>





<?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());
?>





<?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());
?>






php html mysql database-connection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 at 16:24

























asked Nov 13 at 17:50









owensteel

317




317












  • Try specifying 127.0.0.1 instead of localhost. Most mysql drivers try to short-circuit localhost to the unix socket path.
    – Sammitch
    Nov 13 at 17:56










  • That returns Could not connect to MySQL: Can't connect to MySQL server on '127.0.0.1' (111)? I used localhost as the hostname in the database
    – owensteel
    Nov 13 at 18:09










  • Then chances are that either: 1. MySQL isn't running. 2. MySQL isn't running on the default port. 3. Something else on your system is preventing the connection, eg a firewall or other security thing.
    – Sammitch
    Nov 13 at 18:14






  • 1




    Also you should stop using @ to suppress errors. It's like shoving your fingers in your ears and yelling so that you can't hear the fire alarm going off. Errors and warnings should be fixed, not suppressed.
    – Sammitch
    Nov 13 at 18:14








  • 2




    Connect using the mysql command in Terminal and use the status command. It will tell you the socket file path, for example in my case UNIX socket: /tmp/mysql.sock
    – Joni
    Nov 13 at 21:14


















  • Try specifying 127.0.0.1 instead of localhost. Most mysql drivers try to short-circuit localhost to the unix socket path.
    – Sammitch
    Nov 13 at 17:56










  • That returns Could not connect to MySQL: Can't connect to MySQL server on '127.0.0.1' (111)? I used localhost as the hostname in the database
    – owensteel
    Nov 13 at 18:09










  • Then chances are that either: 1. MySQL isn't running. 2. MySQL isn't running on the default port. 3. Something else on your system is preventing the connection, eg a firewall or other security thing.
    – Sammitch
    Nov 13 at 18:14






  • 1




    Also you should stop using @ to suppress errors. It's like shoving your fingers in your ears and yelling so that you can't hear the fire alarm going off. Errors and warnings should be fixed, not suppressed.
    – Sammitch
    Nov 13 at 18:14








  • 2




    Connect using the mysql command in Terminal and use the status command. It will tell you the socket file path, for example in my case UNIX socket: /tmp/mysql.sock
    – Joni
    Nov 13 at 21:14
















Try specifying 127.0.0.1 instead of localhost. Most mysql drivers try to short-circuit localhost to the unix socket path.
– Sammitch
Nov 13 at 17:56




Try specifying 127.0.0.1 instead of localhost. Most mysql drivers try to short-circuit localhost to the unix socket path.
– Sammitch
Nov 13 at 17:56












That returns Could not connect to MySQL: Can't connect to MySQL server on '127.0.0.1' (111)? I used localhost as the hostname in the database
– owensteel
Nov 13 at 18:09




That returns Could not connect to MySQL: Can't connect to MySQL server on '127.0.0.1' (111)? I used localhost as the hostname in the database
– owensteel
Nov 13 at 18:09












Then chances are that either: 1. MySQL isn't running. 2. MySQL isn't running on the default port. 3. Something else on your system is preventing the connection, eg a firewall or other security thing.
– Sammitch
Nov 13 at 18:14




Then chances are that either: 1. MySQL isn't running. 2. MySQL isn't running on the default port. 3. Something else on your system is preventing the connection, eg a firewall or other security thing.
– Sammitch
Nov 13 at 18:14




1




1




Also you should stop using @ to suppress errors. It's like shoving your fingers in your ears and yelling so that you can't hear the fire alarm going off. Errors and warnings should be fixed, not suppressed.
– Sammitch
Nov 13 at 18:14






Also you should stop using @ to suppress errors. It's like shoving your fingers in your ears and yelling so that you can't hear the fire alarm going off. Errors and warnings should be fixed, not suppressed.
– Sammitch
Nov 13 at 18:14






2




2




Connect using the mysql command in Terminal and use the status command. It will tell you the socket file path, for example in my case UNIX socket: /tmp/mysql.sock
– Joni
Nov 13 at 21:14




Connect using the mysql command in Terminal and use the status command. It will tell you the socket file path, for example in my case UNIX socket: /tmp/mysql.sock
– Joni
Nov 13 at 21:14

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286842%2fcant-connect-to-local-mysql-server-through-socket-var-run-mysqld-mysqld-sock%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286842%2fcant-connect-to-local-mysql-server-through-socket-var-run-mysqld-mysqld-sock%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word