Running MongoDB on Android

Rami Awar
PumpkinBox Blog
Published in
2 min readDec 29, 2020

--

A short guide on installing a MongoDB server as a service on Android over SSH from scratch.

Installing OpenSSH

Step one, we need to SSH into our phone. Make sure your computer and android device are on the same network. Download Termux to get access to a unix environment on your android.

Run Termux and install OpenSSH.

apt install openssh

To setup a passwordless ssh access (which is required when using Termux for reasons out of the scope of this guide), we need to copy our public ssh key from our computer to our android device, and add it to the authorized_keys. I won’t go into the details of generating an SSH key and explaining pub/priv keys here, so refer to another guide for this part. Once you have the key on your android device, you can run the following. (Note that you might have to change the permissions as necessary for SSH to work)

mkdir ~/.ssh
touch ~/.ssh/authorized_keys
cat <my_id_rsa.pub> > ~/.ssh/authorized_keys

Now we can run the ssh daemon using:

sshd

At this point we can log in to our android device through ssh and its ip address, and it should not ask for a password, for example:

ssh 192.168.0.100

Installing MongoDB Server

Step two is installing MongoDB. For this, another guide offers some helpful tools for setting this up:

Summarizing the steps mentioned there,

pkg install curl
curl -LO https://its-pointless.github.io/setup-pointless-repo.sh
bash setup-pointless-repo.sh
pkg update
pkg install mongodb

At this point, mongod and mongo are installed (server and client respectively). To run the server, simply:

mongod --bind_ip=0.0.0.0

Binding the IP is needed if you wish to access the database from another device through the IP address of the android device.

What is left is to set up mongod as a service, having it run automatically and have its lifecycle managed by a tiny service manager on Termux.

pkg install termux-services
exec "$SHELL"

We can now run sshd as a service using:

sv-enable sshd

We can check the status of the service using:

sv status sshd

As a final step, we can write our own service.

cp -r $PREFIX/var/service/sshd $PREFIX/var/service/mongodecho "exec mongod --bind_ip=0.0.0.0 2>&1" > $PREFIX/var/service/mongod/runln -sf $PREFIX/share/termux-services/svlogger $PREFIX/var/service/mongod/log/runexec "$SHELL"

And now we can run a MongoDB server as a service using:

sv-enable mongod

Hope someone finds this useful, sorry for not going into more details but this is more of a summary guide than an in-detail guide.

--

--