0

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 and Connection 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 using port 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.

  • You checked that your MySQL server was really running, don't you ? (status mysql). – Benoit Jul 10 '14 at 7:07
  • @Benoit My mysql is online and working... – Muhammad Hassan Jul 10 '14 at 15:09
0

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.

  1. Open Terminal In Ubuntu.
  2. Type mysql -u root -p
  3. ENTER_YOUR_SQL_PASSWORD
  4. Type > grant all privileges on DATABASE_NAME.* to USERNAME@IPADDRESS identified by 'PASSWORD';
  5. Or
  6. Type > grant all privileges on DATABASE_NAME.* to USERNAME@localhost identified by 'PASSWORD';
  7. Change USERNAME, IPADDRESS, PASSWORD With Your Data.
  8. Type FLUSH PRIVILEGES;
  9. Now Restart SQL Typing sudo service mysql start
  10. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.