Can't connect to local MySQL server through socket - mysql

I'm getting the following error on my site when I upload it or submit a page:
mysql_real_escape_string() [function.mysql-real-escape-string]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
What in the world does this mean?

Since the error is being thrown by the call to mysql_real_escape_string() it rather implies that you didn't call mysql_connect() first and pass a valid db handle to mysql_real_escape_string (or the call to mysql_connect() failed).
In some circumstances, the mysql extension will attempt to connect automatically using the default settings in php.ini, failing over to my.cnf if these are not available - which obviously are not valid. Or it may be that the settings are valid but the mysqld is not running.
Have you got scripts which are connecting to the database successfully?
Do you have a username and password for the database?
Try:
check_running();
$user=''; // fill in your details
$password=''; // fill in your details
$hosts=array(
'localhost', '127.0.0.1', $_SERVER['HTTP_HOST'], $_SERVER['SERVER_ADDR']
);
foreach ($hosts as $addr) {
try_con($addr, $user, $password);
try_con($addr . ':3306', $user, $password);
}
function try_con($host, $user, $password)
{
$dbh=mysql_connect($host, $user, $password);
if ($dbh) {
print "Connected OK with $host, $user, $password<br />\n";
} else {
print "Failed with $host, $user, $password<br />\n";
}
}
function check_running()
{
// this assumes that you are using Apache on a Unix/Linux box
$chk=`ps -ef | grep httpd | grep -v grep`;
if ($chk) {
print "Checking for mysqld process: " . `ps -ef | grep mysqld | grep -v grep` . "<br />\n";
} else {
print "Cannot check mysqld process<br />\n";
}
}

Yes exactly, some servers pass the default connection parameters when using mysql functions before connection and throw off an error, some other servers work just fine wherever you place the code
it is always safer to just place mysql_real_escape_string() after establishing mysql_connect() connection

Related

Cannot connect to my Mysql DB with PHP7.0 and Apache2 (2 different VM)

