Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect MySQL through localhost not working but 127.0.0.1 is working

OS: Ubuntu 14.04 LTS

php version control : phpbrew

php version : 5.5.10

I pinged localhost which resolved to 127.0.0.1.

This indicates that my host (/etc/hosts) file is correct.

127.0.0.1 localhost

Whenever I try connecting to MySQL using a php script like the one below it doesn't work and gives me the error: no such directory.

    //connect to the database
    mysql_connect("localhost","root","password") or die(mysql_error());

However, when I connect via 127.0.0.1 it will work

 //connect to the database
        mysql_connect("127.0.0.1","root","password") or die(mysql_error());

Additionally, my phpmyadmin does not work when logging in using "localhost" i had to change the file to add 127.0.0.1 option during log on.

How can I use localhost to connect to the MySQL database?

like image 439
JoeCodeCreations Avatar asked Jul 14 '15 17:07

JoeCodeCreations


1 Answers

PHP is still trying to use the default socket location. This problem can appear if you have moved the MariaDB/MySQL folder from /var/lib/mysql to another location. In order to solve the problem you have to define the new socket's location in the /etc/php.ini file.

mysqli.default_socket =/newDBLocation/mysql/mysql.sock

Watch out, depending on which driver you use, you may have to specify the pdo_mysql.default_socket=!

In order to check your current directory run the following command in mysql:

select @@datadir;
like image 141
joelschmid Avatar answered Sep 20 '22 01:09

joelschmid