Mongo DB installation on RHEL 5.11 x64 - mongodb

I am trying to install MongoDB 3.0.0 on one of the sandbox which has RHEL Linux 5.11 64bit
I was able to download all the packages using yum command mentioned in the official document here MongoDB Installation.
I am trying to run following command as per the documentation
semanage port -a -t mongod_port_t -p tcp 27017
and it is giving me below error.
/usr/sbin/semanage port -a -t mongod_port_t -p tcp 27017
libsepol.context_from_record: type mongod_port_t is not defined
libsepol.context_from_record: could not create context structure
libsepol.port_from_record: could not create port structure for range 27017:27017 (tcp)
libsepol.sepol_port_modify: could not load port range 27017 - 27017 (tcp)
libsemanage.dbase_policydb_modify: could not modify record value
libsemanage.semanage_base_merge_components: could not merge local modifications into policy
/usr/sbin/semanage: Could not add port tcp/27017
I have root access on this sandbox but I am really clueless what this means. How can I install MongoDB successfully?

Step 1: Configure the package management system(yum)
Sudo vi /etc/yum.repos.d/mongodb-org-3.4.
so that you can install MongoDB directly, using yum.
Use the following repository file:
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-
org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
Step 2: Use the below command to install MongoDB
Sudo yum install -y mongodb-org
Step 3:Start Mongod Service
Sudo service mongod start
Step 4: Then start Mongo with the command below
Mongo
Now Create your database as use
And then start building collections and insert documents into in the collection
For More detail vist https://docs.mongodb.com/master/tutorial/install-mongodb-on-red-hat/

By default this port is already authorized:
#### # semanage port -l | grep mongod_port_t
mongod_port_t tcp **27017**-27019, 28017-28019

Related

i'm getting following error while connecting to mongodb server

$ mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
2018-10-18T17:09:03.003+0530 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2018-10-18T17:09:03.004+0530 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:251:13
#(connect):1:6
exception: connect failed
what OS are you using? In ubuntu you need to check your version:
service mongod status, mongod --version.
If mongod is off then just restart and write the error if possible.
service mongod restart
if you are using linux, try this command
$ sudo mongod
you are getting that because you are running
$ mongo
First go to your mongoDB directory and in that directory go to bin directory and open terminal in that directory(bin), then run the following command:
mongod.exe
After that open another terminal in same directory and run the following command.
mongo.exe
It will resolve this issue.
first you check it mongodb proper installed or not and also check it version compatibility
I am saying windows os setup,
set environment variable is path C:\Program Files\MongoDB\Server\3.6\bin;
then open your command prompt and type
mongod
after open another command prompt and type
mongo
A connection refused failure to connect reason may just be a matter of your network configuration and firewall or more specifically, your operating system's port restrictions. While you may be connecting to a mongoDB server on localhost (127.0.0.1) which can't be blocked by a firewall, it is possible for a firewall to block access to specific ports on a localhost.
If you're on a linux-based system, this can be addressed by using iptables as described here with the applicable commands shown below:
iptables -A INPUT -s 127.0.0.1 -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d 127.0.0.1 -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
However, in reading the comments on another answer, it may be that you aren't able to connect because the server isn't being launched and ran properly due to a lack of of the /data/db directory. But, a presumption based on the lack of reply to other answers suggesting to check your server is running properly, tells me you're sure that it is. In which case, try changing your hostname to localhost instead of 127.0.0.1 when connecting, as in:
mongoose.connect('mongodb://localhost:27017');
# or as more applicable for connecting to a deployment:
mongosh "mongodb://localhost:27017"
# alternatively, by default, MongoDB Shell already connects to localhost
# so you could simply use the command line option --port such as:
mongosh --port 27017
You can also try specifying more parameters in the url as it applies.
For example, connecting to an instance and authenticate against the foobar database as user rohan:
mongosh "mongodb://localhost:27017" --username rohan --authenticationDatabase foobar
To provide a password with the command instead of using the masked prompt, simply add the --password command line option.
For Windows, you may also try the solutions offered on this page.

MongoDB in Linux Mint 19

I am trying to use mongoDB in Linux mint 19. But I am getting below error.
What is the solution ?
You're starting the mongo shell client, which by default tries to connect to a mongod server running on localhost and listening on port 27017. However, there is no server listening there so the mongo shell notes that and exits.
You'll need to investigate why there was no mongod server listening there:
The MongoDB Server package isn't installed
You're specifying a non-default port in e.g. /etc/mongod.conf
The mongod process/service is not running
...
First verify that the Server package is installed:
sudo dpkg -l | grep -i ".*mongodb.*server"
Then check the status of the mongod service and go from there. That will tell you if it's running, what port it's listening on, what config file is used, etc.
sudo systemctl status mongod
If it's not running then try to start it:
sudo systemctl start mongod
If it's still not running then look at the log messages:
sudo journalctl -u mongod.service

Mongo Shell couldn't connect to the local server

