Could Not Connect: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I was trying myself too after posting the question here and I got it working through following the below steps. This work in mycase. You can try it too.
- Open Terminal In Ubuntu.
- Type
mysql -u root -p
- ENTER_YOUR_SQL_PASSWORD
- Type
> grant all privileges on DATABASE_NAME.* to USERNAME@IPADDRESS identified by 'PASSWORD';
- Or
- Type
> grant all privileges on DATABASE_NAME.* to USERNAME@localhost identified by 'PASSWORD';
- Change
USERNAME
,IPADDRESS
,PASSWORD
With Your Data. - Type
FLUSH PRIVILEGES;
- Now Restart SQL Typing
sudo service mysql start
- Close Terminal.
After this you can connect your database from outside to server too. If you have better contribution then share that also.
Note: Keep in kind that you have to changed bind-address = 127.0.0.1
to # bind-address = 127.0.0.1
to allow connection in /etc/mysql/my.cnf
also.
Related videos on Youtube
Muhammad Hassan
Updated on September 18, 2022Comments
-
Muhammad Hassan over 1 year
I am new to Ubuntu and using Ubuntu Server 14.40 LTS. I am trying to connect my mysql database to show database data on a php file but I am not able to connect it. I am getting error
Could Not Connect: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
when I open that php file and when I open my phpmyadmin then I got#2002 Cannot log in to the MySQL server
andConnection for controluser as defined in your configuration failed.
Just after installing Ubuntu, I opened my php page outside from server and then was not able to connect the database or even not getting any error. Then searching over Google, I changed
bind-address = 127.0.0.1
to# bind-address = 127.0.0.1
to allow connection in
/etc/mysql/my.cnf
but after this I got the above two error.My connection file is below...
<?php $host = "localhost"; $username = "my_username"; $password = "my_password"; $dbname = "my_database"; $connection = mysql_connect($host,$username,$password); if (!$connection) { die('Could Not Connect: ' . mysql_error()); } mysql_select_db($dbname) or die(mysql_error()); mysql_query("SET NAMES utf8"); ?>
In addition, my
port 80
is blocked my ISP and I am usingport 7550
for Apache. I also have a dynamic IP. Please consider these limitation and tell me How to connect to mysql server? Please answer using terminal.-
Muhammad Hassan almost 10 years@Benoit My
mysql
is online and working...
-