简体   繁体   中英

mysqlnd php_network_getaddresses: getaddrinfo failed: No such host is known

I followed this tutorial http://www.clusterdb.com/mysql-cluster/creating-a-simple-cluster-on-a-single-linux-host using this download from mysql site: mysql-cluster-gpl-7.3.10-linux-glibc2.5-x86_64.tar.gz

I have this up and running on a Ubuntu 14.04LTS box, meaning I'm able to login to mysql-cluster using the terminal on each port to confirm that it works well. Just not managing to connect from PHP test script to mysql-cluster because of the above PHP error when I try to connect to it using /etc/mysqlnd_ms_cfg.ini. Spent a whole day searching on the internet without solving this matter, so any help is greatly appreciated.

Running www.test.loc/test.php generates the error:

$pdo = new PDO("mysql:host=myapp;dbname=test", "root", "");
printf("[%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
$result = $pdo->query("select * from assets");
var_dump($result->fetchAll());
die();

Whereas this does work www.test.loc/test.php:

$pdo = new PDO("mysql:unix_socket=/tmp/my.1.sock;dbname=test", "root", "");
printf("[%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
$result = $pdo->query("select * from assets");
var_dump($result->fetchAll());
die(); 

Which confirms that connecting to one of the nodes does work as it return the row result. Just that this way we are not using the mysqlnd_ms_cfg.ini which would load balance to different master in a round robin manner.

If I change the "myapp" key to something else in /etc/mysqlnd_ms_cfg.ini than I got also another error saying that the file isn't found by php which would imply that the path the the file is correct. Also validated the json format for typos but according to https://jsonformatter.curiousconcept.com/ it's valid.

Setting: /etc/mysqlnd_ms_cfg.ini

{
"myapp": {
"master": {
  "master_1": {
    "host": "localhost",
    "db": "test",
    "user": "root",
    "password": "",
"port": "3306",
"socket": "\/tmp\/my.1.sock"     
  },
  "master_2": {
    "host": "127.0.0.1",
    "db": "test",
    "user": "root",
    "password": "",
"port": "3307"          
  },
  "master_3": {
    "host": "127.0.0.1",
    "db": "test",
    "user": "root",
    "password": "", 
"port": "3308"         
  }
},
"slave": {
},
"filters": {
  "roundrobin": []
},
"failover": {
  "strategy": "loop_before_master",
  "remember_failed": true
}
}
}

Installed php5-mysqlnd:

sudo apt-get install php5-mysqlnd

Added the settings to 10-mysqlnd.ini and rebooted apache2:

; configuration for php MySQL module
; priority=10
extension=mysqlnd.so
mysqlnd_ms.enable=1
mysqlnd_ms.force_config_usage=1
mysqlnd_ms.config_file=/etc/mysqlnd_ms_cfg.ini
;mysqlnd_ms.ini_file=/etc/mysqlnd_ms_cfg.ini

;Disabling built-in read-write splitting.
mysqlnd_ms.disable_rw_split=1

;Configure masters only.
;mysqlnd_ms.multi_master=1

PHPINFO output: Checked phpinfo() and all section about mysqlnd are there meaning:

  • Additional .ini files parsed: /etc/php5/apache2/conf.d/10-mysqlnd.ini
  • Client API version: mysqlnd 5.0.11-dev - 20120503
  • Whole block for mysqlnd: Version mysqlnd 5.0.11-dev - 20120503
  • Loaded plugins: mysqlnd

Content my hosts file /etc/hosts:

127.0.0.1   localhost
127.0.1.1   dev-pc
127.0.0.1   www.test.loc

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

There's a lot of irrelevant information in your question.

This is nothing to with with clustering.

It's nothing to do with PHP.

It's nothing to do with MySQL.

The important bits are:

new PDO("mysql:host=myapp;dbname=test", "root", "");

and

$pdo = new PDO("mysql:unix_socket=/tmp/my.1.sock;dbname=test", "root", "");

and

mysqlnd php_network_getaddresses: getaddrinfo failed: No such host is known

and

127.0.0.1   localhost
127.0.1.1   dev-pc
127.0.0.1   www.test.loc

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

There's no entry in your hosts file for "myapp" (hostname used in first connect string above). Change this to 127.0.0.1 or one of the aliases you have defined other than localhost, and it should work as you expect (assuming this isn't just covering up further config issues elsewhere).

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