PHP not connecting to MYSQL via local socket - php

I have just installed mysql onto my macbook pro, mysql seems to be working, however i cannot connect to mysql from php via mysqli. I keep getting errors. I have read many different articles and web pages, all of which seem complicated and not solving my issue.
I have added /tmp/mysock to the php.ini file:
pdo_mysql.default_socket= /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock
I have installed apache, php, mysql all separately and not using software such as MAMP, XAMP, phpmyadmin or MYSQLworkbench
This is my PHP code that is trying to connect to the database, and following the code is the error that is displayed at the browser.
<?php
$title = $_POST['blog_title'];
$body = $_POST['blog_entry'];
$link = mysqli_connect('localhost','root','password','blog');
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
mysqli_select_db($link,"blog") or die("Unable to select database");
$query = "INSERT INTO entires(title,body) VALUES ($title,$body);";
mysqli_query($query);
mysqli_close($link);
header('Location: ViewBlog.php');
exit();
?>
This is the error i get:
Error: Unable to connect to MySQL. Debugging errno: 2054 Debugging error: The server requested authentication method unknown to the client
Ive been trying to figure this out for so long, any help would be much appreciated, thanks.
The database i'm trying to access, i have already created in mysql ("blog");

Try checking the user auth type in MySQL.
I just ran into the same error while setting up wp on Win 10, PHP 7.3 and MySQL 8.0.13.
This was the full error message I was getting:
Warning: mysqli_connect(): The server requested authentication method
unknown to the client [caching_sha2_password] in
C:\inetpub\wwwroot\dbtest.php on line 2
Warning: mysqli_connect(): (HY000/2054): The server requested
authentication method unknown to the client in
C:\inetpub\wwwroot\dbtest.php on line 2 Error: Unable to connect to
MySQL. Debugging errno: 2054 Debugging error: The server requested
authentication method unknown to the client
To resolve the issue I had to set the MySQL user to use the standard authentication type.
Here is the SQL Script I ran to fix the issue:
ALTER USER 'user'#'localhost' IDENTIFIED WITH mysql_native_password BY 'pass';

Related

How to fix HY000/2054 when implementing MySQL with Abyss Web Server?

I'm starting up a web server using Abyss X1, and just tried implementing a MySQL server through PHP with the following two scripts:
connect_db.php :
#Connect on 'localhost' for user 'admin'
#with password 'RoboticCowboy2019' to database 'site_db'.
$dbc = mysqli_connect
( 'localhost' , 'admin', 'password', 'site_db' )
OR die
( mysqli_connect_error() );
#Set encoding to match PHP script encoding.
mysqli_set_charset($dbc, 'utf8' );
and
require.php :
#Incorporate the MySQL connection script.
require ('../connect_db.php');
#Display MySQL version and host.
if(mysqli_ping($dbc)){
echo 'MySQL Server'.mysqli_get_server_info($dbc).'on'.mysqli_get_host_info($dbc);
}
However, when I go to http://localhost/require.php, I get these errors:
"Warning: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password] in /Applications/Abyss Web Server/connect_db.php on line 6"
"Warning: mysqli_connect(): (HY000/2054): The server requested authentication method unknown to the client in /Applications/Abyss Web Server/connect_db.php on line 6
The server requested authentication method unknown to the client"
I'm following the instructions of a book that I know is outdated, but I've been able to get around every problem until now. I've also read that it has something to do with MySQL updating and implementing sha2, while PHP still does not? I'm doing this for my compsci class, so I'd appreciate all the help I can get.
Thank you!
Alter the user
ALTER USER 'mysqluser'#'localhost' IDENTIFIED WITH mysql_native_password BY 'mysqlpassword';
or use
shell>mysql --default-auth=mysql_native_password

PHP - Mysql: can't connect to database

I've setup mysql server 8.0, Apache2.4, php 7.2 and created a simple database.
As a start I am trying to write a php code for connecting to the database but i can't figure it out.
My code:
<?php
$con = mysqli_connect("localhost","Don","password","db1");
if (!$con) {
echo "Error: " . mysqli_connect_error();
exit();
}
echo 'Connected to MySQL';
?>
I get Error: The server requested authentication method unknown to the client.
I've tried several things: connect to root, 192.168.0.1 instead of localhost but nothing is working.
Any ideas? Thanks.
(post for someone who has the same problem)
i searched about the old password issue but i couldnt find the old_passwords var, not even in my.cnf file.
so what i did was uninstalling mysql server 8.0 and installing 5.7.2 and it worked.
ps i didnt configure anything differently while setting up the 5.7.2 version.

