title: How to Start MongoDB as a Service on Ubuntu with a custom DBPath
body: <p>I have an Ubuntu server that was running MongoDB perfectly fine, but the server ran out of diskspace and crashed. I've cleared it out, but now I can't get Mongo back up and running as a service. </p>
  
  <p>This has happened before and I was able to solve it by doing the following:</p>
  
  <pre><code>sudo rm /var/lib/mongodb/mongod.lock
  sudo mongod --repair
  sudo chown mongodb /tmp/mongodb-27017.sock
  sudo service mongod start
  </code></pre>
  
  <p>But now when I do that, I'm getting this error during the repair:</p>
  
  <pre><code>exception in initAndListen: 29 Data directory /data/db not found., terminating
  </code></pre>
  
  <p>And when I append the location of the dbpath, I get a different error:</p>
  
  <pre><code> sudo mongod --dbpath /var/lib/mongodb --repair
   ...
   Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted
  Fatal Assertion 28578
  </code></pre>
  
  <p>What's weird is that, if I run the mongod service independently, everything works great:</p>
  
  <pre><code>sudo mongod --dbpath /var/lib/mongodb
  </code></pre>
  
  <p>I'm able to connect to it and everything is hunkydory, but I need this to run as a service so that I can close the terminal window. Any ideas what might be wrong?</p>
  
  <p>I suspect that for some reason, when I run the service mongo start command, it's not picking up the settings in mongod.conf.</p>
  
  <p>For good measure, here's what my /etc/mongod.conf looks like:</p>
  
  <pre><code># mongod.conf
  storage:
    dbPath: /var/lib/mongodb
    journal:
      enabled: false
  #  engine:
  #  mmapv1:
  #  wiredTiger:
  
  # where to write logging data.
  systemLog:
    destination: file
    logAppend: true
    path: /var/log/mongodb/mongod.log
  
  # network interfaces
  net:
    port: 27017
  #  bindIp: 127.0.0.1
  
  
  #processManagement:
  
  security:
      authorization : enabled
  #operationProfiling:
  
  #replication:
  
  #sharding:
  
  ## Enterprise-Only Options:
  
  #auditLog:
  
  #snmp:
  </code></pre>
  
  <p>UPDATE: I was able to make some progress. I copied the /etc/mongod.conf file to /data/db/mongod.conf and it appears to be finding the correct database now, however, now I'm getting a new error:</p>
  
  <pre><code>listen(): bind() failed errno:98 Address already in use for socket: /tmp/mongodb-27017.sock
    addr already in use
  Failed to set up sockets during startup.
  </code></pre>