How to get MONGO_URL from command line Meteor Up deployment? - mongodb

I am currently deploying to Digital Ocean using Meteor Up. If I don't specify a MONGO_URL in the mup.json, can I get the value from the command line while the website is running, i.e. I don't want to shutdown the site?
If I go to the app directory and run meteor mongo --url, I get the following error:
mongo: Meteor isn't running a local MongoDB server.
This command only works while Meteor is running your application
locally. Start your application first. (This error will also occur if
you asked Meteor to use a different MongoDB server with $MONGO_URL when
you ran your application.)
If you're trying to connect to the database of an app you deployed
with 'meteor deploy', specify your site's name with this command.
Even if I run the app from the app directory, it will only give the localhost MONGO_URL. I need the MONGO_URL for the deployed app.
I have also taken a look at a similar question as suggested by some of the answers. I disagree that it is "impossible" to get the MONGO_URL without some other program running on the server. It's not as if we are defying the laws of physics here, folks. Fundamentally, there should be a way to access it. Just because no one has yet figured it out doesn't mean it is impossible.

meteor mongo --url should return the URL.
Try opening another shell in the app directory and running that command.

Meteor Up packages your app in production mode with meteor build so that it runs via node rather than the meteor command line interface. Among other things, this means meteor foo won't work on the remote server (at least not by default). So what you're really looking for is a way to access mongo itself remotely.
I recently set up mongo on an AWS EC2 instance and listed some lessons learned here: https://stackoverflow.com/a/28846703/2669596. Some details of how you do it are going to be different on Digital Ocean, but these are the main things you have to take care of once mongo itself is installed:
Public IP/DNS Address: This is probably fine already since you can deploy to the server.
Port Security Rules: You need to make sure port 27017 is open for TCP access, at least from your IP address. MongoDB also has an http interface you can set up; if you want to use that you'll need to open 28017 as well.
/etc/mongod.conf (file location may differ depending on Linux flavor):
Uncomment port=27017 to make sure you have the default port (I don't think this is actually necessary, but it made me feel better and it's good to know where to change the default port...).
Comment out bind_ip=127.0.0.1 in order to listen to external interfaces (e.g. remote connections).
Uncomment httpinterface=true if you want to use the http interface.
You may have to restart the mongod host via sudo service mongod restart. That's a problem if you can't have downtime, but I don't know of a way around that if you change the config file.
Create User: You need to create an admin and/or user to access the database remotely.
Once you've done all of that, you should be able to access the database from your local machine (assuming you have the mongo client installed locally) by running
mongo server.url.com:27017/mup-app-name -u username -p
where server.url.com is the URL or IP address of your remote server, mup-app-name is the appName parameter from your mup.json file, username is the user you created to access the database, and you'll be prompted for that user's password after you run the command (or you could put it after -p on the same line, depending on the password).
There may also be a way to do this by setting up nginx to reverse-proxy 127.0.0.1:27017 on your remote server, but I've never done it and that's just me speculating.

Related

How do I SSH from a Docker container to a remote server

I am building a docker image off postgres image, and I would like to seed it with some data.
I am following the initialization-scripts section of the documentation.
But the problem I am facing now, is that my initialisation scripts needs to ssh to a remote database and dumb data from there. Basically something like this:
ssh remote.host "pg_dump -U user -d somedb" > some.sql
but this fails with the error that ssh: command not found
Question now is, in general, how do I ssh from a docker container to a remote server. In this case, specifically how do I ssh from a docker container to a remote database server as part of the initialisation step of seeding a postgres database?
As a general rule you don't do things this way. Typical Docker images contain only the server they're running and some core tools, but network clients like ssh or curl generally aren't part of this. In the particular case of ssh, securely managing the credentials required is also tricky (not impossible, but not obvious).
In your particular case, I might rearrange things so that your scripts didn't have the hard assumption the database was running locally. Provision an empty database container, then run your script from the host targeting that empty database. It may even work to set the PGHOST and PGPORT environment variables to point to your host machine's host name and the port you publish the database interface on, and then run that script unmodified.
Looking closer at that specific command, you also may find it better to set up a cron job to run that specific database dump and put the contents somewhere. Then a developer can get a snapshot of the data without having to make a connection to the live database server, and you can limit the number of people who will have access. Once you have this dump file, you can use the /docker-entrypoint-initdb.d mechanism to cause it to be loaded at first startup time.

Move mongo database from vagrant outside of VM

