Question

How to deploy spring boot project with mongodb database in digitalocean.

Posted May 22, 2021 709 views
MongoDBDeployment

Hi,

I want to deploy my spring boot rest api project , where i used mongodb database,

(1) I created a droplet,
(2) then installed java,
(3) install tomcat server,
(4) installed mongodb server.

then, I generated a war file, and deployed, but it says false.

before generate war file, i have changed my application.properties mongodb configuration.

#for localhost

#spring.data.mongodb.host=localhost
#spring.data.mongodb.port=27017
#spring.data.mongodb.database=MultiVendor

#for droplet [I used it for deploy]

spring.data.mongodb.host=206.1**.1**.1**
spring.data.mongodb.port=27017
spring.data.mongodb.database=mvdb

when I run this applicaton in intellij idea with droplet host, port, console shows **Your localhost (206.1.1.1) is not a loopback adress
We can NOT send shutdown to mongod, because it is denied from remote.
**

also u can check my war file name, it autometically named multivendorshop-0.0.1-SNAPSHOT.war
and pom.xml file

<groupId>com.cyclicsoft</groupId>
<artifactId>multivendorshop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>multivendorshop</name>
<description>Multiventor Rest API</description>

once i have faced a problem, i rename a war file ROOT, it showed false in deployment.
is any problem with maven install?

I stucked with this deployment problem last couple of days, please help me.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there,

If your Java application and the MongoDB service are running on the same server, then you could still use localhost for the MongoDB host in your application properties file. This should fix the error that you are getting.

If you have two different servers for your database and your application, then you would need to configure MongoDB to accept remote connections. You can do that by following the steps here:

https://www.digitalocean.com/community/tutorials/how-to-configure-remote-access-for-mongodb-on-ubuntu-20-04

Hope that this helps!
Regards,
Bobby

by Mark Drake
This tutorial walks you through configuring a MongoDB installation to securely allow access from a trusted remote computer. This will involve updating your firewall rules to provide the remote server access to MongoDB's default port and changing Mongo's configuration file to update its IP binding setting.
  • hello, i logged into by ssh root@206.189.1@8.1@@
    i run command sudo ufw status
    it shows “`Status: active

    To Action From


    OpenSSH ALLOW Anywhere

    27017 ALLOW *206.189.1@8.1@@ *

    OpenSSH (v6) ALLOW Anywhere (v6) ”`

    i edit mongod.conf by sudo nano /etc/mongod.conf

    . . .
    # network interfaces
    net:
      port: 27017
      bindIp: 127.0.0.1,**206.189.1@8.1@@**
    
    . . .
    

    after that, when i run this nc -zv **206.189.1@8.1@@ ** 27017
    it says:

    it says nc: connect to 206.189.148.199 port 27017 (tcp) failed: Connection refused

    but when i run this nc -zv **127.0.0.1@@ ** 27017

    it says Connection to 127.0.0.1 27017 port [tcp/*] succeeded!

    i run my spring boot project in ntellij idea spring.data.mongodb.host=localhost
    spring.data.mongodb.port=27017
    spring.data.mongodb.database=MultiVendor

    it gives this error here

    *please note *that, i start my mongo server in local pc, when run project, local mongo server stop autometically

    please help me, i stucked last few days

    • Hi there,

      Did you also restart MongoDB after the change?

      Also if you are running your Java project locally and trying to connect to the MongoDB on the remote server, you need to allow your home IP address via ufw as well. You can find your IP by visiting this website here:

      http://ifconfig.io/

      Regards,
      Bobby

      • I run my java project from local computer(linux-ubuntu os),
        my local comuter ip: 103.147.163.233
        my digitalocean droplet ip: 206.189.148.199
        localhost ip: 127.0.0.1

        edit mongod.conf by sudo nano /etc/mongod.conf
        output:

        . . .
        #network interfaces
        net:
          port: 27017
          bindIp: 127.0.0.1,**206.189.148.199**
        
        . . .
        

        then i also added my local computer ip:

        . . .
        #network interfaces
        net:
          port: 27017
          bindIp: 127.0.0.1,**103.147.163.233**
        . . .
        
        

        saved, then
        (1) run nc -zv **206.189.148.199 ** 27017
        output nc: connect to 206.189.148.199 port 27017 (tcp) failed: Connection refused

        (2) run nc -zv **103.147.163.233 ** 27017
        output nc: connect to 103.147.163.233 port 27017 (tcp) failed: Connection refused

        (3) run nc -zv **127.0.0.1 ** 27017
        output nc: connect to 127.0.0.1 port 27017 (tcp) failed: Connection refused

        Is that approach is right?

        finally checed my UFW satatus by sudo ufw status

        output:

        To                         Action      From
        --                         ------      ----
        OpenSSH                    ALLOW       Anywhere                  
        27017                      ALLOW       206.189.148.199           
        22/tcp                     ALLOW       Anywhere                  
        27017                      ALLOW       103.147.163.233           
        OpenSSH (v6)               ALLOW       Anywhere (v6)             
        22/tcp (v6)                ALLOW       Anywhere (v6)  
        

        Is that configuration approach is right? if not, please help me.

        may be i mixing up my droplet IP and local pc IP.

        Thanks brother.

        • Hi there,

          Yes, so in the bind-address array you need to add the IP address of the server and for the firewall, you need to allow the IP address that you are connecting from.

          After the change to the MongoDB configuration file you need to restart the service and then make sure that it is running with the following command:

          systemctl status mongod
          

          Let me know how it goes.
          Regards,
          Bobby

          • Hi Bobby,

            i did it, tomcat is running, mongodb server is runing on my droplet.

            but i have a very few question,

            QUESTION-1: is my application.property file’s mongodb connection is okay?

            spring.data.mongodb.host=206.189.148.199
            spring.data.mongodb.port=27017
            spring.data.mongodb.database=mvdb
            

            please remember, so that you easily find out my mistakes.

            my local comuter ip: 103.147.163.233
            my digitalocean droplet ip: 206.189.148.199
            

            is that connection property okay?

            QUESTION-2: Which IP is need to add in bindIP array? 103.147.163.233 or 206.189.148.199 ?

            QUESTION-2: Which IP is need to add to allow in firewall? 103.147.163.233 or 206.189.148.199?

            QUESTION-3: what is the process to deploy war file?
            [3.1- what i did, run project in intellij idea, build as artifact and generate war file in ‘target’ folder, then deploy war in tomcat manager, is that process is okay?

            QUESTION-4: after creating new user in droplet
            why I can’t login my tomcat manager?[http://206.189.148.199:8080/manager/html]
            its shows

            This site can’t be reached206.189.148.199 took too long to respond.
            Try:
            
            Checking the connection
            Checking the proxy and the firewall
            ERR_CONNECTION_TIMED_OUT
            

            but my internet connection is okay. why this occured?

            please give my questions’s answer.

            thanks.

          • Hi there @tuhinal,

            Your setup should be the following:

            • In the bind IP section you need to specify the Droplet IP

            • For the firewall, you need to allow your public address on your home network that you can get by visiting https://ifconfig.io/

            That way the service will be accessible from your PC.

            Regarding the deployment process that you’ve described, I believe that it should work as expected.

            Regarding the timeout error, are you still seeing that? If so make sure that the port is still open via your firewall and that the service is running. I tried accessing the IP and it loaded as expected.

            Regards,
            Bobby