23

I had uploaded my website to server and then I tried to access, but the code igniter returns me that error that I don't find any answer. Why is this happening?

My config database is set like:

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'netservice',
'password' => '*********',
'database' => 'valedastrutas',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => FALSE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE,
);
peterh
  • 11,875
  • 18
  • 85
  • 108
William
  • 467
  • 1
  • 6
  • 19
  • Have you checked this question: http://stackoverflow.com/questions/4219970/warning-mysql-connect-2002-no-such-file-or-directory-trying-to-connect-vi – VolenD Apr 28 '15 at 19:33
  • Yes, and this one question haven't helped, because is a different problem. I'm trying to access de project in the website, and not in localhost – William Apr 28 '15 at 19:37
  • 1
    Did you try 127.0.0.1 instead of localhost? Because according to the error, it seems that socket connection is used instead of TCP connection and the socket is not found. – VolenD Apr 28 '15 at 19:40
  • Got now the mysqli::real_connect(): (HY000/2002): Connection refused – William Apr 28 '15 at 19:54
  • OK, try specifying the port number (`'port' => 3306`). If your host does not use 3306 (the default port), then use the one that they have given you instead. – VolenD Apr 28 '15 at 20:02
  • Was that, such idiot 'problem' hehe, thank you very much! – William Apr 28 '15 at 20:10
  • @William facing same issue, how did you solve it? – runningmark Sep 28 '15 at 18:15

11 Answers11

63

MySQL connection driver does not get any meaningful server at the location of localhost. So use,

'hostname' => '127.0.0.1' 

rather than

'hostname' => 'localhost'
Subarata Talukder
  • 5,407
  • 2
  • 34
  • 50
  • Why does this happen btw? – Pacerier Nov 12 '17 at 19:06
  • 4
    great! No one thinks about this. – huuthang Aug 17 '18 at 05:27
  • 1
    Localhost refers to a name or an identical name which points to an IP address 127.0.0.1. This IP(127.0.0.1) is supposed to exist on any computer capable of connecting to Internet. I mean has a LAN connection. Localhost refers to the local machine itself. Any IP address starting with 127.x.x.x refers to the local machine. Some compiler can't resolve localhost.yourdomain.com. Because "localhost" is not the same as localhost.yourdomain.com. So why it may cause a problem. – Subarata Talukder Aug 17 '18 at 08:13
  • 5
    This is happening because specifying 'localhost' will make the connection opened via the MySQL socket instead of TCP and the error is telling you that the socket file was not found. – Zoltan Fedor Oct 07 '19 at 16:51
18

I've got this error

mysqli::real_connect(): (HY000/2002): No such file or directory

from PhpMyAdmin running on my Mac Mojave, with MySQL server also running on my Mac.

Fixed it by editing PhpMyAdmin/config.inc.php and changed the line:

$cfg['Servers'][$i]['host'] = 'localhost';

to

$cfg['Servers'][$i]['host'] = '127.0.0.1';
Michal J Figurski
  • 1,262
  • 1
  • 11
  • 18
7

in phpmyadmin version 5.1.0 all lang., the config. file is located in libraries/config.default.php

do the following change:

$cfg['Servers'][$i]['host'] = 'localhost';

to

$cfg['Servers'][$i]['host'] = '127.0.0.1';
Dharman
  • 30,962
  • 25
  • 85
  • 135
Zaid Mohammed
  • 71
  • 1
  • 2
3

Same problem occurd with me, with AWS RDS MySQL. Looked various sources, but as of this thread, almost all lack of answer. While this answer helped me, tweak in mind to update hostnames at server. Access your SSH and follow the steps:

cd /etc/
sudo nano hosts

Now, Appending here your hostnames: For example:

127.0.0.1 localhost
127.0.0.1 subdomain.domain.com [if cname redirected to endpoints]
127.0.0.1 xxxxxxxx.xxxx.xxxxx.rds.amazonaws.com [Endpoints]

and now, configuring config/database.php as follows:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => '35.150.12.345',
    'username' => 'user-name-here',
    'password' => 'password-here',
    'database' => 'database-name-here',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

where 35.150.12.345 is your IPv4 Public IP, located at ec2-dashboard > Network Interfaces > Description : RDSNetworkInterface >> IPv4 Public IP('35.150.12.345') there.

Note: Please note that only IPV4 will work at 'hostname' option. hostname, cname, domains will output database connection error.

Community
  • 1
  • 1
Asharam Seervi
  • 77
  • 2
  • 15
  • I know this is old thread.. but wanted to understand.. ```127.0.0.1 xxxxxxxx.xxxx.xxxxx.rds.amazonaws.com [Endpoints] ``` this line. I think, this will make the RDS hostname point to localhost instead of RDS server.. Can you explain? – hriziya Aug 08 '20 at 04:19
2

In my case restarting mysql was needed as it had stopped running

ThurstonLevi
  • 664
  • 13
  • 34
2

If the error is due to the MySQL service being dead, you can restart it with this command:

sudo service mysql restart

If the error occured after restarting the server, then you may also need to enable MySQL at startup:

sudo systemctl enable mysql
Pikamander2
  • 7,332
  • 3
  • 48
  • 69
shubomb
  • 672
  • 7
  • 20
1

Also had this problem. The only solution that helped me - first to stop mysql server by the command "service mysql stop" and than run start lampp again: "sudo /opt/lampp/lampp start".

Roman Karagodin
  • 740
  • 2
  • 11
  • 16
0

I had this same problem. I was using a host defined in my /etc/hosts file:

127.0.0.1 test.localhost

and connecting with the url http://test.localhost/index.php/news/view as in the tutorial. I got that error, and only fixed it by changing the line:

'hostname' => 'localhost',

to:

'hostname' => 'test.localhost',

Phil Smith
  • 48
  • 4
0

I suggest you enable and look at the detailed php_errors.log

Check that the directory specified using session.save_path in php.ini exists and is writable. You can use phpinfo() to show the actual session.save_path.

Creating the missing "phptmp" directory specified as session.save_path fixed this issue for me.

relish
  • 31
  • 1
0

I had the same issue, in my case the site was up an running fine for a long time. Since lots of answers here concern a first time install and use I decided to tackle the same issue for systems that already function properly for a while then suddenly give this error.

First I think is important to understand the issue. As far as I know (and I may be wrong) this issue comes up if the driver cannot secure a connection to the database. This can be caused by many things, including the database being down for any issue or maintenance.

In my case the issue was caused as I accessed the website during a MySQL update (Software Updater was running in the background updating MySQL components). So I had to wait for the update to complete, then the website was again up and running with no issues.

So keep this aspect in mind, too.

Marius
  • 190
  • 1
  • 9
0

None of the other answers worked for me. I have MySQL 8.0.22 installed on my Mac OS 10.14.6, works fine from the command line. However, what I had done is chosen the stronger password encryption when I upgraded MySQL. This was the problem, not the localhost config. The config.inc.php can point to either 'localhost' or '127.0.0.1'. Once I got phpMyAdmin running, I confirmed this by using both.

The way to fix this (for me) was to click on Mac system preferences and choose MySQL. Click Initialize Database, and switch over to "Legacy password encryption". Restart and everything works fine. MySQL restarts, phpMyAdmin at http://localhost/phpmyadmin let me log in no problems.

I'm waiting to upgrade the Mac to OS 15 once I'm sure I won't brick the silly thing.

Dharman
  • 30,962
  • 25
  • 85
  • 135
noogrub
  • 872
  • 2
  • 12
  • 20