Deploying Spring Boot with Mysql on DigitalOcean droplet
1. Login to Digital Ocean account
2. Create a project
3. Create a new droplet
Create a new key pair and add to the droplet key section.
When droplet creation finishes then reset the password for root. It is mandatory.
4. Login using the console . I prefer to login using ssh.
        Â
ssh -i [filekey] root@ipaddress
    5.    Before doing anything run update command:
sudo apt update
Â
Â
Installing MySQL
1. After update command run command below
sudo apt install mysql-server
  Press Y if prompted and it will install mysql on the machine
2. Start the mysql server by running the command
sudo systemctl start mysql.service
3. Check the status all fine or not.      Â
sudo systemctl status mysql.service
This is show Active(running) status
Â
4. Now it's time to configure mysql
5. Alter the root user password first
sudo mysql
ALTER USER ârootâ@âlocalhostâ IDENTIFIED WITH mysql_native_passowrd BY âpasswordâ
6. Type exit.
7.    Now Login with root user   Â
sudo mysql -u root -p
8. Now run mysql secure script
sudo mysql_secure_installation
Provide the answer of prompted questions
9. Creating a dedicated user so that we can access this user outside the machine also.
mysql -u root -p
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO âuserâ@'hostâ;
FLUSH PRIVILEGES;
exit
Â
10. Restart the mysql server so that changes reflect.
sudo systemctl restart mysql.service
11. Finally done with mysql installation and now we can use mysql.
Let's Connect mysql to our local workbench
1. First , convert public key to pem
ssh-keygen -p -m PEM -f [yourpublickey]
Type this command to convert , make sure to backup the key first.
2. Open the workbench and add a new connection.
3. For Connection Name, enter any name youâd like that helps you identify the connection youâre making later.Â
4. Change the Connection Method to Standard TCP/IP over SSH.
5. For SSH Hostname, enter your droplet IP address. If your server accepts SSH connections on a different port, enter the IP address, followed by a colon and port number.
6. For SSH Username(root), enter the username you use to log into the server via SSH.
7. For SSH Password, enter the password you use for your SSH user. If you use public keys instead of passwords, select an SSH key for authentication.
8. For MySQL Hostname and MySQL Server Port, use the default values.
HostName: 127.0.0.1
Port: 3306
9. For Username, enter the MySQL username.
10. For Passwords, you can either enter the password or leave it blank. If you do not store the MySQL password in MySQL Workbench, a prompt will request the password each time you attempt to connect to the database.
11. Choose Test Connection to ensure your settings are correct.
12. Choose OK to create the connection.
Create your database using mysql work now.
Lets install Java and Deploy Spring boot
1.On you droplet console type command:
sudo apt install openjdk-11-jre-headless
sudo apt install openjdk-11-jdk-headless
I am installing java 11Â
Important: Using above command java 11 get installed but javac is not.
There is no need to install javac because we are going to just run application not compile or build but if you want you can install
2. Build your spring boot application with prod profile and server config details in your properties files.
3. Upload this jar to droplet machine folder using FileZilla in our case we created new folder with projects
4. Now Create two script with following commands.
start.sh
#!/bin/bash
nohup java -jar /projects/blog-app-apis-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod > /projects/log.txt 2>&1 &
echo $! > /projects/pid.file
stop.sh
kill $(cat /projects/pid.file)
Important : Change your folders path and filename accordingly.
5. Run start scriptÂ
sudo ./start.sh
Congratulation project is now running on you given port in project config file.
You can now test it onÂ
http://ip-adress:portnumber
Â