How to access mongodb remotely in linux - mongodb

I have successfully installed mongodb into my linux machine.
Please suggest how to access it remotely.

I worked on it and searched for it. Finally came up with following solution.
Importing the Public Key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Creating a List File
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
sudo apt-get update
Installing and Verifying MongoDB
sudo apt-get install -y mongodb-org
service mongod status
Creating a user
use admin
db.createUser({user:"<username>", pwd:"<password>", roles:[{role:"root", db:"admin"}]})
Try to Login
mongo -u <username> -p <password> --authenticationDatabase admin
Set vi /etc/mongod.conf
# Comment bindIp with #
service mongod restart
Allow connections on port 27017, MongoDB default port
ufw allow 27017
ufw status
Enable mongodb authentication on vi /etc/mongod.conf
security:
authorization: enabled
To access mongodb remotely the command is:
mongo -u <username> -p <password> <ip-address>:27017/<collection-name>

Related

Start a postgres server with pgadmin

I'm trying to start a postgreSQL server in Debian 10 in order to access this database: https://catalogofraneitalia.wordpress.com/database/.
I have never used postgreSQL before and I have tried to follow the instructions I found on the internet. This is what I have run up to now:
sudo apt -y install gnupg2
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" |sudo tee /etc/apt/sources.list.d/pgdg.list
sudo apt update
sudo apt -y install postgresql-11
sudo apt install postgis postgresql-11-postgis-3
I checked that postgreSQL is active, enabled and accepting connections
sudo su - postgres, psql and create user username with superuser password 'password' with the username and password chosen
sudo apt install pgadmin3 pgadmin3-data
I opened pgadmin and I cliked on Add server, inserting the username and password previously created and I received the message Peer authentication failed for user "username"
the correct steps to install pg are in the official site: https://www.postgresql.org/download/
Follow the steps on screen, once initialized you can open the port (listen_address and pg_hba.conf), restart the service, and done... you can connect from other machine.
Other thing, pgadmin3 is deprecated, try using pgadmin4.

Can connect to Azure VM on Port 22 but not on port 5111

I have the network security port open on 5111:
I have firewall open on port 5111:
my postgresql is on port 5111:
I've restarted the server, ran diagnostics (nothing found from azure)
I can ssh to my VM but can't connect to postgres:
Any help is appreciated, thank you!
In case outbound rule is needed, here it is configured:
What helped was I installed netstat and executed command netstat | grep 0.0.0.0 to see if postgres was running and listening. I found out that it wasn't listening at all. Found out that the listen_addresses needed to be listen_addresses = '*'. That still didn't work, so I reinstalled it with these steps and it worked: https://gist.github.com/luizomf/1a7994cf4263e10dce416a75b9180f01
# Update all your packages
sudo apt update
sudo apt upgrade -y
# Add postgresql repository and key
sudo sh -c 'echo "deb [arch=$(dpkg --print-architecture)] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# Update again
sudo apt-get update
# Install pgadmin4
sudo apt install pgadmin4
# Install postgresql-13
sudo apt-get -y install postgresql-13
# Check version to see if it's correct
psql --Version
# Allow remote connections
# edit line #listen_addresses to listen_addresses = '*'
sudo nano /etc/postgresql/13/main/postgresql.conf
# edit file
sudo nano /etc/postgresql/13/main/pg_hba.conf
# add line at the end (change 192.168.0.0/24 to your network or 0.0.0.0/0 to all)
host all all 192.168.0.0/24 md5
# FOR SSL: add line at the end (change 192.168.0.0/24 to your network or 0.0.0.0/0 to all)
hostssl all all 192.168.0.0/24 md5 clientcert=1
# Restart postgres
sudo systemctl restart postgresql
# Access psql to create users, databases and passwords
sudo -u postgres psql
# Add a stronger password to default postgres user
alter user postgres with encrypted password 'the_postgres_user_password';
# Create user
create user your_username with encrypted password 'your_user_password';
# OR a superuser
CREATE ROLE your_username WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'your_user_password';
# Create a database
CREATE DATABASE db_name2 WITH OWNER your_username;
# Grant permissions to user on database
GRANT ALL PRIVILEGES ON DATABASE db_name TO your_username;
# Read security tips here
# https://www.digitalocean.com/community/tutorials/how-to-secure-postgresql-against-automated-attacks

Mongorestore command doesn't work as expected

i am trying to import my database to my mongoLab database, but it keep showing the following error:
2016-10-19T21:05:49.183+0800 Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
-bash: vd: command not found
This is how I ran my command:
mongorestore -h 243253423.mlab.com:2131242 -d meteor -u <Username> -p <Password> /Users/directory/desktop/mongo/dump
I ran across this error message and I am not sure this was your specific problem but this ended up working for me
mongorestore -d production-db \
-u myusername -p MyPassword \
--authenticationDatabase admin --host mydomain.com \
~/tmp/mongodump/local-production-db/
(be sure to check firewall sudo ufw status and net.bindIp sudo nano /etc/mongod.conf to confirm that you have access and your mongo process is listening on an external port)

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.

mongo is not connecting but installed and show version

i have following step.
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
sudo gedit /etc/apt/sources.list
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
sudo apt-get install mongodb-10gen
when i run command
mongo --version
result was
MongoDB shell version: 2.4.9
then at command
sudo start mongodb
result
mongodb start/running, process 5808
at command
sudo stop mongodb
result was
stop: Unknown instance:
at command
mongo
result was .MongoDB shell version: 2.4.9
connecting to: test
Sun Mar 2 18:57:50.050 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect failed
again on command
sudo service mongodb restart
stop: Unknown instance:
mongodb start/running, process 5873
mongo
MongoDB shell version: 2.4.9
connecting to: test
Sun Mar 2 18:57:50.050 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect failed
Did you try to remove .lock file from /var/lib/mongodb/ ?
sudo rm /var/lib/mongodb/mongod.lock
sudo service mongodb restart
Also you can try: sudo -u mongodb mongod --repair --dbpath /var/lib/mongodb/ and then start mongo again.
Hope this helps