In order for the Server/backend to function correctly, you need to setup a MySQL Database. This document only covers MySQL Server setup, you should refer to server setup documentation for configuring your backend with the database.
Assuming you are in db/
directory
Run following command to build database docker image
- Note: you MUST supply --build-arg so docker can setup the root password of database
- You should replace the password with something more secure
docker build -t yourmoon-db . --build-arg="ROOT_PASSWORD=root"
Now, you can launch the database with following command
docker run -d --rm --name yourmoon-db -p 3306:3306 yourmoon-db
First, you need to install MySQL Server on your machine
- Windows
- MacOS
- Ubuntu/Debian Linux
- Setup using XAMPP, a popular PHP development environment that will also setup MySQL Server for you
After you install MySQL Server on your machine, you should have a root account at this point.
Now, you can connect to your database via a MySQL Client like
- MySQL Workbench
- MySQL Shell
- You might have this software while installing MySQL Server
- Or other MySQL Client
Then, you need to initialize database and tables, just copy the content of Your Moon Database.sql and paste in your MySQL Client
This sql file will create a database YourMoonDB
with all the tables we need.
If you are using a MySQL Client or running the backend on your host machine (with npm run devStart
), you can simply connect to localhost:3306
.
If you are running both of backend and db inside docker containers, you need to setup a docker network so containers can find each other.
- Once your backend and db container are in the same network, you can use their name as host name, docker will do the DNS for us automatically. In the backend config, you can set:
// ...
"db": {
"host": "yourmoon-db",
"port": 3306,
"user": "root",
"password": "root",
"database": "YourMoonDB"
}
// ...
At this point, you already can use the database with the root account. However, it is a bad idea to deploy your app using database root account. So we recommend you to setup an user account for this application.
Go to file Init User.sql. This file contains all the commands you need for creating an user called yourmoonapp
who only have the necessary privileges for this app.
You need to modify the password by changing this line:
CREATE USER 'yourmoonapp'@'%' IDENTIFIED BY 'password';
Just replace password
with something more secure.
Then, you can copy the modified command to your MySQL Client and execute them with root account.
Optional Step
- you can ask MySQL Server to only allow this user connecting from specific IP by:
- replace all the character
%
in the file with your desire IP
- replace all the character
- This step will add an extra layer of protection to your database. You can only allow the connection coming from specific remote server
Refer to server setup documentation for configuring your backend with the database.