Trouble Connection to phpMyAdmin MySQL database - php

I'm pretty new at php and I'm getting an error whenever I try to load a php page that requires access to the database I set up on phpMyAdmin
Here is the error:
Database connection failed: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (46) (2002)
The code I'm using for the connection is this:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
I set up phpMyAdmin on my server by following the directions from the site that hosts my website, so I'm pretty sure I did that correctly.
I've been able to get databases to work before on my computer by using WAMP, but this is the first time I've actually tried getting everything working online, so I don't know if this is a stupid error on my part, or if it's something bigger.
I don't know what a socket is either so I'm not sure how to troubleshoot this.
Thanks for your help!!

You have to find a way to connect to your MySQL instance. There are two ways, TCP (networking, even if that network address is 127.0.0.1) and sockets (a file such as /var/run/mysqld/mysql.sock). Generally, MySQL is configured out of the box to listen on both, and again in general there's little difference between the two. You have to be careful when creating or editing permissions that they match the connection type, user bob#localhost is different from user bob#127.0.0.1. I tend to use sockets and that's also what your script is trying to do by default. You could tell it to connect via TCP, but it's just as easy to tell it the proper path to the socket.
Anyway, for me the quickest way to figure it out is to try the command line tool. If you can do mysql -u root -p at the command line and get a MYSQL> shell prompt, type in "STATUS" and look for the Connection line, which might read Localhost via UNIX socket; and a bit further down you might see a line like "UNIX socket: /var/run/mysqld/mysql.sock" in which case you just tell your PHP script or global PHP configuration about the socket path, because right now it's looking in /tmp/mysql.sock which doesn't exist.
You can also see this when you log in to phpMyAdmin, it should display on the right hand side of the page some "Database Server" information -- look here for the "Server" line which might read something like "phpMyAdmin demo - MySQL (192.168.30.23 via TCP/IP" (a clear indication you're connecting over TCP).
Anyway, whichever method you use to find the path to the socket or deciding to use TCP networking, see the PHP manual page for information about using that to connect. You can also just set the global path in php.ini for the socket (which is what I'd suggest you do), then you don't have to set it manually in each script as it just uses the php.ini setting as a default. You may need to set each of the values for pdo_mysql.default_socket, mysql.default_socket, and mysqli.default_socket (though in your code you're only using mysqli, so technically you only need to set the last value...but why risk confusing your future self with questions like "why would mysqli work but pdo fail?" -- just set them all now and don't worry about it).
Hope this helps.

That either means you don't have the privileges to access phpMyAdmin or that socket isn't there. Try reinstalling phpMyAdmin.

Related

Error connecting a remote server from a PHP page

I have a .php file which should receive and show data from a remote database. I run my program from PHPStorm (which is connected to the remote database through the "Database" right-hand pane) and a browser. Both ways I get an error which depends on the number of arguments I pass to pg_connect() function.
If I use
$dbconn = pg_connect("host=pg hostaddr=server.address.ru port=5432 dbname=studs user=... password=...")
than the error is
Unable to connect to PostgreSQL server: could not parse network address "server.address.ru": Unknown host in...
But I am sure that I wrote the address correctly (there are no typos in it). This way I am not sure about the correctness of the format of the passed arguments.
If I use
$dbconn = pg_connect("host=server.address.ru dbname=studs user=... password=...")
command, the error is
pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection timed out
I found a lot of information about this errors, but it mostly refers to localhosts and doesn't solve my problem. I guess, the problem can be in the way this connection is set in the function, but I do not know why it doesn't work properly. How can I solve it?
Thanks to #TangentiallyPerpendicular, I got on a right way of setting the connection. But since I have PostgreSQL remote connection, it wasn't just up to this answer.
What I did and how I set the connection (I work from PHPStorm so all the actions are based on this platform):
Open PuTTY and set an SSH connection (an SSH tunnel) between the server's DB port (for PostgreSQL it's usually 5432) and your local computer's PostgreSQL port (5432 most often too). You can do the same from a command line.
Open PHPStorm and in "Database" section (an icon on the right-hand side of the environment or "Data Sources and Drivers" in Settings) set general information ("General" section) and set another SSH tunnel ("SSH/SSL"). In SSH Configurations (the same "SSH/SSL" section) set a local port - it will be important in our connection! Let's say, this port is 20000. The port of the server you're connecting to is a usual one (usually 22 or 2222).
In the program the right use of function is $dbconn = pg_connect("host=localhost port=20000 dbname=studs user=... password=...") or die('Error: ' . pg_last_error());
The connection is set.
For those who has troubles setting an SSH tunnel with a remote PostgreSQL from PHP this can be useful too.

How to connect a PHP app to MySQL via pipes?

I have found glimpses of the fact that it is possible to connect PHP to MySQL via pipes in their documentation of MySQLi, but I cannot, for the life of me, find anyone explaining what is needed.
The host parameter claims:
When possible, pipes will be used instead of the TCP/IP protocol.
But when is it "possible"? I have my own machine, and I definitely have the necessary privileges to achieve this, I just don't know how. Connecting to the host localhost reports "Localhost via UNIX socket" when examining the host_info.
Trying to follow one (downvoted) comment from that page, and connecting to host ., with socket parameter set to mysql, causes a 2002 connection error.
How do I tell it to (always) connect via a pipe instead?
Today I had the same issue and it required much time to solve this on Windows.
I can establish a named pipe connection with the following code:
$connection = new mysqli('.', 'user', 'pass', 'database', null, '\\\\.\\pipe\\name_of_pipe');
The server, where I want to connect to, has the following configuration:
[mysqld]
skip-networking
enable-named-pipe
socket=name_of_pipe
Using '127.0.0.1', 'localhost' or NULL as hostname doesn't work. Also you must specify a path to the named pipe and not just the name of the pipe.
Unfortunately the PHP documentation is a little bit weak here...
Named Pipes only work under Windows.
Also
Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as in your PHP configuration and leave the server field blank.
It is not mentioned in the actual PHP documentation, but it should be still valid.

Can't Connect To MySQL (Openshift)

I can't connect to MySQL on Openshift. There is nothing wrong in the parameter, but it doesn't want to connect.
$link = mysqli_connect("127.0.0.1","user","password","database") or die("Error" . mysqli_error($link));
What am I missing?
Your question seems is a bit a bit vague, but assuming you have a Openshift PHP application and want to connect to a Openshift MySQL cartridge, then you should not use hard coded IP addresses. Instead there is a whole range of environment variables which define the required properties, e.g. OPENSHIFT_MYSQL_DB_HOST. You need to use these variables. Using PHP you can read an environment variable like this:
$database_host = getenv('OPENSHIFT_MYSQL_DB_HOST');
You should use the environment variables to connect to your database, you can read more about them here (https://developers.openshift.com/en/databases-mysql.html), also, what is the error that is being displayed when your connection does not work?
I was having this issue when I was working with Angular JS earlier. When I would try to connect to the port 8080, I would get a connection error. After looking around online, I used port 3306 and that connected just fine.
I would recommend specifying a port to connect to. You can try the below line of code to see if it works.
mysqli_connect("127.0.0.1","user","password","database", 3306)

Cannot connect to MySQL database, socket error

I'm new to php, and while trying to make a connection on one of my pages to the database I set up on the phpMyAdmin page of my site.
I get this error:
"Database connection failed: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (46) (2002)"
I don't know what a socket is, or why it's trying to go to what looks like a temp file, so I don't even know where to being to troubleshoot this.
The code I'm using to make the initial connection is this:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
I know I should have access to the MySQL databases, since I logged on and made them myself.
I previously set the page up by using WAMP on my computer, and everything worked fine.
It's just when I tried making it live on the site that I ran into this error.
Any help would be awesome!!
Try using 127.0.0.1 instead of localhost.
If that does not solve it, and you have root access to your server, try the following command
service mysql restart
To restart the mysql server.
The first option will probably work. Again, if you have root access to your server, you should change the mysql config to support sockets, since it's better than the TCP-ip connection.
After contacting my hosting service several times, it seems the error occurred because my hosting service recently changed their specifications and now uses "mysql" instead of "localhost" in the host and server fields. I had used "localhost" before with another host service, so I didn't think to change this, and the latest help articles on my host's website had not updated to reflect this.

PHP, MySQL, Lost connection to MySQL server during query, TurnKey Ubuntu/Linux,

Hi to all php and mysql experts,
I try to find some help or 'tutorial' or similar question/answer for my problem but it was unsuccessful.
I have install TurnKey Linux, php, mysql, successfuly width ip address: 172.##.##.## and I can connect to this server from another comp.
I was edit my my.cnf file, I was open port 3306 and I can connect via mysql -u root -p -h 172.##.##.## from another comp. I was add user:root that can connect from any host or IP address. Also I can make successful connections width mysqladmin or mysql workbench.
I can, also, make database and some tables on this server like db_test, tab_test etc. and if I use next script on this server its work very good:
<?php
$db_hostname="localhost";
$db_username="root";
$db_password="pxxxxx";
$db_connect=mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_connect) {
die('Could not connect: ' . mysql_error());
}
mysql_close($db_connect);
?>
But if I use next script it does not work and report to me error: Could not connect: Lost connection to MySQL server during query;
<?php
$db_hostname="172.##.##.##";
$db_username="root";
$db_password="pxxxxx";
$db_connect=mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_connect) {
die('Could not connect: ' . mysql_error());
}
mysql_close($db_connect);
?>
Also this script it does not work if I use another php server witch can see or ping this server.
Can somebody help me to resolve this problem! Please. I am desperate.
I have the same error, please go to xampp\phpMyAdmin\libraries\config.default.php
Modify the following code
existing one - $cfg['ExecTimeLimit'] = 300;
modify to - $cfg['ExecTimeLimit'] = 600;
This is getting a bit long for a comment....
It doesn't appear to be a connection error - that's a different error message.
Changing the configuration of PHPMyAdmin is not going to make any difference to other scripts.
It might help if you publish details of the mysql config from PHP. Also is your mysqld started with skip-name-resolve? How long does your example script take to fail?
One thing to watch for is that when the mysql client sees 'localhost' in the conection string, it tries to connect via a filesystem socket - not a network socket - do you get the same error when you supply the host as 172.##.##.## ?

Categories