I'm starting MongoDB using the following command:
sumeet#sumeet-acer:~$ sudo service mongod start
I get a reply as:
mongod start/running, process 7209
sumeet#sumeet-acer:~$
But when I try to enter MongoDB shell by typing mongo I get the following error:
sumeet#sumeet-acer:~$ mongo
MongoDB shell version: 2.0.4
connecting to: test
Fri Jun 12 14:01:59 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
By default, mongod and mongos use the 27017 port. Without any option, the client will try to connect to a server running on localhost and listening at that port.
A few general advices here:
check if the mongod server is really running (ps -edf | grep mongod);
check if it uses the default port (look in the config file1, or use nmap localhost -p0-65535 or netstat | grep 27017);
if mongod is not running, check in the log2 for clues about what goes wrong.
1 /etc/mongodb.conf (on Debian-like systems at least)
2 /var/log/mongodb (on Debian-like systems at least)
The default IP and Port for MongoDB is 127.0.0.1 & 27017.
If you are using config file for specifying settings than re-verify IP & Port mentioned in it.
If you are going with default option than mongod should function as expected.
try mongo 127.0.0.1:27017
this worked for me
For MongoDB 2.2.2 running on Ubuntu 12.10, it's in /var/lib/mongodb/mongod.lock
and after that running the repair command
sudo -u mongodb mongod --repair --dbpath /var/lib/mongodb/
courtesy: https://stackoverflow.com/a/13700262/4907105

Cannot connect to mongodb container from linux host

I have the following Dockerfile that only installs mongodb 3
# Start with docker's base ubuntu image
FROM ubuntu:14.04.2
# Mongodb prerequisite
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
# Update package sources
RUN apt-get -y update
# Install Mongodb binaries
RUN apt-get -y install mongodb-org
# Copy configuratio file
COPY mongod.conf /etc/mongod.conf
# Create mongo data path (will be mapped to a volume on host machine later on)
RUN mkdir -p /data/db
RUN chown -R mongodb:mongodb /data/db
# Expose MongoDB port
EXPOSE 27017
# Run mongo with mongodb user
USER mongodb
# Run mongod using provided configuration file
ENTRYPOINT ["/usr/bin/mongod"]
CMD ["--config", "/etc/mongod.conf"]
I create the image with
sudo docker build -t mongod .
I run the container with
sudo docker run -d -P mongod
And verify it's started
> sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43c75e7e44b3 mongod:latest "/usr/bin/mongod --c 3 minutes ago Up 3 minutes 0.0.0.0:49165->27017/tcp silly_mccarthy
When I run a mongo client from the container, it can connect without any error:
> sudo docker exec -ti 43c75e7e44b3 bash
$ mongo
MongoDB shell version: 3.0.1
connecting to: test
Welcome to the MongoDB shell.
....
My host is a Ubuntu 14.04 box and the network interfaces / bridges are
docker0 Link encap:Ethernet HWaddr 63:54:7b:f3:47:33
inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0
....
em1 Link encap:Ethernet HWaddr c4:2f:e3:64:ae:7c
inet addr:192.168.1.101 Bcast:192.168.1.255 Mask:255.255.255.0
....
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
....
From my host, I cannot connect to the mongod container
> mongo --port 49165
MongoDB shell version: 3.0.1
connecting to: 127.0.0.1:49165/test
2015-03-21T18:14:55.936+0100 I NETWORK Socket recv() errno:104 Connection reset by peer 127.0.0.1:49165
2015-03-21T18:14:55.936+0100 I NETWORK SocketException: remote: 127.0.0.1:49165 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:49165]
2015-03-21T18:14:55.936+0100 I NETWORK DBClientCursor::init call() failed
2015-03-21T18:14:55.937+0100 E QUERY Error: DBClientBase::findN: transport error: 127.0.0.1:49165 ns: admin.$cmd query: { whatsmyuri: 1 }
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
Is there something I'm missing here ?
MongoDB's default configuration is to bind mongod to the IP of localhost. Search for the following line in your mongod.conf:
bindIp = 127.0.0.1
If you comment this line out using a hash like this
# bindIp = 127.0.0.1
then mongod will bind to all IPs available during startup. If you set a specific IP like this
bindIp = 192.168.0.42
then mongod binds only to this specific IP address and will only be available to hosts which can access that IP on the specified port.

docker: show open ports from linked container

If I inspect the official mongo docker image, I can see that it exposes port 27017
$ docker inspect mongo
...
"ExposedPorts": {
"27017/tcp": {}
},
...
I have run the image, binding the internal port to the same on my host:
$ docker run -p 27017:27017 -d --name db mongo
I now run my own image in interactive mode, launching bash
$ docker run -i -t --link db:db_1 cd9b5953b633 /bin/bash
In my dockerized container, if I try to show open ports, nothing is listening.
$ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
What am I doing wrong here? How can I connect from my dockerized container to the mongo container?
If it is of some use, here is my Dockerfile:
# https://registry.hub.docker.com/u/dockerfile/nodejs/ (builds on ubuntu:14.04)
FROM dockerfile/nodejs
MAINTAINER My Name, me#email.com
ENV HOME /home/web
WORKDIR /home/web/site
RUN useradd web -d /home/web -s /bin/bash -m
RUN npm install -g grunt-cli
RUN npm install -g bower
RUN chown -R web:web /home/web
USER web
RUN git clone https://github.com/repo/site /home/web/site
RUN npm install
RUN bower install --config.interactive=false --allow-root
ENV NODE_ENV development
# Port 9000 for server
# Port 35729 for livereload
EXPOSE 9000 35729
CMD ["grunt"]
Docker create a Network namespace, so within your container, you will not see the exposed port of the host.
In your usecase, you do not need to run mongo with -p if you just need to access it from an other container. The --link will simply "inject" the linked container info as environement variable.
From your new container, you can do env to see the list, and you will have something like DB_1_PORT_27027_TCP_ADDR with the private IP of the mongo container where you can connect.