简体   繁体   中英

REDIS php extension connect error

When I use redis PHP extension, I get the error, the host is 127.0.0.1 and the port is 6379.

Redis::connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known.

I had the same issue happening sporadically on my local server with a number of attempted fixes yielding no resolution. This post here gave me an idea that seems to have finally fixed the problem.

Basically, I have a number of vhosts set up on my local machine (running Ubuntu) which allows me to use domain names to access locally-run sites instead of the usual 'localhost/sitename' approach. As a result, I had no IP assigned to 'localhost' name.

This might not be the same root cause for the problem for you, but here's how I've solved mine:

  1. See what is the value of 'redis-host' in your redis config file (for me it's set to 'localhost').
  2. In your /etc/hosts file check that there's an IP assigned to that host name. (In your case, where redis-host is 127.0.0.1, check that there isn't some other host name assigned to that IP, like phpmyadmin, for example.)

Of course, as a sanity check, make sure your redis-server is up and running, try rebooting Apache in case you've made any config changes, etc.

In my case, the issue was simple; the host name was incorrectly typed.

To find out the exact issue, I had to do the following in my console:

ping <host-name>

or if you have redis-cli installed, you can just call that from the console as such:

redis-cli -h <host-name> -p <port-number> -a <auth-pass>

Source

For mac users:

Open terminal and try this command:

  1. sudo vi /etc/hosts
    (sudo to execute the command as a root user, You have enter the password for this)
  2. press 'i' to get into input mode of VI editor
  3. Add this line at the end 127.0.0.1 redis
  4. press esc to go to command mode
  5. type :wq (to save and quit the vi
    editor)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM