Warning: mysql_connect(): Can't connect to local MySQL server - php

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (11) in /home/aa/public_html/bb/db.php on line 2
Could not connect:
iam getting this error but the problem is that this error comes irregularly,sometimes it didnt come all the day and sometimes it comes countless times.
what could be the possible reason for this?????

That could mean that MYSQL is down or you use the wrong host name while connect. One more possible reason for that could be difference in socket configuration of php against mysql, you can check it in by looking at entry socket in mysql config. file and by looking at output of phpinfo(), you need just to compare it. Or it could that someone else on your machine also using that socket.
PS. As well my wild guess, go through your code and check you always get your connection close right and all your queries as well.

Make sure that your MySQL is running.
OR
Create a file called: phpinfo.php with the following inside it:
<?php phpinfo(); ?>
Load the file in your browser: http://localhost/phpinfo.php and scroll down for mysql. Look for the MYSQL_SOCKET and make sure it matches your entry in your my.cnf file.
FYI, my my.cnf file is located in: /etc/my.cnf and it contains something like this:
# The MySQL server
[mysqld]
port = 3306
socket = /tmp/mysql.sock

Related

#2002 - No connection could be made because the target machine actively refused it. Wamp Server [duplicate]

This question already has answers here:
No connection could be made because the target machine actively refused it (PHP / WAMP)
(11 answers)
Closed 3 years ago.
I'm trying to connect Android with PHP, MySQL, for that I'm referring this tutorial. I installed WAMP server successfully on my local machine and now I want login to phpmyadmin by entering "http://localhost/phpmyadmin" url in my browser but I'm getting following error-
#2002 - No connection could be made because the target machine actively refused it. — The server is not responding (or the local server's socket is not correctly configured).
For more information, please see the below screenshot-
One more thing I would like to mention here is, after starting Wamp server it shows the icon in Orange colour. I googled n tried almost all available solutions but unable to resolve my issue yet. The strange thing is yesterday it was working successfully but in today morning when I tried to login again I'm getting this error. Please help me to resolve this issue also let me know if I can provide more information for the same. Thank you.
Case 1 :
One is mysql port not conflict with other application
Case 2 :
Second one is some mysql table We do not continue the crash recovery, because the table may become corrupt if we cannot apply the log records in the InnoDB log to it.
Case 1 : solution
You could go to wamp/bin/mysql/mysql5.x.x /
There can find my.config file you can change below mention port number what we change like that.
The following options will be passed to all MySQL clients
[client]
password = your_password
port = 3309
socket = /tmp/mysql.sock
and
The MySQL server
[wampmysqld]
As well as you could change php/bin/ folder
Php.ini here you could change port number default one 3306 to 3309 like this
port = 3309
socket = /tmp/mysql.sock
Case 2 Solution :
Wamp/bin/mysql/mysql5.x.xx/data
Here can find the data folder in that your project name appeared open that project and then find the currepted (uses space ID: 43 at filepath: .\myschool\tbl_student.ibd
InnoDB: Error: could not open single-table tablespace file .\myschool\tbl_student.ibd) file then delete it .
wamp\bin\mysql\mysql5.6.17\data\myschool
here you find the table struce then delete currepted table structure like this extension tbl_student.ibd
Logs file you can find wamp/logs folder here you can select MySQL then go down see what the error then you can resolve it .
Courtesy webTutorialsPoint.com and ApacheForum
have you configure control panel of your server ..
i had same problem then i found it was nothing wrong there
the problem was the other application was using the same port which control panel of my server was trying to access
hope you will find your error ..
good luck
That's Because mysqld is not running Check it mysqld On \MySQL\bin
if you are using wamp go to ~\wamp\apps\phpmyadmin{version} and open config.inc.php then add $cfg['Servers'][$i]['port'] = '????' bellow $cfg['Servers'][$i]['connect_type'] = 'tcp' in /* Server parameters */ section.
replace ???? with your current port (3306 is default port so if you change the port, write it here)

#1130 - Host 'localhost' is not allowed to connect to this MySQL server

When I try to access the PhpMyAdmin page, I had the error in the title above.
I tried all procedures found in the internet but unfortunately I didn't found the resolution.
Below the error that I have :
phpMyAdmin tried to connect to the MySQL server, and the server
rejected the connection. You should check the host, username and
password in your configuration and make sure that they correspond to
the information given by the administrator of the MySQL server.
Edit:
Ichecked the Mysql service and I found that is not running, I try to start it but it won't.
I restart the machine and the things return to normal !! but when I edit the My.ini file in order to clean all added entries and restarted Mysql I had the same error and when I added skip-grant-tables option under [mysqld] in My.ini file I had once again access to Mysql !!!
I guess there is an issue in users or privileges !?

Connecting to MYSQLi through Terminal in Mac OS using MAMP