Hello guys I'm trying to find a way to move my mongo database inside vagrant outside of it. I'm reading some posts in this forum but they're related to postgres and mysql.
When I run npm start this is the code I have in my package.json
"start": "MONGODB=mongodb://localhost:27017....
So the problem is that the databse will get saved in Virtual Machine localhost, so, by the time it runs it won't be accessible outside of VM. How can I change this localhost path to communicate outside?
It is not different wether it is vagrant or another server.
The db location files are specified in /etc/mongodb.conf. By default db are saved in /data/db
So the problem is that the databse will get saved in Virtual Machine localhost, so, by the time it runs it won't be accessible outside of VM. How can I change this localhost path to communicate outside?
If you want the db to be accessible from your host machine you need to replace localhost by the IP of the vagrant VM (if you specified a private IP) or better use the 0.0.0.0 so its accessible from all network interfaces
I did it, this link gave me the answer: Vagrant reverse port forwarding?
It seems that by default mongo will be located in 10.0.2.2 outside of vagrant, so if I run inside vagrant: mongo 10.0.2.2:27017 it connects to my databases outside of vagrant.
Therefore, this is what I need to put in my package.json to run npm start...
"start": "MONGODB=mongodb://10.0.2.2:27017/

Error when using mongoose.connect on AWS EC2 server

I've installed Node, express, and mongodb all successfully. I can run mongo in my terminal and it starts correctly. I can also see data i've manually stored.
Locally, I was using mongoose.connect('mongodb://localhost:27017/test'); and I had no issues. On my EC2 I used mongoose.connect('mongodb://ipaddress:27017/test'); but it's failing.
Error: failed to connect to [ipaddress:27017].
ipaddress is an actual ip address not a string or variable.
mongo
show dbs <-- this shows my databases so I know it's running!
I've looked online for a few hours and have come up short! I'm sure it's a simple setting i've missed.
On my EC2 i'm allowing all connections on all port ranges.
What am I missing?
Thanks!
Since the mongod instance is running on the same server, you need to set the IP Address to 0.0.0.0
I'm not sure why this needs to be done, but I got (some) understanding by reading the explanations listed on this post.

Can't connect to local (running) mongo using 'meteor mongo'

I'm playing with meteor, Installed it on Ubuntu 14.10 & followed the todos tutorial. The Meteor run log:
$ meteor run
[[[[[ ~/bckd/homedir/learn/meteor/mycoolapp ]]]]]
=> Started proxy.
=> Started MongoDB.
=> Started your app.
=> App running at: http://localhost:3000/
When I try to connect to mongo via another terminal, I get:
$ meteor mongo
mongo: Meteor isn't running a local MongoDB server.
This command only works while Meteor is running your application locally. Start your
application first. (This error will also occur if you asked Meteor to use a different MongoDB
server with $MONGO_URL when you ran your application.)
If you're trying to connect to the database of an app you deployed with `meteor deploy`,
specify your site's name with this command.
Trying to work around this problem I looked at the mongo command, and soon enough I figured I can connect using:
$ mongo localhost:3001
So great, that's working and it seems that I am able to use that for the time being.
But... why is the official way: meteor mongo not working in my setup, and can I do anything to fix it?
Edit 5/6/2015
After creating another account on my ubuntu, logging in and creating a new app, I run it and managed to connect to the mongo instance as intended with the meteor mongo command (without sudo). I thought it could be a result of a difference in my environment variables so I compared the two (dumping both environments with the env command. There were some extra bash variables in my primary account so one by one I've unset them until I reached two identical environments (with the exceptions of home directory values, user names, auto-generated values for gnome session tokens and such). Despite those changes, the problem consists. Another wild guess of mine was that the ~/.meteor folder has gone evil on me. So, I've removed it and reinstalled meteor. That didn't fix it either. Whatever the problem is, it's pretty stubborn.
Try:
sudo meteor mongo
it worked for me on a Vagrant box :)
You need to be the same user as the user who started Meteor.
Also you need to connect from within the application directory.
Can you connect to the database with the native MongoDB client?
Try running sudo meteor mongo - maybe the Meteor/Node process is running as root.
Looks like it's your environment variable problem.
export MONGO_URL=''
meteor mongo
Did you create the meteor app initially :
meteor create
I was having same issue on Ubuntu and found that i missed the create part as I copied the code from windows machine.
In my case(Windows 10),I had set MONGO_URL='remote url'.
In this case when you type 'meteor mongo' meteor will try to connect to the local mongodb server,but we are pointing to remote mogodb.
To point to local mongodb:
1) befor starting the app type 'set MONOGO_URL=' command in terminal.
2) now start the meteor app
3) now open a new terminal and try 'meteor mongo'
This worked for me with the same problem.
I had two terminals open: one running with meteor command, the other terminal I tried meteor mongo and received the very same message as stated in the question.
mongo: Meteor isn't running a local MongoDB server.
This command only works while Meteor is running your application locally. Start
your application first with 'meteor' and then run this command in a new
terminal. This error will also occur if you asked Meteor to use a different
MongoDB server with $MONGO_URL when you ran your application.
If you're trying to connect to the database of an app you deployed with
'meteor deploy', specify your site's name as an argument to this command.
I tried the sudo command, which did not work as well.
Here is what worked for me.
Goto running meteor server terminal and ctrl-c (shut down server).
In meteor server terminal type: meteor mongo.
Note: In my case the command worked and the terminal entered into mongo.
Type exit to exit mongo.
Type meteor to spool back up meteor server.
Goto meteor mongo terminal and type: meteor mongo.
Note: For me, Meteor was running in the other terminal and meteor mongo command did open meteor mongo for me without error.
Perhaps the something in Meteor needs to reboot which does not occur without shutting down meteor server first?
check if there is a running mongo process, and kill it manually:
ps -aux | grep mongo
sudo kill [pid]
..then restart your meteor app.
--> I think this specific situation exists only when starting meteor on a custom port (not tested, but pretty obvious)

