Linux安裝好MongoDB本機執行(mongo)都OK,IPv6也有關閉,但用PHP去連都回:
No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling ismaster on '127.0.0.1:27017']
試了google到的N種方法都不行,一度放棄,隔了二個月有新案子剛好再挖出來試,也是很挫折地摸了三天,最後看到 Selinux安全性 httpd_can_network_connect 要打開,姑且一試,BINGO !!
PHP code 如下:
$client = new MongoDB\Client ( "mongodb://127.0.0.1:27017" );
$collection = $client->mydb->test;
$result = $collection->find ( [ ] );
檢查目前的設定:
# getsebool -a | grep httpd_can_network_connect
httpd_can_network_connect --> off
設定成on:
# setsebool -P httpd_can_network_connect 1
再次檢查:
# getsebool -a | grep httpd_can_network_connect
httpd_can_network_connect --> on
還有一直重啟MongoDB有時服務會起不來,這跟user (mongod)權限有關,
# systemctl restart mongod
Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.
# systemctl status mongod.service
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Wed 2019-12-18 17:02:18 CST; 20s ago
Docs: https://docs.mongodb.org/manual
Process: 2293 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)
執行以下就能成功啟動
# chown -R mongod:mongod /var/lib/mongo
# chown -R mongod:mongod /var/log/mongodb
# chown mongod:mongod /tmp/mongodb-27017.sock
有時刪除以下這個檔案也能正常重啟MongoDB
# rm /tmp/mongodb-27017.sock
沒有留言:
張貼留言