I am trying to run my PHP script via Terminal in Mac. When I try to run the script, I am getting this error:
Warning: mysqli_connect(): (HY000/2002): No such file or directory
This is how I am trying to connect:
mysqli_connect('localhost','root','root','my_db');
I also tried to connect using the host: 127.0.0.1 instead of localhost and I then got this error:
Warning: mysqli_connect(): (HY000/2002): Connection refused
From what I've read online it might have something to do with mysql socket, but whatever I try to do to get this to work doesn't help.
I even tried to connect using the following as my host:
:/Applications/MAMP/tmp/mysql/mysql.sock
:/var/mysql/mysql.sock
Any idea what can be the problem/solution?
Had the same issue. Here's what I did.
Open Terminal (/Applications/Utilities/terminal.app)
Use your favorite text editor to create/edit ~/.bash_profile. For instance, if you're using vim, type vim ~/.bash_profile. I use TextMate, so I typed mate ~/.bash_profile.
Add these lines:
export MAMP_SQL=/Applications/MAMP/Library/bin
export MAMP_PHP=/Applications/MAMP/bin/php/php5.5.10/bin
export PATH="$MAMP_SQL:$MAMP_PHP:$PATH"
These lines ensure that the first versions of MySQL and PHP that are found (and therefore used) are the versions used by MAMP. NB: Make sure that php5.5.10 matches the version MAMP is using. You can check this against http://localhost:8888/MAMP/index.php?page=phpinfo&language=English by default. SAVE THE FILE (I shouldn't have to save this, but invariably someone complains that it doesn't work).
Close your terminal window and reopen. This restarts the shell, which will load your .bash_profile script.
Type which php. You should get something akin to /Applications/MAMP/bin/php/php5.5.10/bin/php
Type which mysql. You should get something akin to /Applications/MAMP/Library/bin/mysql
If for any reason you get something different than the responses from 5 and 6, go back and check your .bash_profile; chances are you just neglected to save it properly.
If you have MySQL installed on your computer, you can do the following command:
mysql -u "username"
replacing "username" with the username of your computer
NOTE: often times root does not work, try the username of your computer

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

everyone. I'm a student and very inexperience newbie with PHP . Before you mark this as irrelevant or duplicate or anything. Hear me out please, I need to connect to my school host to test out my codes, I have installed XAMPP, phpmyadmin 4.0.4.1 using notepad++ text editor, PHP5.4.19 and FTP WinSCP a program similar to filezilla
uploaded all the files to the server side public html and tried to run the file in browser
by using: my_school_link/my_username/
and I have this error
Warning: mysql_connect(): Can't connect to local MySQL server through
socket '/tmp/mysql.sock' (2) in /my_full_filepath..../connect.php on
line 3 and this is my connect.php file.
<?php
mysql_connect('host_name', 'user_name', 'password');
mysql_select_db('my_database_name');
?>
I cannot locate my /tmp/mysql.sock, * i have tmp folder in xampp folder but inside i cant find mysql.sock*
and I have tried the following
view nearly every post on this matter (i don't understand what is being said, thou i did try out some of the suggestions and answers)
have entered the correct hostname, username and password
run as admin
checked if mysql is running ( it is so is Apache)
MySQL socket specified in php.ini was never edited so it is in default mode
cant locate mysql config file, or my.cnf, or etc folder
What could the problem be? Please if anyone could help me, please tell me what's wrong.
Please provide a step-by-step guide. and file extensions if need to save files....
Is there any additional things i can add to help anyone understand my problem better?
if yes, please let me know...this is my last resort so Thank you so very much in advance for any help.
Warning: mysql_connect(): Can't connect to local MySQL server through
socket '/tmp/mysql.sock' (2) in /my_full_filepath..../connect.php on
line 3 and this is my connect.php file.
This error appear when your mysql server is not running. If you are on an shared hosting, it means that your provider has temporary stopped mysql server (some hacks needed to be solved, or doing updates etc.)
If you are on WAMP or local machine, you should check your /etc/my.cnf file it contains something like this:
# The MySQL server
[mysqld]
port = 3306
socket = /tmp/mysql.sock
Another reason may be that you are using the wrong host name while connect.
Don't use mysql_connect it is deprecated and instead you should use MySqli

create mysql database on mac os x

I know this sounds a little ridiculous, but I'm just honestly wondering how I can create a MySQL database on my Mac OS X machine. I have MySQL installed (as far as I know) as well as PHP, and I'm wondering how I can create one.
I have downloaded a sample PHP ecommerce site to kind of delve into what I'm trying to familiarze myself with, and the PHP files are attempting to connect to a nonexistant database, which I'm hoping to create right now.
Do they have an extension of .sock? Because, here is the "error" code when I try to test a PHP page referencing the nonexistant database:
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in< /Library/WebServer/Documents/plaincart/library/database.php on line 4 Warning: mysql_connect(): No such file or directory in
/Library/WebServer/Documents/plaincart/library/database.php on line 4 MySQL connect failed. No such file or directory
My guess is you are specifying the database server as localhost somewhere. Try 127.0.0.1 instead.
I had this problem once with WordPres on Mac OS X. The answer was to name the local machine in the db connection configuration 'Localhost' with capital 'L', because on OS X localhost != Localhost.
I had the same problem with MySQL on the OSX. It turned out that the MySQL server was actually set up to use a different socket than the default setting in PHP.
PHP was looking in /var/mysql/mysql.sock, while mysql was creating the socket in /tmp/mysql.sock.
I fixed it by editing the php.ini and set mysql.default_socket = /tmp/mysql.sock

Categories