Warning: mysqli_connect(): (HY000/2001): Can't create UNIX socket (12) - php

I have a problem with mysqli connection, in my project sometimes appears the following error:
Warning: mysqli_connect(): (HY000/2001): Can't create UNIX socket (12)
line error code is as follows:
$this->cnx_ = mysqli_connect($this->server_, $this->user_, $this->pass_,$this->db_);
I searched about it and can't find the solution...
Thanks!

Related

Occasional "php_network_getaddresses: getaddrinfo failed" connecting to DigitalOcean Managed Database

The other day I purchased a DigitalOcean Managed Database to use as a secondary MySQL server to reduce load on our primary CPanel CentOS server.
This has worked great but every couple of hours our error logs from our PHP scripts fill with the following errors:
[24-Jan-2022 04:16:21 UTC] PHP Warning: mysqli::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known
[24-Jan-2022 04:16:21 UTC] PHP Warning: mysqli::__construct(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known
[24-Jan-2022 04:16:21 UTC] PHP Warning: mysqli_set_charset(): Couldn't fetch mysqli
[24-Jan-2022 04:16:21 UTC] PHP Warning: mysqli::prepare(): Couldn't fetch mysqli
[24-Jan-2022 04:16:21 UTC] PHP Fatal error: Uncaught Error: Call to a member function bind_param() on null
These errors are originating on this line:
$db = new mysqli("ygo-external-database-do-user-10645539-0.b.db.ondigitalocean.com:25060","username","password", "database");
I don't think it's a firewall issue since it is working most of the time. The website is also using Cloudflare but I don't think that is the issue either as I paused Cloudflare once when it occurred and the issue remained for ~20 minutes or so. Restarting the Litespeed web server tends to fix this immediately but I am non the wiser as to why this is happening.

Warning: mysqli::__construct(): (HY000/2002): No such file or directory in [duplicate]

This question already has answers here:
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in
(17 answers)
Closed 2 years ago.
I'm totally stuck having spent a couple of days trying to figure this out...
I have set up an Apache web server (version 2.4.34) on my Mac OS running Mojave (10.14.3) and have installed the current version of MySQL Community Server (8.0.15). Both running perfectly as I can determine (from command line). I have PHP 7.1.23 installed. When I create a .php file with the following code and put it in my Sites directory and then in Safari go to http://localhost/~dave/hello.php I get this error:
"Warning: mysqli::__construct(): (HY000/2002): No such file or
directory in /Users/dave/Sites/hello.php on line 7 Connection failed:
No such file or directory".
Code in hello.php:
<?php
$servername = "localhost";
$username = "dave";
$password = "*****";
//Create connection
$conn = new mysqli($servername, $username, $password);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
I have tried everything I can think of, including those suggestions in the other two similar posts (this one: My database user exists, but I still get an (HY000/2002): No such file or directory and this one: PHP Warning: mysqli::__construct(): (HY000/2002): No such file or directory (Debian GNU/Linux 9)), and I cannot connect to MySQl with php. In particular, I checked with the php info page and the socket for mysqli is given as var/mysql/mysql.sock. In the relevant table in mysql the value for socket is given as /tmp/mysql.sock. I tried changing the php.ini.default file by adding each of these sockets (in place of the empty value) and neither worked (still generates the same error). I thought that the 2002 MySQL socket error might be the problem, so I made a symlink by doing this:
sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
but all that did was generate a new error:
Warning: mysqli::__construct(): The server requested authentication
method unknown to the client [caching_sha2_password] in
/Users/dave/Sites/hello.php on line 7
Warning: mysqli::__construct(): (HY000/2054): The server requested
authentication method unknown to the client in
/Users/dave/Sites/hello.php on line 7 Connection failed: The server
requested authentication method unknown to the client
So I removed the symlink...and returned to the original error.
I also tried replacing "localhost" with the IP address 127.0.0.1 as well as "localhost:3306" as other question threads have suggested. Nothing worked here either.
Please help, I am new to this kind of use of the computer/web design and I'm out of obvious (googleable/understandable) options.
Use 127.0.0.1 instead of localhost.
I got some help from some local tech guys in my area and it turns out that fixing the socket error was the first step, like I tried to do above. I eventually did it this way, using the solution by noun:
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in
The second error message was then generated (same one as in my question, here again):
mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in /Users/dave/Sites/hello.php on line 7
Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in /Users/dave/Sites/hello.php on line 7
This was fixed by the SQL-based solution (June 9 2018 answer) to this question:
php mysqli_connect: authentication method unknown to the client [caching_sha2_password]
This SQL statement was simply run in MySQL from the terminal. After restarting the Apache server I reloaded the .php file in Safari and this resulted in a successful mysqli connection to MySQL with PHP. I also tried a PDO connection and this also worked.

Failed to connect to MySQL: Can't connect to MySQL server on 'sql306.juichost.ml' (113 "No route to host")

I have looked everywhere for a fix, but could only find ones for localhost or private sites.
I keep getting this error:
Failed to connect to MySQL: Can't connect to MySQL server on 'sql306.juichost.ml' (113 "No route to host")
The website is Here. I am using this website host for mysql, but hostmonster for the actual hosting.
I have tried ping sql306.juichost.ml and gotten this:
ping: sendto: Host is down
Request timeout for icmp_seq 876.
I have no idea what to that means though.
In the error log I see:
[01-Nov-2017 09:52:56 UTC] PHP Warning: include(): Failed opening 'inc/database.php' for inclusion (include_path='.:/opt/alt/php56/usr/share/pear:/opt/alt/php56/usr/share/php') in /home/aussieug/public_html/ppyazi/license/login.php on line 2
And
[01-Nov-2017 09:52:56 UTC] PHP Notice: Undefined variable: con in /home/aussieug/public_html/ppyazi/license/login.php on line 14
Line 2:
include 'inc/database.php';
Line 14:
$checksettings = mysqli_query($con, "SELECT * FROM `settings` LIMIT 1") or die(mysqli_error($con));
Please keep in mind that I am very new to terminal and php so I need detailed answers.

Unknown MySQL server host

After a php verison change i have the following error
Warning: mysqli::mysqli(): (HY000/2005): Unknown MySQL server host
'clouddb.myhost.gr:3306' (2) in
/var/www/vhosts/mywebside.gr/httpdocs/system/library/db/mysqli.php on
line 7Warning: DB\MySQLi::__construct(): Couldn't fetch mysqli in
/var/www/vhosts/mywebside.gr/httpdocs/system/library/db/mysqli.php on
line 10Warning: DB\MySQLi::__construct(): Couldn't fetch mysqli in
/var/www/vhosts/mywebside.gr/httpdocs/system/library/db/mysqli.php on
line 10 .
And i cant understand what is the problem
You might be passing the clouddb.myhost.gr:3306 all together, on the mysqlcli, the port have its own parameter:
mysql:host=clouddb.myhost.gr;port=3306;
Related question:
SQLSTATE[HY000] [2005] Unknown MySQL server host 'mysql1.alwaysdata.com:3306' (2)

MYSQL and PHPMYADMIN cant connect via php

Ive set up phpmyadmin and mySql on my windows server but when i try to connect via php i get this error :
Warning: mysqli::mysqli(): MySQL server has gone away in C:\xampp\htdocs\newservertest\dbscripts\sqlconnection.php on line 5
Warning: mysqli::mysqli(): Error while reading greeting packet. PID=15852 in C:\xampp\htdocs\newservertest\dbscripts\sqlconnection.php on line 5
Warning: mysqli::mysqli(): (HY000/2006): MySQL server has gone away in C:\xampp\htdocs\newservertest\dbscripts\sqlconnection.php on line 5
Catchable fatal error: Object of class mysqli could not be converted to string in C:\xampp\htdocs\newservertest\dbscripts\sqlconnection.php on line 14
I know by default mySql only allows local connections but following tutorials online they are all saying to uncomment a line in a "my.ini" file. All i have in my sql folder is a my-default.ini with the following :
How can i open this up to connections from an external php script?
Try adding this to your php.ini:
mysqli.reconnect = 1

Categories