No suitable servers found (serverSelectionTryOnce set): [connection closed calling ismaster on 'mongo:27017']
#0 /usr/src/mybroker/vendor/mongodb/mongodb/src/functions.php(431): MongoDB\Driver\Manager->selectServer(Object(MongoDB\Driver\ReadPreference))
#1 /usr/src/mybroker/vendor/mongodb/mongodb/src/Collection.php(929): MongoDB\select_server(Object(MongoDB\Driver\Manager), Array)
#2 /usr/src/mybroker/src/Receiver.php(53): MongoDB\Collection->insertOne(Array)
#3 /usr/src/mybroker/src/receive.php(8): brokers\Receiver->listen()
#4 {main}\n
Два дня непонятная ошибка, я уже порты менял на 28017, 27018. На эту ошибку ответ один видел для MongoDB 3.4 версии, которая писали уже не поддерживается. Еще пробовал обратиться к контейнеру mongo curl
mongo:27017 (и 28017). Все выдает такую ошибку:
It looks like you are trying to access MongoDB over HTTP on the native driver port.
.env
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_USER=admin
POSTGRES_PASSWORD=admin
POSTGRES_DB=admindb
MONGODB_HOST=mongo
MONGODB_PORT=27017
MONGODB_USER=root
MONGODB_PASSWORD=root
MONGODB_DB=shopdb
ME_CONFIG_MONGODB_ENABLE_ADMIN=true
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=root
ME_CONFIG_MONGODB_ADMINUSERNAME=root
ME_CONFIG_MONGODB_ADMINPASSWORD=root
CLIENT_PORT_EXT=8008
CLIENT_PORT_INT=80
APACHE_PORT_EXT_X=800
APACHE_PORT_INT_X=8
RABBIT_QUEUE=message_for_mongo
RABBIT_HOST=rabbit
RABBIT_PORT=5672
RABBIT_PORT2=15672
RABBIT_USER=guest
RABBIT_PASSWORD=guest
REDIS_HOST=redis
REDIS_PORT=6379
docker-compose.yum
{
"version": "3.1",
"volumes": {
"postgres_data": { },
"nginx_data": { },
"mongo_data": { },
"rabbitmq_data": { }
},
"services": {
"mongo": {
"image": "mongo",
"hostname": "${MONGODB_HOST}",
"restart": "always",
"env_file": ".env",
"ports": [
"${MONGODB_PORT}:${MONGODB_PORT}"
],
"environment": {
"MONGO_INITDB_ROOT_USERNAME": "${MONGODB_USER}",
"MONGO_INITDB_ROOT_PASSWORD": "${MONGODB_PASSWORD}",
},
"volumes": [
"mongo_data:/data/db/"
]
},
"mongo-express": {
"image": "mongo-express",
"restart": "always",
"ports": [
"8081:8081"
],
"env_file": ".env",
"environment": {
"ME_CONFIG_MONGODB_AUTH_DATABASE": "${MONGODB_USER}",
"ME_CONFIG_MONGODB_ADMINUSERNAME": "${MONGODB_USER}",
"ME_CONFIG_MONGODB_ADMINPASSWORD": "${MONGODB_PASSWORD}",
"ME_CONFIG_MONGODB_ENABLE_ADMIN": "true"
},
"links": [
"mongo"
]
},
"shop": {
"build": {
"context": "./",
"dockerfile": "Dockerfile-client-api"
},
"ports": [
"${CLIENT_PORT_EXT}:${CLIENT_PORT_EXT}"
],
"env_file": ".env",
"depends_on": [
"mongo",
"rabbit"
]
},
"rabbit": {
"build": {
"context": "./",
"dockerfile": "Dockerfile-rabbit"
},
"env_file": ".env",
"hostname": "${RABBIT_HOST}",
"ports": [
"${RABBIT_PORT}:${RABBIT_PORT}",
"${RABBIT_PORT2}:${RABBIT_PORT2}"
],
"volumes": [
"rabbitmq_data:/var/lib/rabbitmq_data/data/"
],
"healthcheck": {
"test": "rabbitmq-diagnostics -q ping",
"interval": "5s",
"timeout": "15s",
"retries": "5"
}
},
"consumer": {
"build": {
"context": "./",
"dockerfile": "Dockerfile-consumer"
},
"ports": [
"${APACHE_PORT_EXT_X}0:${APACHE_PORT_INT_X}0"
],
"links": [
"rabbit",
"mongo"
],
"depends_on": [
"rabbit",
"mongo"
],
"healthcheck": {
"test": "curl -sS http://${RABBIT_HOST}:${RABBIT_PORT2} || exit 1",
"interval": "5s",
"timeout": "15s",
"retries": "5"
},
"env_file": ".env",
}
}
}
Dockerfile-consumer
FROM php:7.4-cli
RUN apt-get update -y \
&& apt-get install -y libcurl4-openssl-dev pkg-config libssl-dev \
&& pecl install mongodb \
&& docker-php-ext-enable mongodb
COPY ./broker /usr/src/mybroker
WORKDIR /usr/src/mybroker
CMD ["php", "./src/receive.php"]
Receiver.php
<?php
namespace brokers;
require_once __DIR__ . '/../vendor/autoload.php';
use MongoDB\Client;
use MongoDB\Driver\Manager;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exception\AMQPTimeoutException;
use RecursiveArrayIterator;
use RecursiveIteratorIterator;
//define('INSTANCE_NAME', getenv('INSTANCE_NAME'));
//define('AMQP_DEBUG', true);
class Receiver
{
public function listen()
{
$user = $_ENV['MONGODB_USER'];
$pwd = $_ENV['MONGODB_PASSWORD'];
$host = $_ENV['MONGODB_HOST'];
$port = $_ENV['MONGODB_PORT'];
$db = $_ENV['MONGODB_DB'];
$connect = "mongodb://${host}:${port}/";
try {
$collection = (new Client($connect, [
'username' => $user,
'password' => $pwd,
'ssl' => true,
'authSource' => $user,
],))->test->users;
$insertOneResult = $collection->insertOne([
'username' => 'admin',
'email' => 'admin1@example.com',
'name' => 'Admin User',
]);
printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
echo $insertOneResult->getInsertedId();
$manager = new Manager($connect);
$r = $manager->executeCommand($db, new \MongoDB\Driver\Command(['ping' => 1]));
echo $r->isDead();
// var_dump($manager);
//
// $collection = (new Client)->test->users;
//
// printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
// echo $insertOneResult->getInsertedId();
$queue = $_ENV['RABBIT_QUEUE'];
$connection = new AMQPStreamConnection(
$_ENV['RABBIT_HOST'],
$_ENV['RABBIT_PORT'],
$_ENV['RABBIT_USER'],
$_ENV['RABBIT_PASSWORD']
);
} catch (\Exception $e) {
echo $e->getMessage() . '\n';
echo $e->getLine() . '\n';
echo $e->getTraceAsString() . '\n';
die;
}
}