How to resolve mysql connection error? - php

I have an application designed in mysql and php. Earlier this app was working fine. But now it is not. Its giving me an error :
***Failed to connect to MySQL: Can't connect to local MySQL server through socket
'/var/lib/mysql/mysql.sock' (2)***
How can I resolve this error ?
The application is running on a redhat machine.

Does the directory "/var/lib/mysql" exist? also please check your mysql user (or better, mysql group) have writer permission. If so, please try re-starting mysql and see what happens

You must follow the below steps to debug the error:-
start mysql engine.
check below database connecting variable
a. host name
b. username
c. password
d. database name
now check query for connecting with mysql.
now check whether it's able to connect with database or not.
I think this will help You to resolve the problem. If it persists then let me know.

Reference link you may find the solution.According to the link it says:
Are you connecting to "localhost" or "127.0.0.1" ? I noticed that when you connect to "localhost" the socket connector is used, but when you connect to "127.0.0.1" the TCP/IP connector is used. You could try using "127.0.0.1" if the socket connector is not enabled/working.

i think your mysql server is not running. Make sure your mysql server is running
use
# mysqladmin -u root -p status
to check status
if it is running , try to connect from command line. And execute some simple select statement from command line. If your are able to do that, make sure that username and password (used in command line) are used in your application too.
If your are application working sometime and sometime not, in that case make sure you are closing connections in your program after use.

Related

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.

How to connect MySQL workbench to database

I'm trying to use mysql workbench to get access the database in my office. My friend has already helped me setup the connection in PuTTY, so I can write my MySQL script in that "black and white" window. However when I try to use MySQL workbench to get connection to the database, it causes error. It says:
Failed to Connect to MySQL at 127.0.0.1:3306 with user martin (I tried root as well)
Can't connect to MySQL server on '127.0.0.1' (10061)
I know there are a lot of questions and answers about this on stackoverflow. I tried some of them but without any luck.
I'm thinking do I need something called mysqld.exe in a certain directory? I have downloaded the file but when I double click it nothing happens. Do I need to run that as an administrator? One of my friends told me I don't need that at all. So what should I do now?
Any clue please enlighten me. Thanks heaps!!
To connect to your MySQL database with Workbench software you should do the following:
Open your Workbench application >> Database >> Connect to database. Setup window will show up, please fill out the fields as follows:
SSH Section
Connection Method: Standard TCP/IP over SSH
SSH Hostname: OFFICE-SERVER-IP
SSH Username: SSH-USERNAME-HERE
SSH Password: SSH-PASSWORD-HERE
Mysql Section
MySQL Hostname: 127.0.0.1
MySQL Server Port: 3306
Username: MYSQL-USERNAME-HERE
Password: MYSQL-PASSWORD-HERE
Just For Ilustration purposes your configuration should look something like this:
Seems like you are trying to access the Mysql server running on your local machine in your example. The MySQL at 127.0.0.1 means connect to a mysql server running on the local machine.
If you are on the same network as the server and your Mysql server is set up to allow access from a second machine you need to enter the ip address of the machine that the Mysql server is running on for instance 192.168.1.210:3360 means connect to the machine at Ip address 192.168.1.210 using the default Mysql Port of 3360. Your Putty setup should have the ip address of the machine you need to connect to.

MySql remote connection from php

I have allowed remote connections to mysql. I can successully connect from the console of another machine.
I have some php files on another server but I cant connect to the same mysql db from php.
This is the way I'm doing it:
mysql_pconnect("theipaddress","username","password")
or die("Unable to connect to db server");
Now bear in mind I am using the same creds which I successfully used in the console. I even tried putting the port after the ip but no joy.
Any ideas?
You must have a user in MySQL who is allowed to connect from % (any host) (see manual for details).
Have you selinux installed? Maybe selinux is not allowing apache to make remote connections? If yes, type:
setsebool -P httpd_can_network_connect=1

Logging with a user in a remote MySQL database

How to log with a diferent user in a MYSQL remote database?
Here's what I've done:
Logged as root in the MYSQL:
'create user 'user'#'%' IDENTIFIED BY 'password';
'grant select on *.* to 'user'#'%';
Then I setted a PHP script which connection is this one:
$con = mysql_pconnect("xxx.xx.xxx.xxx","user","password");
$selected = mysql_select_db("database",$con);
Aaaand it isn't working:
I'm using LAMP on a cloud server, by the way;
Warning: mysql_pconnect() [function.mysql-pconnect]: Can't connect to MySQL server on 'xxx.xx.xxx.xx' (10061) in D:\path\index.php on line 21
What am I doing wrong?
EDIT: Not a firewell issue;
You might want to check the MySQL documentation on this specific problem. If I had to guess, I would say that your MySQL server may be bound only to the local (127.0.0.1) address. To troubleshoot you should probably try connecting to the server using the command line MySQL client in order to get a better idea of why exactly the connection isn't being made.
Seems to me like a firewall issue. You should check if the machine hosting the MySQL server allows connection on the port 3306 from external IPs as well.
Check if the server you have to connect to has the firewall open on the port you are trying to connect... default port is 3306
you can use
mysqladmin -h localhost
to see check what is the port mysql is using

mysql_connect doesn't work on a certain host

I get this everytime I use mysql_connect() no matter what database I choose:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'IP' (111) in filename.php on line 17
A MySQL error has occurred: Can't connect to MySQL server on 'IP' (111)
The exact same file works on my personal website fine. I have tried multiple databases hosted on different servers and it always gives that output.
The database itself is hosted on the same server, but using its full IP in mysql_connect(). Using localhost:port doesn't work either as it says:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in filename.php on line 17
A MySQL error has occurred: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
But using the IP should work as it has worked calling it via the same file hosted on other servers.
This is the code:
$connect = mysql_connect($db_url,$db_user,$db_pass); // connects
if ($connect == false) exit("A MySQL error has occurred: " . mysql_error());
Now since the file works on other servers i am guessing it is something to do with the server it is on and might need something changed. I don't personally have root access to the server (just my part of the shared host). Is there anything I can do i php, editing the php.ini file or something I should pass on to someone with root access?
Edit: Ok it turns out that the server doesn't have access to outside databases, so thats why the IP didn't work. Thanks for all your answers but we have decided simply to change hosting provider. We need to be able to access an outside database.
This is on a hosting service? Check their documentation, there will be something that tells you where to find mysql. It isn't necessarily localhost.
For example, on startlogic.com, you use: yourdomain.startlogicmysql.com
Can you connect using mysqladmin using the same host, username and password?
mysqladmin -h $db_url -u $db_user -p $db_pass
Replace $db_xxxx with real values.
If that works from the same host as your php script, then sudo to the apache User and try the same test. One of those must be failing.
EDIT: nevermind on sudo part, I noticed that you don't have root access.
Something else to try: Use '127.0.0.1' instead of 'localhost'. I have had issues before where mysql stupidly assumed it could silently change 'localhost:' to '/var/run/mysqld/mysqld.sock'.
Your wording is not very clear, I hope you are not thinking you can connect to the same mysql server from any old web server just because you know the IP address and port number. If the web host is at all competent, they have probably firewalled mysql so it is only accessible through their own web servers.

Categories