PHP7 Not connecting to Microsoft SQL Server 2012

I have been searching for an answer for last 3 days before posting.
I have installed SQL Server 2012 and, Apache24, PHP7.0
The Apache started fine, PHP7 Started fine
I can browse localhost/info.php and it shows perfect
Installed ODBC
Installed the SQL extensions drivers
Configured PHP.ini and apache conf file
SQL services started through SQL configuration manager
also allowed in the network configuration the TCP\IP and named pipes
I can't login to the SQL server using Microsoft SQL server manager
So all seems to be fine but every time I try to do a simple PHP code to connect my PHP to the SQL server I get the below error
Warning: mysqli_connect(): (HY000/2002): No connection could be made
because the target machine actively refused it. in
C:\Apache24\htdocs\index.php on line 13 Could not connect
Have disabled the firewall also and even added a rule with few ports
I am running on windows 10 Enterprise
Also when I change the server name to WALEED\SQLEXPRESS I get the below
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo
failed: No such host is known. in C:\Apache24\htdocs\index.php on line
13
Tried to search in the SQL server log but couldn't find any errors
All extensions for my PHP are working fine on info.php
Index file :
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$dbhost = 'localhost';
$dbuser = 'admin';
$dbpass = 'admin2017';
$db = 'sunto';
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db);
if(! $conn){
die('Could not connect') ;
}
echo 'Successfully Connected';
$sql = 'Connected Successfully';
$retvalue = mysqli_query($sql);
if(! $retvalue){
die('Cannot connect to SQL: ');
}
echo 'DataBase test_db13 has successfully created';
mysqli_close($conn);
?>
In the comments you said:
what is strange when i Try to connect with sqlsrv_connect(
"localhost", $connectionInfo ); it is fine connected but when i try to
connect using mysqli. it shows error
There's nothing strange about that. The clue's right there in the name of the method you're using:
mysqli_connect()
That code library is for connecting to MySQL databases only.
You need to use the sqlsrv_ library instead if you want to connect to SQL Server. They're completely different database platforms, with completely different PHP libraries to use for working with them. As you saw yourself, it works when you use sqlsrv_connect.
I'm not really sure why you then tried to use mysqli after that, or why you expected that to be successful.

Mysql Operation timeout Error

Warning: mysqli_connect(): (HY000/2002): Operation timed out in
/Applications/XAMPP/xamppfiles/htdocs/conn.php on line 4
Connection failed: Operation timed out
Get the following error while connecting to XAMPP on Mac OS X.
Code line is
$conn=mysqli_connect("SNA","root","","localhost");
// Check connection
if (mysqli_connect_errno()) {
echo "Connection failed: " . mysqli_connect_error();
}
Is it a Mysql Host problem or a Mysql default socket problem and how to resolve it? Default Host is 3306. I dont know where my mysql.sock file is generated and whether its generating or not.
change your code here, interchange host and database name
$conn=mysqli_connect("localhost","root","","SNA");
check php manual for more info : http://php.net/manual/en/function.mysqli-connect.php

Why am I getting mysql_connect(): Lost connection to MySQL server and how can I fix it?

My php code fails to connect to the mysql database on my web server. I get the following error:
mysql_connect(): Lost connection to MySQL server at 'reading initial
communication packet', system error: 110 in filename at line 71.
The same code works fine when run from my development machine pointed at the mysql database on the server I'm trying to run this from (I copied the file up to the server and I'm trying to run it via ssh on that server). I have verified that the mysql user in the script can connect to the database from the server by running mysql from the ssh command line on that server using the same user name and password as specified in my php script.
here is my code:
function jsw_set_cnxn($env){
global $env;
$cnxn = mysql_connect($env['db_svr'], $env['db_usr'], $env['db_pwd'])
or die ('DB Connection Error: ' . mysql_error());
mysql_select_db ($env['db'], $cnxn);
return $cnxn;
}
I've seen this error caused when using the servers IP address when it expects localhost as the server address.
This is because the server is the machine mysql expects but the wrong host coming in. Try using localhost.

Categories