MongoDB Server Ubuntu 16.04 - mongodb

I've installed MongoDB on my Ubuntu 16.04 VPS and allowed connections through the firewall. When I run the server using the command (mongod), it starts without a problem and I can then connect to it (using RoboMongo as test application).
However, when I run the service automatically on startup (using systemctl), I cannot connect to it. I verified it was running, and could not run it myself as the address was already in use.
Does anyone have any experience with this, or any tips on how to solve the problem? The server will be running localhost only and I'm going to be using an SSH tunnel for testing purposes, so no authentication is necessary.
Anyone?

When you started mongod did you then provide a configuration file (using the --config or -f parameter)? If not then it defaults to listening on all interfaces.
The default configuration file which is used when starting the daemon with systemctl defaults to only listen on localhost preventing it from being available on the network. This is fine if you intend to connect to the server using a tunnel.

Related

How can I connect and use MongoDB Compass from Windows to MongoDb server on WSL2

I want to work with Mongo Compass running in Windows and connect to MongoDb server in WSL2.
Both MongoDb services are working fine (I can connect from Windows to MongoDb server on Windows and from WSL2 to MongoDb server on WSL2).
Windows build 19042.
Thanks!
Unless you are running a pretty old build (from 2018 or earlier) WSL includes a feature known as Localhost Forwarding, which should automatically allow you to use localhost in Mongo Compass in Windows to connect to the database running under WSL.
However, sometimes that features "breaks", especially if you hibernate or turn on Windows with the Fast Startup feature enabled (which is the default).
If this is the case, try wsl --shutdown and restart WSL. Then disable Fast Startup. If you do need to hibernate, remember that you may need to wsl --shutdown again to restore the forwarding mechanism.
See this answer for some additional details.
As far, as I understood, to access something running on WSL from you host-machine(Windows) you need to manually map host:port of your WSL MongoDB server to localhost.
This link describes the topic, I hope:
https://learn.microsoft.com/en-us/windows/wsl/networking#accessing-a-wsl-2-distribution-from-your-local-area-network-lan
After mapping your port you probably will connect to your MongoDB server via Compass with mapped address and port.

Flask running in WSL2 cannot connect to postgres running on windows (did work with WSL1)

I have started to develop a Flask app using VSCode/WSL1 and have then migrated from WSL1 to WSL2.
Since then, I cannot connect to my the database running on windows using "localhost"
DATABASE_URI = 'postgresql://sysadmin:mypassword#localhost/db_dev'
I can only connect if use the ip address found in "cat /etc/resolv.conf"
DATABASE_URI = 'postgresql://sysadmin:mypassword#172.31.96.1:5432/db_dev'
But that IP address changes on every reboot, so that is not convenient at all.
It looks like networking has changed quite a bit between WSL and WSL2, but I cannot find an acceptable solution to this I would say rather basic issue.
There are no problems with my Postgres config, as I can connect using the ip address.

Cannot access local MongoDB from sam local

I have Windows 10 machine where MongoDB is installed. I can connect it from a command line. I run NodeJS app with sam local. When I use a production environment, the app can access Mongo Atlas cloud instance. But when I switch to a dev environment with localhost MongoDB it fails to connect.
The sam command starts Docker so it is clear why it cannot connect Mongo running on windows localhost. I found relevant question: From inside of a Docker container, how do I connect to the localhost of the machine?. The problem is that I still cannot connect my local MongoDB, even if I try:
"MONGODB_URI": "mongodb://docker.for.win.localhost:27018/bud?retryWrites=true&w=majority"
or
"MONGODB_URI": "mongodb://host.docker.internal:27018/bud?retryWrites=true&w=majority"
Error:
Request failed { MongoNetworkError: failed to connect to server [docker.for.win.localhost:27018] on first connect [MongoNetworkError: connect ECONNREFUSED 192.168.65.2:27018]
Has anybody faced this issue as well and overcome it? Mongo is installed directly to windows, not in Docker.
If MongoDB is installed and running directly from windows, it should be accessible via localhost:27017. Default port for mongod is 27017, as described in mongoDB documentation page.
Try using:
"MONGODB_URI": "mongodb://localhost:27017/bud?retryWrites=true&w=majority"
If you are using NETWORKS_DRIVER other than bridge for your NodeJS docker container, which is set by default. Refer to Docker Network drivers
Other cases:
The default port for mongod is 27018 when running with --shardsvr command-line option or the shardsvr value for the clusterRole setting in a configuration file.
The default port for mongod is 27019 when running with --configsvr command-line option or the configsvr value for the clusterRole setting in a configuration file.
Remember, that localhost (or any name) is just for your convinience. Tcp stack works on ip addresses. If you configure dns service (e.g. via hosts file) to resolve name to 127.0.0.1 for container it doesn't mean your host, but 127.0.0.1 points to the container, always.
You could make mongo service to listen on your main ip and use it for docker app, but you can also leverage hyper-v virtual network cards and setup mongo to listen not only on host's loopback interface, but also on the virtual one and give docker app ip of that interface. It remains on your virtual lan, therefore it's not exposed to public. However, windows firewall might block it, so make sure you set it up as private network (it will be marked as unidentified and by default is public, which usually has stuff blocked).

Accessing WSL postgresql server from Windows PGadmin

I am running postgresql in WSL Ubuntu on windows. Everything is up to speed, my data is loaded and I wish to access the database through some graphical interface. I was thinking pgadmin4.
Is it possible to accomplish this through a windows install of pgadmin4? I installed pgadmin4 on windows and tried to connect the traditional way in the GUI through localhost but am not getting a connection. I figure there may be a special method here.
For everyone else stumbling across this: The best way to do this (that I know of) is to SSH into your local WSL and then configure the SSH in pgAdmin to that.
I have yet to figure out, how to use this remotely.
As long as postgres is running within your wsl2 instance (check with sudo service postgresql status) then within PgAdmin 4 (running in windows), all you need to do is click to "Register" a new server.
Then, while entering the Connection data, set host to localhost and port to 5432 (unless you specified unique port within your postgres instance when creating in wsl2.
See this answer for more detail
There is no special way needed, you should be able to get a connection. Just like your dev http ports are exposed to your browser on Windows, your db port should too.
I had to manually add localhost to pg admin though which is a bit weird.
Make sure your db service is up and running on Ubuntu, sometimes the db service is killed for no reason.
To see if your PostgresSQL service is up or not:
sudo service postgresql status
If it's not, start the service:
sudo service postgresql start

How to utilize GUI tool of pgadmin using vagrant

I have installed postgresql upon a vagrant ubuntu box and also companied control console with
sudo apt-get --assume-yes install pgadmin3
the tool is installed under /usr/bin
but I can not open pdadmin3 in the virtualbox due to the error
Error: Unable to initialize gtk, is DISPLAY set properly?
Could I possibly do this kind of thing in vagrant virtual box ? Or some work around
Run PgAdmin locally on your main desktop and connect to PostgreSQL on your Vagrant VM over TCP/IP.
The host can connect to guests if properly configured. You'll need to (a) ensure that Vagrant has suitable networking from host to guest, either using NAT and port forwards or using a virtual bridge; (b) ensure that PostgreSQL on the guest listens on all interfaces for TCP/IP connections using listen_addresses in postgresql.conf and (c) ensure that PostgreSQL on the guest is configured to allow connections from remote peers in pg_hba.conf.
Alternately, PgAdmin supports ssh tunnels. If you can ssh to the virtualbox you can configure PgAdmin to ssh to it too, then make a local PostgreSQL connection.
Personally I prefer to instead just use the psql command-line client for PostgreSQL directly on the VM. Way less hassle.