NitrousIO mongodb always connecting to "test" db

I'm trying to setup mongodb on my nitrousio nodejs box. Following this tutorial.
When I finish executing this command,
mongo --shell --host $MONGODB_DEVELOPMENT_HOST --port $MONGODB_DEVELOPMENT_PORT
-u $MONGODB_DEVELOPMENT_USERNAME -p $MONGODB_DEVELOPMENT_PASSWORD
$MONGODB_DEVELOPMENT_DB
This happens,
Even though my MONGO_DEVELOPMENT_DB environment variable is set to pest, it tries to connect to test. Not only pest, if I try to connect to any of my DB instances, it always tries to connect to test.
Any idea as to what could be going wrong?
I had something similar recently and it turned our that the config file was overriding some of the parameters. Try to look at /usr/local/mongodb/mongod.conf (on OS X, probably similar on linux)
Ok, terminated the box and created a new one, that fixed it. Guess I messed up the env. variables in my first attempt. Works just fine now.
I had the same problem and I've tried with new boxes(minimum memory and storage) but it didn't work out.
After increasing memory and storage on existing box to maximum I could
(Memory: 640MB and Storage: 1500MB)
I was able to start MongoDB server and Mongo shell.
After starting the mongo shell your session will use the test database by default. For mong details, please refer to MongoDB Doc
I'm not sure if this will give you everything you need but at least I can ensure the mongo command interface works on Nitrous for me. Firstly a bit of background.
The helpful support guy at Nitrous told me that Nitrous only expose ports: 1024 - 10,000 open. The default for mongodb is: 27017. This port seems to be hand coded into the mongo command as well.
On Nitrous, I changed my mongodb.config file to use a port within that range, on the 0.0.0.0 address:
~/.parts/etc/mongodb.conf
bind_ip = 0.0.0.0
port = 7017
See:
default mongodb ports
config file format
Apparently the bind to 0.0.0.0 IP address is not necessary due to Nitrous magic, however I haven't tested that. After re-starting mongodb server:
$ parts stop mongodb
$ # ... edit ~/.parts/etc/mongodb.conf
$ parts start mongodb
$ mongo localhost:7017
Anyway that works for the default database. I expect other (valid, reasonable) command line options should be OK. The 'test' db name is the default, so if you never make a connection it mongodb probably won't even look at the DB Name given.
I am experimenting with the MEAN.io stack on Nitrous, so the config will be a quite different to your example. That said, this command works fine:
$ mongo localhost:7017/mean-dev
To open the mean-dev database. Hopefully setting the port will give you what is needed.
One last thing the mongodb control interface for that port is on the nitrous preview URL with port number +1,000 ==> 8017. I have not discovered how to make the REST API work with the admin interface (yet). Even so, the admin screen shows me that things are looking reasonable.
Good luck /w.