I'm trying to connect to my MySQL DB located on 192.168.23.140 from my Web Frontend on .23.139
Here is my code :
$servername = "192.168.23.140";
$username = "web";
$password = "rootnetwork";
$dbname = "test";
try {
$conn = new PDO('mysql:host=$servername; dbname=$dbname', $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
Here is what i get :
Connection failed: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
I've been looking for some answers on the internet but none of them work.
Thank you for your help
$variables are not expanded in a single quotes string literal, they are only expanded in a double quoted string literals.
So
$conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);
You will also have to make sure that this MYSQL user account web is setup to allow connections from ip addresses that are not the ip of the machine running MySQL

PHP - The server requested authentication method unknown to the client

I want to connect to a MySQL database from PHP but I can't because "authentication method unknown", I found many solution but all of them about creating a new user with the old password type, but according to the PHP documentation, it should support the new password type. So is it possible to use the new MySQL password type with PHP 7.2.7?
Versions
PHP: 7.2.7MySQL Server: 8.0.12
PHP Code
// Create connection
$conn = new mysqli($servername, $username, $password);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
Result
Connection failed: The server requested authentication method unknown to the client

Error while connecting to Database on hosted server

Warning: mysql_connect(): (HY000/2002): Connection refused in
/home/vol14_1/byethost31.com/b31_16461744/htdocs/Mysql/con.php on line
7
Warning: mysql_select_db(): No such file or directory in
/home/vol14_1/byethost31.com/b31_16461744/htdocs/Mysql/con.php on line
8
Warning: mysql_select_db(): A link to the server could not be
established in
/home/vol14_1/byethost31.com/b31_16461744/htdocs/Mysql/con.php on line
8
I have the below code
<?php
$localhost="localhost";
$username=b31_16461744;
$pass=test123;
$dbname=b31_16461744_user;
$a= mysqli_connect($localhost,$user,$pass);
mysql_select_db($dbname);
if($a)
{
echo "connected..";
}
else
{
echo "not...!!";
}
?>
Sidenote: Assuming the credentials are correct, given to you by your web host.
There are several problems with this code (taken from a comment you left).
Firstly, three of your declarations are not quoted and are being treated as constants.
PHP error reporting would have thrown notices of undefined constants.
These are treated as constants:
$username=b31_16461744;
$pass=test123;
$dbname=b31_16461744_user;
You are also referencing the wrong variable for the username being $user which should be $username. Error reporting would have signabled an undefined variable notice.
Then you're mixing mysql_ with mysqli_ syntax. Those different MySQL APIs do NOT intermix. You must use the same one throughout your code.
Sidenote: The other question you posted Access denied for user 'test123'#'192.168.0.38' (using password: NO) you are using sql306.byethost31.com for the host. Make sure that is correct. I have no idea what settings that host wants you to use.
<?php
$localhost="localhost";
$username="b31_16461744";
$pass="test123";
$dbname="b31_16461744_user";
$a= mysqli_connect($localhost, $username, $pass);
mysqli_select_db($a, $dbname);
if($a)
{
echo "connected..";
}
else
{
echo "not...!!";
}
?>
or just use all four parameters:
<?php
$localhost="localhost";
$username="b31_16461744";
$pass="test123";
$dbname="b31_16461744_user";
$a= mysqli_connect($localhost, $username, $pass, $dbname);
if($a)
{
echo "connected..";
}
else
{
echo "not...!!" . mysqli_error($a);
}
?>
However, your else with the echo does not help you. Use mysqli_error() to get the real error.
I.e.: or die("Error " . mysqli_error($a));
Example from the manual
$link = mysqli_connect("myhost","myuser","mypassw","mydb")
or die("Error " . mysqli_error($link));
References:
http://php.net/manual/en/function.error-reporting.php
http://php.net/manual/en/mysqli.error.php
http://php.net/manual/en/function.mysqli-connect.php
http://php.net/manual/en/language.constants.php
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production
I Think Credentials are not correctly set. See Your connection statement.
For Reference :
While Working On Localhost, We write connection statement as :
$con=mysql_connect("localhost","root","");
$db1=mysql_select_db("DatabaseName",$con);
But, While working on server, we need to change the following credential.
Username and password values are must.
$con=mysql_connect("localhost","Username","password");
$db1=mysql_select_db("DatabaseName",$con);

Google cloud sql with php

I'm having a problem to connect my DB cloud sql with php.
I've tried every possible way to connect (pdo, mysqli & mysql)
none of them worked.
I've put an IPv4 for the could sql instance and authorized the compute engine IP in the allowed networks section.
When I'm trying to connect from the compute engine with
mysql --host=cloud_sql_IP --user=my_user --password
it's working and I'm able to see the tables.
On the php side I've put this code:
$db = mysql_connect(<CLOUD_SQL_IPV4>, 'root', '');
if (!$db){
die('Connect Error (' . mysql_error());
}
and I get "Connect Error (Permission denied)"
when I'm trying this way:
$conn = mysql_connect(":/cloudsql/<COMPUTE_INSTANCE>:<DB>", "root", "");
if (!$conn) {
die('Connect Error (' . mysql_error());
}
I'm getting:
"Connect Error (No such file or directory"
What else should I do?
Thanks!
Well after diggin into it for 2 days I managed to connect cloud DB
first run this for the permission fix (centos)
setsebool httpd_can_network_connect=1
And for the php it should be written this way:
new mysqli(
<IP_V4>, // host
'root', // username
'', // password
<DB_NAME>, // database name
null,
'/cloudsql/<GCE_INSTANCE>:<DB>'
);

MySQL via SSH Tunnel Access Denied

I want to use SSH tunnel to connect to MySQL DB on remote host.
I've set up the tunnel with command:
ssh user#host -L 3307:remote_mysql_hostname:3306
I can successfull connect with HeidiSQL using this settings:
hostname: localhost
user: remote_mysql_user_login
password: remote_mysql_user_password
port: 3307
But when i use PDO in PHP to connect, i get:
Access denied for user 'remote_mysql_user_login'#'localhost' (using password: YES)
My PDO Dns is sth like this:
mysql:type=Core_Db_Adapter_Pdo_Mysql;host=localhost;port=3307;dbname=db_name;
Where is the trick?
Solution:
#symcbean thanks!
The problem was (as suggested by #symcbean) in hostname.
Changing to '127.0.0.1' fix the problem.**
I borrowed from your example and was able to connect to a remote host using your same SSH tunnel setup. Can you share the PHP script you are using to get some more info about your setup?
You may try a hostname other than 'localhost' for your remote database you are connecting to.
setup ssh tunnel - I setup an entry in /etc/hosts for my_db_host_name instead of using localhost
ssh my_user#my_remote_host -L 3307:my_db_host_name:3306
<?php
$dsn = 'mysql:host=my_db_host_name;dbname=my_db;port=3307';
$username = 'my_user';
$password = 'my_pass';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
$result = $dbh->query("select * from my_table");
try {
foreach($result as $row) {
echo "column1: " . $row['column1'];
}
} catch (PDOException $pde) {
echo "PDO exception: $pde";
}
echo "done \n";
?>